Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,9 +38,12 @@ def process_tags(content: str) -> str:
|
|
| 38 |
def ocr_image_gradio_stream(image, max_tokens=4096):
|
| 39 |
"""Process image through Nanonets OCR model with streaming output"""
|
| 40 |
if image is None:
|
| 41 |
-
yield "Please upload an image."
|
| 42 |
return
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
prompt = """Extract the text from the above document as if you were reading it naturally. Return the tables in html format. Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>. Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number> or <page_number>9/22</page_number>. Prefer using β and β for check boxes."""
|
| 46 |
|
|
@@ -84,10 +87,14 @@ def ocr_image_gradio_stream(image, max_tokens=4096):
|
|
| 84 |
for new_text in streamer:
|
| 85 |
generated_text += new_text
|
| 86 |
processed_text = process_tags(generated_text)
|
| 87 |
-
yield processed_text
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
# Keep the original function for non-streaming use if needed
|
| 93 |
@spaces.GPU()
|
|
@@ -185,14 +192,14 @@ with gr.Blocks(title="Nanonets OCR Demo") as demo:
|
|
| 185 |
extract_btn.click(
|
| 186 |
fn=ocr_image_gradio_stream,
|
| 187 |
inputs=[image_input, max_tokens_slider],
|
| 188 |
-
outputs=output_text,
|
| 189 |
show_progress=True
|
| 190 |
)
|
| 191 |
|
| 192 |
image_input.change(
|
| 193 |
fn=ocr_image_gradio_stream,
|
| 194 |
inputs=[image_input, max_tokens_slider],
|
| 195 |
-
outputs=output_text,
|
| 196 |
show_progress=True
|
| 197 |
)
|
| 198 |
|
|
|
|
| 38 |
def ocr_image_gradio_stream(image, max_tokens=4096):
|
| 39 |
"""Process image through Nanonets OCR model with streaming output"""
|
| 40 |
if image is None:
|
| 41 |
+
yield "Please upload an image.", gr.update(interactive=True)
|
| 42 |
return
|
| 43 |
|
| 44 |
+
# Disable button at start of processing
|
| 45 |
+
yield "Processing image...", gr.update(interactive=False)
|
| 46 |
+
|
| 47 |
try:
|
| 48 |
prompt = """Extract the text from the above document as if you were reading it naturally. Return the tables in html format. Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>. Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number> or <page_number>9/22</page_number>. Prefer using β and β for check boxes."""
|
| 49 |
|
|
|
|
| 87 |
for new_text in streamer:
|
| 88 |
generated_text += new_text
|
| 89 |
processed_text = process_tags(generated_text)
|
| 90 |
+
yield processed_text, gr.update(interactive=False)
|
| 91 |
+
|
| 92 |
+
# Re-enable button when complete
|
| 93 |
+
yield processed_text, gr.update(interactive=True)
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
+
error_msg = f"Error processing image: {str(e)}"
|
| 97 |
+
yield error_msg, gr.update(interactive=True)
|
| 98 |
|
| 99 |
# Keep the original function for non-streaming use if needed
|
| 100 |
@spaces.GPU()
|
|
|
|
| 192 |
extract_btn.click(
|
| 193 |
fn=ocr_image_gradio_stream,
|
| 194 |
inputs=[image_input, max_tokens_slider],
|
| 195 |
+
outputs=[output_text, extract_btn],
|
| 196 |
show_progress=True
|
| 197 |
)
|
| 198 |
|
| 199 |
image_input.change(
|
| 200 |
fn=ocr_image_gradio_stream,
|
| 201 |
inputs=[image_input, max_tokens_slider],
|
| 202 |
+
outputs=[output_text, extract_btn],
|
| 203 |
show_progress=True
|
| 204 |
)
|
| 205 |
|