benchmark.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """
  2. bench
  3. """
  4. import os
  5. import shutil
  6. import json
  7. import calculate_score
  8. code_path = os.environ.get('GITHUB_WORKSPACE')
  9. #评测集存放路径
  10. pdf_dev_path = "datasets/"
  11. #magicpdf跑测结果
  12. pdf_res_path = "/tmp/magic-pdf"
  13. def test_cli():
  14. """
  15. test pdf-command cli
  16. """
  17. rm_cmd = f"rm -rf {pdf_res_path}"
  18. os.system(rm_cmd)
  19. os.makedirs(pdf_res_path)
  20. cmd = f'magic-pdf pdf-command --pdf {os.path.join(pdf_dev_path, "mineru")}'
  21. os.system(cmd)
  22. for root, dirs, files in os.walk(pdf_res_path):
  23. for magic_file in files:
  24. target_dir = os.path.join(pdf_dev_path, "mineru")
  25. if magic_file.endswith(".md"):
  26. source_file = os.path.join(root, magic_file)
  27. target_file = os.path.join(pdf_dev_path, "mineru", magic_file)
  28. if not os.path.exists(target_dir):
  29. os.makedirs(target_dir)
  30. shutil.copy(source_file, target_file)
  31. def get_score():
  32. """
  33. get score
  34. """
  35. data_path = os.path.join(pdf_dev_path, "ci")
  36. score = calculate_score.Scoring(os.path.join(data_path, "result.json"))
  37. score.calculate_similarity_total("mineru", data_path)
  38. res = score.summary_scores()
  39. return res
  40. def ci_ben():
  41. """
  42. ci benchmark
  43. """
  44. try:
  45. fr = open(os.path.join(pdf_dev_path, "result.json"), "r", encoding="utf-8")
  46. lines = fr.readlines()
  47. last_line = lines[-1].strip()
  48. last_score = json.loads(last_line)
  49. print ("last_score:", last_score)
  50. last_simscore = last_score["average_sim_score"]
  51. last_editdistance = last_score["average_edit_distance"]
  52. last_bleu = last_score["average_bleu_score"]
  53. except IOError:
  54. print ("result.json not exist")
  55. test_cli()
  56. os.system(f"python pre_clean.py --tool_name mineru --download_dir {pdf_dev_path}")
  57. now_score = get_score()
  58. print ("now_score:", now_score)
  59. now_simscore = now_score["average_sim_score"]
  60. now_editdistance = now_score["average_edit_distance"]
  61. now_bleu = now_score["average_bleu_score"]
  62. assert last_simscore <= now_simscore
  63. assert last_editdistance <= now_editdistance
  64. assert last_bleu <= now_bleu
  65. if __name__ == "__main__":
  66. os.system("sh env.sh")
  67. ci_ben()