Sangjun2 commited on
Commit
29a2056
·
verified ·
1 Parent(s): 8e3c4a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -53
app.py CHANGED
@@ -843,6 +843,7 @@ def real_time_check(image_file):
843
  highlighter2 = Highlighter()
844
  highlighter3=Highlighter()
845
  image = Image.open(image_file)
 
846
  result_model1 = predict_model1(image)
847
  parts=result_model1.split("\n")
848
  del parts[-1]
@@ -853,51 +854,41 @@ def real_time_check(image_file):
853
  result_model2 = predict_model2(image)
854
  aihub_deplot_generated_title=result_model2.split("\n")[1].split(":")[1]
855
  aihub_deplot_table=aihub_deplot_convert_to_dataframe(result_model2)
856
- image_base_name = os.path.basename(image_file.name).replace("Source","Label")
857
- file_name, _ = os.path.splitext(image_base_name)
858
 
859
  result_model3=predict_model3(image)
860
  unichart_table=unichart_convert_to_dataframe(result_model3)
861
  unichart_generated_title=result_model3.split(" & ")[0].split(" | ")[1]
862
 
863
  #aihub_labeling_data_json="./labeling_data/"+file_name+".json"
864
-
865
- json_path="./ko_deplot_labeling_data.json"
866
- with open(json_path, 'r', encoding='utf-8') as file:
867
- json_data = json.load(file)
868
- for key, value in json_data.items():
869
- if key == file_name:
870
- ko_deplot_labeling_str=value.get("txt").replace("<0x0A>","\n")
871
- ko_deplot_label_title=ko_deplot_labeling_str.split(" \n ")[0].split(" | ")[1]
872
- break
873
-
874
- ko_deplot_label_table=ko_deplot_convert_to_dataframe2(ko_deplot_labeling_str)
 
 
875
 
876
  #aihub_deplot_labeling_str=process_json_file2(aihub_labeling_data_json)
877
  #aihub_deplot_label_title=aihub_deplot_labeling_str.split("\n")[1].split(":")[1]
878
 
879
- image_row = aihub_deplot_result_df[aihub_deplot_result_df['data_id'] == file_name.replace("Label","Source")]
880
- label_table=""
881
- label_title=""
882
- if not image_row.empty:
883
- label_table = image_row['label_table'].values[0]
884
- label_title=label_table.split("\n")[1]
885
-
886
- aihub_deplot_label_table=aihub_deplot_convert_to_dataframe(label_table)
887
-
888
- json_path="./unichart_labeling_data.json"
889
- with open(json_path, 'r', encoding='utf-8') as file:
890
- json_data = json.load(file)
891
- for entry in json_data:
892
- if entry["imgname"]==os.path.basename(image_file.name):
893
- unichart_labeling_str=entry["label"]
894
- unichart_label_title=entry["label"].split(" & ")[0].split(" | ")[1]
895
- unichart_label_table=unichart_convert_to_dataframe(unichart_labeling_str)
896
-
897
- ko_deplot_RMS=evaluate_rms(result_model1,ko_deplot_labeling_str)
898
- aihub_deplot_RMS=evaluate_rms(result_model2,label_table)
899
- unichart_RMS=evaluate_rms(result_model3.replace("Characteristic","Title").replace("&","\n"),unichart_labeling_str.replace("Characteristic","Title").replace("&","\n"))
900
- ko_deplot_score_table=pd.DataFrame({
901
  'category': ['precision', 'recall', 'f1'],
902
  'value': [
903
  round(ko_deplot_RMS['table_datapoints_precision'],1),
@@ -905,16 +896,7 @@ def real_time_check(image_file):
905
  round(ko_deplot_RMS['table_datapoints_f1'],1)
906
  ]
907
  })
