Spaces:
Running
Running
Luis J Camargo commited on
Commit ·
378d343
1
Parent(s): 414d76f
feat: Add Gradio progress bar updates to the `run_inference` function for better user feedback.
Browse files
app.py
CHANGED
|
@@ -171,7 +171,7 @@ def _escape_inequalities_in_math(md: str) -> str:
|
|
| 171 |
|
| 172 |
# --- Inference Logic ---
|
| 173 |
|
| 174 |
-
def run_inference(img_path, task_type="ocr"):
|
| 175 |
if not PADDLE_AVAILABLE:
|
| 176 |
return "❌ Paddle backend not installed.", "", "", ""
|
| 177 |
|
|
@@ -183,6 +183,7 @@ def run_inference(img_path, task_type="ocr"):
|
|
| 183 |
|
| 184 |
try:
|
| 185 |
logger.info(f"--- Inference Start: {task_type} ---")
|
|
|
|
| 186 |
output = pipeline.predict(input=img_path)
|
| 187 |
logger.info(f"Output object type: {type(output)}")
|
| 188 |
logger.info(f"Output object: {output}")
|
|
@@ -196,26 +197,25 @@ def run_inference(img_path, task_type="ocr"):
|
|
| 196 |
os.makedirs(run_output_dir, exist_ok=True)
|
| 197 |
|
| 198 |
logger.info(f"will iterate")
|
| 199 |
-
|
| 200 |
-
logger.info(f"Output object: {res}")
|
| 201 |
-
res.print()
|
| 202 |
|
| 203 |
for i, res in enumerate(output):
|
| 204 |
logger.info(f"Processing segment {i+1}...")
|
|
|
|
|
|
|
| 205 |
# Save results
|
| 206 |
res.save_to_json(save_path=run_output_dir)
|
| 207 |
res.save_to_markdown(save_path=run_output_dir)
|
| 208 |
res.print()
|
| 209 |
|
| 210 |
-
# Read back generated files
|
| 211 |
-
# Paddle naming: res_{i}.md, res_{i}.json, etc.
|
| 212 |
fnames = os.listdir(run_output_dir)
|
| 213 |
for fname in fnames:
|
| 214 |
fpath = os.path.join(run_output_dir, fname)
|
| 215 |
if fname.endswith(".md"):
|
| 216 |
with open(fpath, 'r', encoding='utf-8') as f:
|
| 217 |
content = f.read()
|
| 218 |
-
if content not in md_content:
|
| 219 |
md_content += content + "\n\n"
|
| 220 |
elif fname.endswith(".json"):
|
| 221 |
with open(fpath, 'r', encoding='utf-8') as f:
|
|
@@ -227,12 +227,15 @@ def run_inference(img_path, task_type="ocr"):
|
|
| 227 |
vis_html += f'<div style="margin-bottom:20px; border: 2px solid #10b981; border-radius: 12px; overflow: hidden; background:white;">'
|
| 228 |
vis_html += f'<img src="{vis_src}" alt="Vis {i+1}" style="width:100%;">'
|
| 229 |
vis_html += f'</div>'
|
|
|
|
|
|
|
| 230 |
|
| 231 |
if not md_content:
|
| 232 |
md_content = "⚠️ Finished but no content was recognized."
|
| 233 |
|
| 234 |
md_preview = _escape_inequalities_in_math(md_content)
|
| 235 |
logger.info("--- Inference Finished Successfully ---")
|
|
|
|
| 236 |
return md_preview, md_content, vis_html, json_content
|
| 237 |
|
| 238 |
except Exception as e:
|
|
|
|
| 171 |
|
| 172 |
# --- Inference Logic ---
|
| 173 |
|
| 174 |
+
def run_inference(img_path, task_type="ocr", progress=gr.Progress()):
|
| 175 |
if not PADDLE_AVAILABLE:
|
| 176 |
return "❌ Paddle backend not installed.", "", "", ""
|
| 177 |
|
|
|
|
| 183 |
|
| 184 |
try:
|
| 185 |
logger.info(f"--- Inference Start: {task_type} ---")
|
| 186 |
+
progress(0, desc="📦 Initializing inference engine...")
|
| 187 |
output = pipeline.predict(input=img_path)
|
| 188 |
logger.info(f"Output object type: {type(output)}")
|
| 189 |
logger.info(f"Output object: {output}")
|
|
|
|
| 197 |
os.makedirs(run_output_dir, exist_ok=True)
|
| 198 |
|
| 199 |
logger.info(f"will iterate")
|
| 200 |
+
progress(0.2, desc="🔍 Parsing document structure...")
|
|
|
|
|
|
|
| 201 |
|
| 202 |
for i, res in enumerate(output):
|
| 203 |
logger.info(f"Processing segment {i+1}...")
|
| 204 |
+
progress((i + 1) / 5, desc=f"✍️ Recognizing content (segment {i+1})...")
|
| 205 |
+
|
| 206 |
# Save results
|
| 207 |
res.save_to_json(save_path=run_output_dir)
|
| 208 |
res.save_to_markdown(save_path=run_output_dir)
|
| 209 |
res.print()
|
| 210 |
|
| 211 |
+
# Read back generated files
|
|
|
|
| 212 |
fnames = os.listdir(run_output_dir)
|
| 213 |
for fname in fnames:
|
| 214 |
fpath = os.path.join(run_output_dir, fname)
|
| 215 |
if fname.endswith(".md"):
|
| 216 |
with open(fpath, 'r', encoding='utf-8') as f:
|
| 217 |
content = f.read()
|
| 218 |
+
if content not in md_content:
|
| 219 |
md_content += content + "\n\n"
|
| 220 |
elif fname.endswith(".json"):
|
| 221 |
with open(fpath, 'r', encoding='utf-8') as f:
|
|
|
|
| 227 |
vis_html += f'<div style="margin-bottom:20px; border: 2px solid #10b981; border-radius: 12px; overflow: hidden; background:white;">'
|
| 228 |
vis_html += f'<img src="{vis_src}" alt="Vis {i+1}" style="width:100%;">'
|
| 229 |
vis_html += f'</div>'
|
| 230 |
+
|
| 231 |
+
logger.info(f"Finished processing segment {i+1}")
|
| 232 |
|
| 233 |
if not md_content:
|
| 234 |
md_content = "⚠️ Finished but no content was recognized."
|
| 235 |
|
| 236 |
md_preview = _escape_inequalities_in_math(md_content)
|
| 237 |
logger.info("--- Inference Finished Successfully ---")
|
| 238 |
+
progress(1.0, desc="✅ Recovery complete")
|
| 239 |
return md_preview, md_content, vis_html, json_content
|
| 240 |
|
| 241 |
except Exception as e:
|