Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from pix2tex.cli import LatexOCR
|
| 4 |
+
import sympy as sp
|
| 5 |
+
from sympy.parsing.latex import parse_latex
|
| 6 |
+
import re
|
| 7 |
+
|
| 8 |
+
model = LatexOCR()
|
| 9 |
+
|
| 10 |
+
def preprocess_handwritten_image(pil_img):
|
| 11 |
+
return pil_img.convert('RGB')
|
| 12 |
+
|
| 13 |
+
def clean_latex2(latex):
|
| 14 |
+
latex = latex.replace('\\ ', '')
|
| 15 |
+
latex = latex.replace('\\\\', '\\')
|
| 16 |
+
latex = re.sub(r'\\[ \t\n\r\f\v]*', '', latex)
|
| 17 |
+
latex = latex.replace(r'\times', 'x')
|
| 18 |
+
latex = latex.replace(r'\cdot', '*')
|
| 19 |
+
latex = latex.replace('β', '-').replace('β', '-')
|
| 20 |
+
latex = re.sub(r'\\(text|mathbf|mathrm|mathit|textbf|mathcal|cal)\s*\{([^{}]+)\}', r'\2', latex)
|
| 21 |
+
latex = re.sub(r'\\[a-zA-Z]+', '', latex)
|
| 22 |
+
latex = re.sub(r'\{+\}+', '', latex)
|
| 23 |
+
latex = re.sub(r'\{([^\{\}]*)\}', r'\1', latex)
|
| 24 |
+
latex = re.sub(r'[;,]', '\n', latex)
|
| 25 |
+
latex = re.sub(r'[^\w\s=+\-*/().]', '', latex)
|
| 26 |
+
latex = re.sub(r'\s+', ' ', latex)
|
| 27 |
+
latex = re.sub(r'(?<![=<>])=(?![=<>])', ' = ', latex)
|
| 28 |
+
replacements = {
|
| 29 |
+
'chi': 'x', 'xi': 'x', 'alpha': 'x', 'beta': 'b',
|
| 30 |
+
'gamma': 'y', 'vartheta': '3', 'mathcal': '',
|
| 31 |
+
'cal': '', 'mathrm': ''
|
| 32 |
+
}
|
| 33 |
+
for wrong, right in replacements.items():
|
| 34 |
+
latex = re.sub(wrong, right, latex)
|
| 35 |
+
return latex.strip()
|
| 36 |
+
|
| 37 |
+
def solve_from_multiple_images(img1, img2, img3):
|
| 38 |
+
try:
|
| 39 |
+
images = [img for img in [img1, img2, img3] if img is not None]
|
| 40 |
+
if len(images) < 2:
|
| 41 |
+
return "β Please upload at least 2 images (each with one equation)."
|
| 42 |
+
|
| 43 |
+
raw_latexes = []
|
| 44 |
+
cleaned_eqs = []
|
| 45 |
+
for img in images:
|
| 46 |
+
img = preprocess_handwritten_image(img)
|
| 47 |
+
latex = model(img)
|
| 48 |
+
raw_latexes.append(latex)
|
| 49 |
+
cleaned = clean_latex2(latex)
|
| 50 |
+
if '=' in cleaned:
|
| 51 |
+
try:
|
| 52 |
+
parsed = parse_latex(cleaned)
|
| 53 |
+
if isinstance(parsed, sp.Equality):
|
| 54 |
+
cleaned_eqs.append(parsed)
|
| 55 |
+
except:
|
| 56 |
+
continue
|
| 57 |
+
|
| 58 |
+
if len(cleaned_eqs) < 2:
|
| 59 |
+
return "β Could not parse enough valid equations."
|
| 60 |
+
|
| 61 |
+
symbols = set()
|
| 62 |
+
for eq in cleaned_eqs:
|
| 63 |
+
symbols.update(eq.free_symbols)
|
| 64 |
+
|
| 65 |
+
solution = sp.solve(cleaned_eqs, list(symbols), dict=True)
|
| 66 |
+
output = "## π§Ύ Extracted Equations\n"
|
| 67 |
+
for latex in raw_latexes:
|
| 68 |
+
output += f"```latex\n{latex}\n```\n"
|
| 69 |
+
output += "---\n## βοΈ Parsed & Cleaned:\n"
|
| 70 |
+
for eq in cleaned_eqs:
|
| 71 |
+
output += f"$$ {sp.latex(eq)} $$\n"
|
| 72 |
+
output += "---\n## β
Solution:\n"
|
| 73 |
+
if solution:
|
| 74 |
+
output += "$$" + sp.latex(solution[0]) + "$$"
|
| 75 |
+
else:
|
| 76 |
+
output += "β No solution found or system is inconsistent."
|
| 77 |
+
return output
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return f"β Error: {str(e)}"
|
| 80 |
+
|
| 81 |
+
with gr.Blocks() as demo:
|
| 82 |
+
gr.Markdown("## π Solve System from Multiple Equation Images")
|
| 83 |
+
with gr.Row():
|
| 84 |
+
img1 = gr.Image(type="pil", label="π· Equation 1")
|
| 85 |
+
img2 = gr.Image(type="pil", label="π· Equation 2")
|
| 86 |
+
img3 = gr.Image(type="pil", label="π· (Optional) Equation 3")
|
| 87 |
+
solve_btn = gr.Button("π Solve System")
|
| 88 |
+
output = gr.Markdown()
|
| 89 |
+
solve_btn.click(fn=solve_from_multiple_images, inputs=[img1, img2, img3], outputs=output)
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
demo.launch()
|