winamnd commited on
Commit
4ee3a20
·
verified ·
1 Parent(s): 5d70ee9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -86,13 +86,18 @@ def generate_ocr(method, img):
86
  return text_output, label
87
 
88
  # Save results to JSON file
 
 
89
  def save_to_json(text, label):
90
- data = {"extracted_text": text, "classification": label}
91
- with open(RESULTS_JSON, "w") as f:
92
- json.dump(data, f, indent=4)
93
- return "Results saved to JSON file!"
 
 
 
94
 
95
- # Gradio Interface
96
  image_input = gr.Image()
97
  method_input = gr.Radio(["PaddleOCR", "EasyOCR", "KerasOCR"], value="PaddleOCR")
98
  output_text = gr.Textbox(label="Extracted Text")
@@ -100,7 +105,7 @@ output_label = gr.Textbox(label="Spam Classification")
100
  save_button = gr.Button("Save to JSON")
101
  save_output = gr.Textbox(label="Save Status")
102
 
103
- # Define the save button within the interface
104
  demo = gr.Interface(
105
  fn=generate_ocr,
106
  inputs=[method_input, image_input],
@@ -110,14 +115,12 @@ demo = gr.Interface(
110
  theme="compact",
111
  )
112
 
113
- # Create a separate interface for saving the results
114
- save_interface = gr.Interface(
115
  fn=save_to_json,
116
  inputs=[output_text, output_label],
117
- outputs=[save_output],
118
- live=False
119
  )
120
 
121
- # Launch both interfaces together
122
- demo.launch()
123
- save_interface.launch()
 
86
  return text_output, label
87
 
88
  # Save results to JSON file
89
+ RESULTS_JSON = "ocr_results.json"
90
+
91
  def save_to_json(text, label):
92
+ data = {"Extracted Text": text, "Spam Classification": label}
93
+
94
+ # Save to JSON file
95
+ with open(RESULTS_JSON, "w") as json_file:
96
+ json.dump(data, json_file, indent=4)
97
+
98
+ return f"Results saved to {RESULTS_JSON}"
99
 
100
+ # Create Gradio Interface
101
  image_input = gr.Image()
102
  method_input = gr.Radio(["PaddleOCR", "EasyOCR", "KerasOCR"], value="PaddleOCR")
103
  output_text = gr.Textbox(label="Extracted Text")
 
105
  save_button = gr.Button("Save to JSON")
106
  save_output = gr.Textbox(label="Save Status")
107
 
108
+ # Main OCR Interface
109
  demo = gr.Interface(
110
  fn=generate_ocr,
111
  inputs=[method_input, image_input],
 
115
  theme="compact",
116
  )
117
 
118
+ # *Attach Save Button to Function*
119
+ save_button.click(
120
  fn=save_to_json,
121
  inputs=[output_text, output_label],
122
+ outputs=[save_output]
 
123
  )
124
 
125
+ # Launch App
126
+ demo.launch()