Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -78,42 +78,44 @@ def format_step_viewer(consensus_result):
|
|
| 78 |
|
| 79 |
def process_mvm2_pipeline(image, auto_enhance):
|
| 80 |
if image is None:
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
# 1. Preprocessing & Preview
|
|
|
|
| 84 |
enhanced_img_np, meta = enhancer.enhance(image)
|
| 85 |
temp_img_path = os.path.join(tempfile.gettempdir(), 'input_processed.png')
|
| 86 |
cv2.imwrite(temp_img_path, enhanced_img_np)
|
| 87 |
|
| 88 |
preview_img = Image.fromarray(cv2.cvtColor(enhanced_img_np, cv2.COLOR_BGR2RGB))
|
| 89 |
-
|
|
|
|
| 90 |
# 2. OCR Extraction
|
| 91 |
ocr_results = ocr_engine.process_image(temp_img_path)
|
| 92 |
latex_text = ocr_results['latex_output']
|
| 93 |
ocr_conf = ocr_results['weighted_confidence']
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
# 3. Multi-Agent Reasoning
|
| 99 |
agent_responses = run_agent_orchestrator(latex_text)
|
| 100 |
-
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
#
|
|
|
|
| 106 |
consensus_result = evaluate_consensus(agent_responses, ocr_confidence=ocr_conf)
|
| 107 |
|
| 108 |
-
#
|
| 109 |
for i, score_data in enumerate(consensus_result["detail_scores"]):
|
| 110 |
-
# Match by agent name
|
| 111 |
for res in agent_responses:
|
| 112 |
if res["agent"] == score_data["agent"]:
|
| 113 |
consensus_result["detail_scores"][i]["reasoning_trace"] = res["response"].get("Reasoning Trace", [])
|
| 114 |
break
|
| 115 |
|
| 116 |
-
#
|
| 117 |
avg_v_sym = np.mean([s["V_sym"] for s in consensus_result["detail_scores"]])
|
| 118 |
avg_l_logic = np.mean([s["L_logic"] for s in consensus_result["detail_scores"]])
|
| 119 |
avg_c_clf = np.mean([s["C_clf"] for s in consensus_result["detail_scores"]])
|
|
@@ -126,7 +128,6 @@ def process_mvm2_pipeline(image, auto_enhance):
|
|
| 126 |
</div>
|
| 127 |
"""
|
| 128 |
|
| 129 |
-
# Final Calibration Bar
|
| 130 |
winner = consensus_result["winning_score"]
|
| 131 |
calibrated_conf = winner * (0.9 + 0.1 * ocr_conf)
|
| 132 |
conf_bar = f"""
|
|
@@ -136,12 +137,12 @@ def process_mvm2_pipeline(image, auto_enhance):
|
|
| 136 |
<span style="color: #10b981; font-weight: bold;">{calibrated_conf:.3f}</span>
|
| 137 |
</div>
|
| 138 |
<div style="width: 100%; bg: rgba(255,255,255,0.05); height: 8px; border-radius: 4px; overflow: hidden;">
|
| 139 |
-
<div style="width: {min(100, calibrated_conf*
|
| 140 |
</div>
|
| 141 |
</div>
|
| 142 |
"""
|
| 143 |
|
| 144 |
-
#
|
| 145 |
reports = generate_mvm2_report(consensus_result, latex_text, ocr_conf)
|
| 146 |
md_report = format_step_viewer(consensus_result)
|
| 147 |
|
|
@@ -150,7 +151,7 @@ def process_mvm2_pipeline(image, auto_enhance):
|
|
| 150 |
|
| 151 |
final_flow = generate_flow_html("success")
|
| 152 |
|
| 153 |
-
|
| 154 |
|
| 155 |
# Build Interface
|
| 156 |
with gr.Blocks(css=css_content, title="MVM²: Senior UI AI Dashboard") as demo:
|
|
|
|
| 78 |
|
| 79 |
def process_mvm2_pipeline(image, auto_enhance):
|
| 80 |
if image is None:
|
| 81 |
+
yield None, "Please upload an image.", None, "", None, None, generate_flow_html("idle")
|
| 82 |
+
return
|
| 83 |
|
| 84 |
# 1. Preprocessing & Preview
|
| 85 |
+
yield None, "Enhancing image...", None, "", None, None, generate_flow_html("enhance")
|
| 86 |
enhanced_img_np, meta = enhancer.enhance(image)
|
| 87 |
temp_img_path = os.path.join(tempfile.gettempdir(), 'input_processed.png')
|
| 88 |
cv2.imwrite(temp_img_path, enhanced_img_np)
|
| 89 |
|
| 90 |
preview_img = Image.fromarray(cv2.cvtColor(enhanced_img_np, cv2.COLOR_BGR2RGB))
|
| 91 |
+
yield preview_img, "Extracting LaTeX...", None, "", None, None, generate_flow_html("ocr")
|
| 92 |
+
|
| 93 |
# 2. OCR Extraction
|
| 94 |
ocr_results = ocr_engine.process_image(temp_img_path)
|
| 95 |
latex_text = ocr_results['latex_output']
|
| 96 |
ocr_conf = ocr_results['weighted_confidence']
|
| 97 |
|
| 98 |
+
yield preview_img, latex_text, None, "", None, None, generate_flow_html("reasoning")
|
| 99 |
+
|
|
|
|
| 100 |
# 3. Multi-Agent Reasoning
|
| 101 |
agent_responses = run_agent_orchestrator(latex_text)
|
| 102 |
+
|
| 103 |
+
# 4. Advanced Heuristics Refinement stage
|
| 104 |
+
yield preview_img, latex_text, None, "", None, None, generate_flow_html("heuristics")
|
| 105 |
+
time.sleep(0.5)
|
| 106 |
+
|
| 107 |
+
# 5. Consensus Fusion
|
| 108 |
+
yield preview_img, latex_text, None, "", None, None, generate_flow_html("consensus")
|
| 109 |
consensus_result = evaluate_consensus(agent_responses, ocr_confidence=ocr_conf)
|
| 110 |
|
| 111 |
+
# Attach traces back to detail_scores for UI formatting
|
| 112 |
for i, score_data in enumerate(consensus_result["detail_scores"]):
|
|
|
|
| 113 |
for res in agent_responses:
|
| 114 |
if res["agent"] == score_data["agent"]:
|
| 115 |
consensus_result["detail_scores"][i]["reasoning_trace"] = res["response"].get("Reasoning Trace", [])
|
| 116 |
break
|
| 117 |
|
| 118 |
+
# 6. Gauges & UI Elements
|
| 119 |
avg_v_sym = np.mean([s["V_sym"] for s in consensus_result["detail_scores"]])
|
| 120 |
avg_l_logic = np.mean([s["L_logic"] for s in consensus_result["detail_scores"]])
|
| 121 |
avg_c_clf = np.mean([s["C_clf"] for s in consensus_result["detail_scores"]])
|
|
|
|
| 128 |
</div>
|
| 129 |
"""
|
| 130 |
|
|
|
|
| 131 |
winner = consensus_result["winning_score"]
|
| 132 |
calibrated_conf = winner * (0.9 + 0.1 * ocr_conf)
|
| 133 |
conf_bar = f"""
|
|
|
|
| 137 |
<span style="color: #10b981; font-weight: bold;">{calibrated_conf:.3f}</span>
|
| 138 |
</div>
|
| 139 |
<div style="width: 100%; bg: rgba(255,255,255,0.05); height: 8px; border-radius: 4px; overflow: hidden;">
|
| 140 |
+
<div style="width: {min(100, calibrated_conf*100)}%; background: linear-gradient(90deg, #6366f1 0%, #10b981 100%); height: 100%; transition: width 1s ease;"></div>
|
| 141 |
</div>
|
| 142 |
</div>
|
| 143 |
"""
|
| 144 |
|
| 145 |
+
# 7. Report & PDF
|
| 146 |
reports = generate_mvm2_report(consensus_result, latex_text, ocr_conf)
|
| 147 |
md_report = format_step_viewer(consensus_result)
|
| 148 |
|
|
|
|
| 151 |
|
| 152 |
final_flow = generate_flow_html("success")
|
| 153 |
|
| 154 |
+
yield preview_img, latex_text, gauges_html, conf_bar, md_report, pdf_path, final_flow
|
| 155 |
|
| 156 |
# Build Interface
|
| 157 |
with gr.Blocks(css=css_content, title="MVM²: Senior UI AI Dashboard") as demo:
|