908
- aihub_deplot_score_table=pd.DataFrame({
909
- 'category': ['precision', 'recall', 'f1'],
910
- 'value': [
911
- round(aihub_deplot_RMS['table_datapoints_precision'],1),
912
- round(aihub_deplot_RMS['table_datapoints_recall'],1),
913
- round(aihub_deplot_RMS['table_datapoints_f1'],1)
914
- ]
915
- })
916
-
917
- unichart_score_table=pd.DataFrame({
918
  'category': ['precision', 'recall', 'f1'],
919
  'value': [
920
  round(unichart_RMS['table_datapoints_precision'],1),
@@ -923,14 +905,13 @@ def real_time_check(image_file):
923
  ]
924
  })
925
 
926
- ko_deplot_generated_df_row=ko_deplot_table.shape[0]
927
- aihub_deplot_generated_df_row=aihub_deplot_table.shape[0]
928
- unichart_generated_df_row=unichart_table.shape[0]
929
- styled_ko_deplot_table=ko_deplot_table.style.applymap(highlighter1.compare_and_highlight,target_table=ko_deplot_label_table,pred_table_row=ko_deplot_generated_df_row,props='color:red')
930
- styled_aihub_deplot_table=aihub_deplot_table.style.applymap(highlighter2.compare_and_highlight,target_table=aihub_deplot_label_table,pred_table_row=aihub_deplot_generated_df_row,props='color:red')
931
- styled_unichart_table=unichart_table.style.applymap(highlighter3.compare_and_highlight,target_table=unichart_label_table,pred_table_row=unichart_generated_df_row,props='color:red')
932
- return gr.DataFrame(styled_ko_deplot_table,label=ko_deplot_generated_title+"(VAIV_DePlot 추론 결과)") , gr.DataFrame(styled_aihub_deplot_table,label=aihub_deplot_generated_title+"(aihub deplot 추론 결과)"),gr.DataFrame(styled_unichart_table,label=unichart_generated_title+"(VAIV_UniChart 추론 결과)"),gr.DataFrame(ko_deplot_label_table,label=ko_deplot_label_title+"(VAIV_DePlot 정답 테이블)"),gr.DataFrame(aihub_deplot_label_table,label=label_title+"(aihub deplot 정답 테이블)"),gr.DataFrame(unichart_label_table,label=unichart_label_title+"(VAIV_UniChart 정답 테이블)"),ko_deplot_score_table, aihub_deplot_score_table,unichart_score_table
933
- #return ko_deplot_table,aihub_deplot_table,aihub_deplot_label_table,ko_deplot_score_table,aihub_deplot_score_table
934
  def inference(mode,image_uploader,file_uploader):
935
  if(mode=="이미지 업로드"):
936
  ko_deplot_table, aihub_deplot_table, unichart_table, ko_deplot_label_table,aihub_deplot_label_table,unichart_label_table,ko_deplot_score_table, aihub_deplot_score_table,unichart_score_table= real_time_check(image_uploader)
 
843
  highlighter2 = Highlighter()
844
  highlighter3=Highlighter()
845
  image = Image.open(image_file)
846
+
847
  result_model1 = predict_model1(image)
848
  parts=result_model1.split("\n")
849
  del parts[-1]
 
854
  result_model2 = predict_model2(image)
855
  aihub_deplot_generated_title=result_model2.split("\n")[1].split(":")[1]
856
  aihub_deplot_table=aihub_deplot_convert_to_dataframe(result_model2)
 
 
857
 
858
  result_model3=predict_model3(image)
859
  unichart_table=unichart_convert_to_dataframe(result_model3)
860
  unichart_generated_title=result_model3.split(" & ")[0].split(" | ")[1]
861
 
862
  #aihub_labeling_data_json="./labeling_data/"+file_name+".json"
