Spaces:
Running
on
Zero
Running
on
Zero
wjm55
commited on
Commit
·
71089ff
1
Parent(s):
525b601
trying to use state to save the output
Browse files
app.py
CHANGED
@@ -149,7 +149,7 @@ def run_example(image, model_id="Qwen/Qwen2-VL-7B-Instruct", run_ner=False, ner_
|
|
149 |
|
150 |
# Create TextWithMetadata instance with the highlighted text and metadata
|
151 |
result = TextWithMetadata(highlighted_text, original_text=ocr_text, entities=ner_results)
|
152 |
-
return result
|
153 |
|
154 |
# If NER is disabled, return the text without highlighting
|
155 |
result = TextWithMetadata([(ocr_text, None)], original_text=ocr_text, entities=[])
|
@@ -227,6 +227,9 @@ css = """
|
|
227 |
"""
|
228 |
|
229 |
with gr.Blocks(css=css) as demo:
|
|
|
|
|
|
|
230 |
gr.Image("Caracal.jpg", interactive=False)
|
231 |
with gr.Tab(label="Image Input", elem_classes="tabs"):
|
232 |
with gr.Row():
|
@@ -254,20 +257,21 @@ with gr.Blocks(css=css) as demo:
|
|
254 |
outputs=[ner_labels]
|
255 |
)
|
256 |
|
257 |
-
#
|
258 |
submit_btn.click(
|
259 |
run_example,
|
260 |
inputs=[input_img, model_selector, ner_checkbox, ner_labels],
|
261 |
-
outputs=[output_text]
|
262 |
)
|
263 |
with gr.Row():
|
264 |
filename = gr.Textbox(label="Save filename (without extension)", placeholder="Enter filename to save")
|
265 |
download_btn = gr.Button("Download Image & Text", elem_classes="submit-btn")
|
266 |
download_output = gr.File(label="Download")
|
267 |
|
268 |
-
|
|
|
269 |
# Validate inputs
|
270 |
-
if not fname or not
|
271 |
return None
|
272 |
|
273 |
try:
|
@@ -280,9 +284,9 @@ with gr.Blocks(css=css) as demo:
|
|
280 |
img_path = os.path.join(temp_dir, f"{fname}.png")
|
281 |
image.save(img_path)
|
282 |
|
283 |
-
#
|
284 |
-
original_text =
|
285 |
-
entities =
|
286 |
|
287 |
# Save text
|
288 |
txt_path = os.path.join(temp_dir, f"{fname}.txt")
|
@@ -317,10 +321,10 @@ with gr.Blocks(css=css) as demo:
|
|
317 |
print(f"Error creating zip: {str(e)}")
|
318 |
return None
|
319 |
|
320 |
-
#
|
321 |
download_btn.click(
|
322 |
create_zip,
|
323 |
-
inputs=[input_img, output_text, filename],
|
324 |
outputs=[download_output]
|
325 |
)
|
326 |
|
|
|
149 |
|
150 |
# Create TextWithMetadata instance with the highlighted text and metadata
|
151 |
result = TextWithMetadata(highlighted_text, original_text=ocr_text, entities=ner_results)
|
152 |
+
return result, result # Return twice: once for display, once for state
|
153 |
|
154 |
# If NER is disabled, return the text without highlighting
|
155 |
result = TextWithMetadata([(ocr_text, None)], original_text=ocr_text, entities=[])
|
|
|
227 |
"""
|
228 |
|
229 |
with gr.Blocks(css=css) as demo:
|
230 |
+
# Add state variables to store OCR results
|
231 |
+
ocr_state = gr.State()
|
232 |
+
|
233 |
gr.Image("Caracal.jpg", interactive=False)
|
234 |
with gr.Tab(label="Image Input", elem_classes="tabs"):
|
235 |
with gr.Row():
|
|
|
257 |
outputs=[ner_labels]
|
258 |
)
|
259 |
|
260 |
+
# Modify the submit button click handler to update state
|
261 |
submit_btn.click(
|
262 |
run_example,
|
263 |
inputs=[input_img, model_selector, ner_checkbox, ner_labels],
|
264 |
+
outputs=[output_text, ocr_state] # Add ocr_state to outputs
|
265 |
)
|
266 |
with gr.Row():
|
267 |
filename = gr.Textbox(label="Save filename (without extension)", placeholder="Enter filename to save")
|
268 |
download_btn = gr.Button("Download Image & Text", elem_classes="submit-btn")
|
269 |
download_output = gr.File(label="Download")
|
270 |
|
271 |
+
# Modify create_zip to use the state data
|
272 |
+
def create_zip(image, text_data, fname, ocr_result):
|
273 |
# Validate inputs
|
274 |
+
if not fname or not image or not isinstance(image, (Image.Image, np.ndarray)):
|
275 |
return None
|
276 |
|
277 |
try:
|
|
|
284 |
img_path = os.path.join(temp_dir, f"{fname}.png")
|
285 |
image.save(img_path)
|
286 |
|
287 |
+
# Use the OCR result from state
|
288 |
+
original_text = ocr_result.original_text if ocr_result else ""
|
289 |
+
entities = ocr_result.entities if ocr_result else []
|
290 |
|
291 |
# Save text
|
292 |
txt_path = os.path.join(temp_dir, f"{fname}.txt")
|
|
|
321 |
print(f"Error creating zip: {str(e)}")
|
322 |
return None
|
323 |
|
324 |
+
# Update the download button click handler to include state
|
325 |
download_btn.click(
|
326 |
create_zip,
|
327 |
+
inputs=[input_img, output_text, filename, ocr_state],
|
328 |
outputs=[download_output]
|
329 |
)
|
330 |
|