Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -300,20 +300,41 @@ def run_ocr(image, keywords_raw: str):
|
|
| 300 |
with gr.Blocks(title="Document OCR Verifier") as demo:
|
| 301 |
gr.Markdown("Upload an image and provide comma-separated keywords (preserve spacing inside keywords).")
|
| 302 |
with gr.Row():
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
run_btn = gr.Button("Run OCR & Verify")
|
| 307 |
-
with gr.Column(scale=1):
|
| 308 |
-
img_preview = gr.Image(label="Uploaded Image Preview")
|
| 309 |
-
summary_card = gr.HTML(label="Summary")
|
| 310 |
-
|
| 311 |
-
# Raw JSON output area (both parsed JSON and raw string)
|
| 312 |
-
with gr.Row():
|
| 313 |
-
json_view = gr.JSON(label="Result (parsed JSON)")
|
| 314 |
-
raw_out = gr.Textbox(label="Result (raw JSON)", lines=20)
|
| 315 |
-
|
| 316 |
-
run_btn.click(fn=run_ocr, inputs=[img_in, kws], outputs=[img_preview, summary_card, json_view, raw_out])
|
| 317 |
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
with gr.Blocks(title="Document OCR Verifier") as demo:
|
| 301 |
gr.Markdown("Upload an image and provide comma-separated keywords (preserve spacing inside keywords).")
|
| 302 |
with gr.Row():
|
| 303 |
+
img_in = gr.File(label="Image (JPEG/PNG). The file will be stored in /tmp.")
|
| 304 |
+
kws = gr.Textbox(label="User keywords (comma-separated). Example: ROHIT, KUMAR, SINGH")
|
| 305 |
+
run_btn = gr.Button("Run OCR & Verify")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
+
with gr.Row():
|
| 308 |
+
with gr.Column():
|
| 309 |
+
card = gr.HTML("""
|
| 310 |
+
<div style='border:1px solid #ccc;padding:16px;border-radius:12px;width:100%;'>
|
| 311 |
+
<h3>Document Summary</h3>
|
| 312 |
+
<p><strong>Document Type:</strong> <span id='doc_type'></span></p>
|
| 313 |
+
<p><strong>Accuracy:</strong> <span id='doc_acc'></span></p>
|
| 314 |
+
<p><strong>User Keywords:</strong> <span id='user_kws'></span></p>
|
| 315 |
+
<p><strong>Status:</strong> <span id='verif_status' style='font-weight:bold;'></span></p>
|
| 316 |
+
</div>
|
| 317 |
+
""")
|
| 318 |
+
out = gr.Textbox(label="Result (raw JSON)", lines=20)
|
| 319 |
+
|
| 320 |
+
def update_card(json_str):
|
| 321 |
+
try:
|
| 322 |
+
data = json.loads(json_str)
|
| 323 |
+
status_color = "green" if data.get("verificationStatus") == "verified" else "red"
|
| 324 |
+
html = f"""
|
| 325 |
+
<div style='border:1px solid #ccc;padding:16px;border-radius:12px;width:100%;'>
|
| 326 |
+
<h3>Document Summary</h3>
|
| 327 |
+
<p><strong>Document Type:</strong> {data.get('documentType')}</p>
|
| 328 |
+
<p><strong>Accuracy:</strong> {data.get('documentTypeAccuracy')}</p>
|
| 329 |
+
<p><strong>User Keywords:</strong> {', '.join(data.get('inputUserKeywords', []))}</p>
|
| 330 |
+
<p><strong>Status:</strong> <span style='color:{status_color};font-weight:bold;'>{data.get('verificationStatus')}</span></p>
|
| 331 |
+
</div>
|
| 332 |
+
"""
|
| 333 |
+
return html
|
| 334 |
+
except:
|
| 335 |
+
return "<div>Invalid JSON</div>"
|
| 336 |
+
|
| 337 |
+
run_btn.click(fn=run_ocr, inputs=[img_in, kws], outputs=[out])
|
| 338 |
+
out.change(fn=update_card, inputs=[out], outputs=[card])
|
| 339 |
+
|
| 340 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)(server_name="0.0.0.0", server_port=7860)
|