Varshith dharmaj commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import time
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import tempfile
|
| 8 |
+
import json
|
| 9 |
+
|
| 10 |
+
# Import consolidated modules
|
| 11 |
+
from ocr_module import MVM2OCREngine
|
| 12 |
+
from reasoning_engine import run_agent_orchestrator
|
| 13 |
+
from verification_service import calculate_symbolic_score
|
| 14 |
+
from consensus_fusion import evaluate_consensus
|
| 15 |
+
from report_module import generate_mvm2_report, export_to_pdf
|
| 16 |
+
from image_enhancing import ImageEnhancer
|
| 17 |
+
|
| 18 |
+
# Initialize Engines
|
| 19 |
+
ocr_engine = MVM2OCREngine()
|
| 20 |
+
enhancer = ImageEnhancer(sigma=1.2)
|
| 21 |
+
|
| 22 |
+
def process_mvm2_pipeline(image, auto_enhance):
|
| 23 |
+
if image is None:
|
| 24 |
+
return 'Please upload an image.', None, None
|
| 25 |
+
|
| 26 |
+
# 1. Preprocessing
|
| 27 |
+
if auto_enhance:
|
| 28 |
+
enhanced_img_np, meta = enhancer.enhance(image)
|
| 29 |
+
# Save temp image for OCR
|
| 30 |
+
temp_img_path = os.path.join(tempfile.gettempdir(), 'enhanced_input.png')
|
| 31 |
+
cv2.imwrite(temp_img_path, enhanced_img_np)
|
| 32 |
+
else:
|
| 33 |
+
# Save original PIL image
|
| 34 |
+
temp_img_path = os.path.join(tempfile.gettempdir(), 'original_input.png')
|
| 35 |
+
image.save(temp_img_path)
|
| 36 |
+
meta = {'metrics': {'initial_contrast': 0}}
|
| 37 |
+
|
| 38 |
+
# 2. OCR Extraction
|
| 39 |
+
ocr_results = ocr_engine.process_image(temp_img_path)
|
| 40 |
+
latex_text = ocr_results['latex_output']
|
| 41 |
+
ocr_conf = ocr_results['weighted_confidence']
|
| 42 |
+
|
| 43 |
+
if 'No math detected' in latex_text:
|
| 44 |
+
return f'OCR Failure: {latex_text}', None, None
|
| 45 |
+
|
| 46 |
+
# 3. Multi-Agent Reasoning
|
| 47 |
+
agent_responses = run_agent_orchestrator(latex_text)
|
| 48 |
+
|
| 49 |
+
# 4. Consensus Fusion
|
| 50 |
+
consensus_result = evaluate_consensus(agent_responses, ocr_confidence=ocr_conf)
|
| 51 |
+
|
| 52 |
+
# 5. Report Generation
|
| 53 |
+
reports = generate_mvm2_report(consensus_result, latex_text, ocr_conf)
|
| 54 |
+
md_report = reports['markdown']
|
| 55 |
+
json_report = json.loads(reports['json'])
|
| 56 |
+
|
| 57 |
+
# 6. Export to PDF
|
| 58 |
+
pdf_path = os.path.join(tempfile.gettempdir(), f'MVM2_Report_{reports["report_id"]}.pdf')
|
| 59 |
+
export_to_pdf(json_report, pdf_path)
|
| 60 |
+
|
| 61 |
+
return md_report, pdf_path, latex_text
|
| 62 |
+
|
| 63 |
+
# Custom CSS for Professional Educational Styling
|
| 64 |
+
custom_css = """
|
| 65 |
+
.gradio-container {
|
| 66 |
+
font-family: 'Inter', sans-serif;
|
| 67 |
+
}
|
| 68 |
+
.mvm2-header {
|
| 69 |
+
text-align: center;
|
| 70 |
+
background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%);
|
| 71 |
+
color: white;
|
| 72 |
+
padding: 20px;
|
| 73 |
+
border-radius: 10px;
|
| 74 |
+
margin-bottom: 20px;
|
| 75 |
+
}
|
| 76 |
+
.report-area {
|
| 77 |
+
background-color: #f9f9f9;
|
| 78 |
+
padding: 15px;
|
| 79 |
+
border-radius: 8px;
|
| 80 |
+
border: 1px solid #ddd;
|
| 81 |
+
}
|
| 82 |
+
"""
|
| 83 |
+
|
| 84 |
+
with gr.Blocks(css=custom_css, title='MVM2: Math Verification & Multi-Signal Consensus') as demo:
|
| 85 |
+
gr.Markdown(
|
| 86 |
+
"""
|
| 87 |
+
<div class="mvm2-header">
|
| 88 |
+
<h1>MVM2: Neuro-Symbolic Math Verification</h1>
|
| 89 |
+
<p>Adaptive Multi-Signal Consensus for Handwritten Mathematical Equation Verification</p>
|
| 90 |
+
</div>
|
| 91 |
+
"""
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
with gr.Column(scale=1):
|
| 96 |
+
input_img = gr.Image(type='pil', label='Upload Handwritten Math (Student Notebook)')
|
| 97 |
+
enhance_toggle = gr.Checkbox(label='Auto-Enhance for Handwritten Math (CLAHE + Gaussian Blur)', value=True)
|
| 98 |
+
run_btn = gr.Button('Run Multimodal Verification', variant='primary')
|
| 99 |
+
|
| 100 |
+
with gr.Column(scale=2):
|
| 101 |
+
with gr.Tabs():
|
| 102 |
+
with gr.TabItem('Explainable Diagnostic Report'):
|
| 103 |
+
report_output = gr.Markdown(label='Verification Report', elem_classes='report-area')
|
| 104 |
+
download_btn = gr.File(label='Download PDF Report')
|
| 105 |
+
with gr.TabItem('Raw OCR Extraction'):
|
| 106 |
+
ocr_output = gr.Textbox(label='Transcribed LaTeX', interactive=False)
|
| 107 |
+
|
| 108 |
+
gr.Markdown(
|
| 109 |
+
"""
|
| 110 |
+
### Project MVM2 Capabilities:
|
| 111 |
+
- Robust OCR: Pix2Text handles complex LaTeX commands and handwritten strokes.
|
| 112 |
+
- Neuro-Symbolic Fusion: Weighted Score_j formula combines LLM logic with SymPy validation.
|
| 113 |
+
- Hallucination Detection: Automatically flags agents with low consistency scores (< 0.7).
|
| 114 |
+
"""
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
run_btn.click(
|
| 118 |
+
fn=process_mvm2_pipeline,
|
| 119 |
+
inputs=[input_img, enhance_toggle],
|
| 120 |
+
outputs=[report_output, download_btn, ocr_output]
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
demo.launch()
|