863
+ if os.path.basename(image_file.name).startswith("C_Source"):
864
+ image_base_name = os.path.basename(image_file.name).replace("Source","Label")
865
+ file_name, _ = os.path.splitext(image_base_name)
866
+ json_path="./ko_deplot_labeling_data.json"
867
+ with open(json_path, 'r', encoding='utf-8') as file:
868
+ json_data = json.load(file)
869
+ for key, value in json_data.items():
870
+ if key == file_name:
871
+ ko_deplot_labeling_str=value.get("txt").replace("<0x0A>","\n")
872
+ ko_deplot_label_title=ko_deplot_labeling_str.split(" \n ")[0].split(" | ")[1]
873
+ break
874
+
875
+ ko_deplot_label_table=ko_deplot_convert_to_dataframe2(ko_deplot_labeling_str)
876
 
877
  #aihub_deplot_labeling_str=process_json_file2(aihub_labeling_data_json)
878
  #aihub_deplot_label_title=aihub_deplot_labeling_str.split("\n")[1].split(":")[1]
879
 
880
+ json_path="./unichart_labeling_data.json"
881
+ with open(json_path, 'r', encoding='utf-8') as file:
882
+ json_data = json.load(file)
883
+ for entry in json_data:
884
+ if entry["imgname"]==os.path.basename(image_file.name):
885
+ unichart_labeling_str=entry["label"]
886
+ unichart_label_title=entry["label"].split(" & ")[0].split(" | ")[1]
887
+ unichart_label_table=unichart_convert_to_dataframe(unichart_labeling_str)
888
+
889
+ ko_deplot_RMS=evaluate_rms(result_model1,ko_deplot_labeling_str)
890
+ unichart_RMS=evaluate_rms(result_model3.replace("Characteristic","Title").replace("&","\n"),unichart_labeling_str.replace("Characteristic","Title").replace("&","\n"))
891
+ ko_deplot_score_table=pd.DataFrame({
 
 
 
 
 
 
 
 
 
 
892
  'category': ['precision', 'recall', 'f1'],
893
  'value': [
894
  round(ko_deplot_RMS['table_datapoints_precision'],1),
 
896
  round(ko_deplot_RMS['table_datapoints_f1'],1)
897
  ]
898
  })
899
+ unichart_score_table=pd.DataFrame({
 
 
 
 
 
 
 
 
 
900
  'category': ['precision', 'recall', 'f1'],
901
  'value': [
902
  round(unichart_RMS['table_datapoints_precision'],1),
 
905
  ]
906
  })
907
 
908
+ ko_deplot_generated_df_row=ko_deplot_table.shape[0]
909
+ unichart_generated_df_row=unichart_table.shape[0]
910
+ styled_ko_deplot_table=ko_deplot_table.style.applymap(highlighter1.compare_and_highlight,target_table=ko_deplot_label_table,pred_table_row=ko_deplot_generated_df_row,props='color:red')
911
+ styled_unichart_table=unichart_table.style.applymap(highlighter3.compare_and_highlight,target_table=unichart_label_table,pred_table_row=unichart_generated_df_row,props='color:red')
912
+ return gr.DataFrame(styled_ko_deplot_table,label=ko_deplot_generated_title+"(VAIV_DePlot 추론 결과)") ,None,gr.DataFrame(styled_unichart_table,label=unichart_generated_title+"(VAIV_UniChart 추론 결과)"),gr.DataFrame(ko_deplot_label_table,label=ko_deplot_label_title+"(VAIV_DePlot 정답 테이블)"),None,gr.DataFrame(unichart_label_table,label=unichart_label_title+"(VAIV_UniChart 정답 테이블)"),ko_deplot_score_table,unichart_score_table
913
+ else:
914
+ return gr.DataFrame(ko_deplot_table,label=ko_deplot_generated_title+"(VAIV_DePlot 추론 결과)"),None,gr.DataFrame(unichart_table,label=unichart_generated_title+"(VAIV_UniChart 추론 결과)"),None,None,None,None,None,None
 
915
  def inference(mode,image_uploader,file_uploader):
916
  if(mode=="이미지 업로드"):
917
  ko_deplot_table, aihub_deplot_table, unichart_table, ko_deplot_label_table,aihub_deplot_label_table,unichart_label_table,ko_deplot_score_table, aihub_deplot_score_table,unichart_score_table= real_time_check(image_uploader)