Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ model = LatexOCR()
|
|
| 10 |
def preprocess_handwritten_image(pil_img):
|
| 11 |
return pil_img.convert('RGB')
|
| 12 |
|
|
|
|
| 13 |
def clean_latex(latex):
|
| 14 |
latex = latex.replace('\\ ', '')
|
| 15 |
latex = latex.replace('\\\\', '\\')
|
|
@@ -49,6 +50,27 @@ def clean_latex(latex):
|
|
| 49 |
latex = latex.replace('e', '2.7183')
|
| 50 |
return latex
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def solve_polynomial(image):
|
| 53 |
try:
|
| 54 |
img = preprocess_handwritten_image(image)
|
|
@@ -93,12 +115,6 @@ def wrapped_solver(img):
|
|
| 93 |
result, cleaned, prompt = solve_polynomial(img)
|
| 94 |
return result, cleaned, prompt
|
| 95 |
|
| 96 |
-
# def solve_from_coeffs(degree, coeff_str):
|
| 97 |
-
# ...
|
| 98 |
-
|
| 99 |
-
# def request_llm_explanation(prompt, llm_url):
|
| 100 |
-
# ...
|
| 101 |
-
|
| 102 |
with gr.Blocks() as demo:
|
| 103 |
with gr.Tab("๐ผ๏ธ Parse from Image"):
|
| 104 |
image_input = gr.Image(type="pil", label="๐ท Upload Image of Polynomial")
|
|
@@ -107,12 +123,6 @@ with gr.Blocks() as demo:
|
|
| 107 |
output_box = gr.Markdown(label="๐ Step-by-step Solution")
|
| 108 |
submit_btn = gr.Button("๐ Solve")
|
| 109 |
submit_btn.click(fn=wrapped_solver, inputs=[image_input], outputs=[output_box, hidden_latex, explanation_prompt])
|
| 110 |
-
# explain_box = gr.Markdown(label="๐ฃ๏ธ Human-style Explanation")
|
| 111 |
-
# explain_btn = gr.Button("๐ง Explain Human-Solution")
|
| 112 |
-
# explain_btn.click(fn=request_llm_explanation, inputs=[explanation_prompt, llm_url], outputs=explain_box)
|
| 113 |
-
|
| 114 |
-
# with gr.Tab("๐งฎ Solve by Coefficients"):
|
| 115 |
-
# ...
|
| 116 |
|
| 117 |
with gr.Tab("๐ Solve Simultaneous Equations"):
|
| 118 |
sim_image_input = gr.Image(type="pil", label="๐ท Upload Image with Simultaneous Equations")
|
|
@@ -128,7 +138,7 @@ with gr.Blocks() as demo:
|
|
| 128 |
|
| 129 |
raw_latex = raw_latex.replace("\n", " ")
|
| 130 |
equations = re.split(r'[;,]', raw_latex)
|
| 131 |
-
cleaned_equations = [
|
| 132 |
|
| 133 |
sympy_eqs = []
|
| 134 |
symbols = set()
|
|
|
|
| 10 |
def preprocess_handwritten_image(pil_img):
|
| 11 |
return pil_img.convert('RGB')
|
| 12 |
|
| 13 |
+
# Tab 1 cleaning (KEEP UNCHANGED)
|
| 14 |
def clean_latex(latex):
|
| 15 |
latex = latex.replace('\\ ', '')
|
| 16 |
latex = latex.replace('\\\\', '\\')
|
|
|
|
| 50 |
latex = latex.replace('e', '2.7183')
|
| 51 |
return latex
|
| 52 |
|
| 53 |
+
# โ
Tab 2 specific cleaner (gentle)
|
| 54 |
+
def clean_latex2(latex):
|
| 55 |
+
latex = latex.replace('\\ ', '')
|
| 56 |
+
latex = latex.replace('\\\\', '\\')
|
| 57 |
+
latex = re.sub(r'\\[ \t\n\r\f\v]*', '', latex)
|
| 58 |
+
latex = re.sub(r'\\([+\-=])', r'\1', latex)
|
| 59 |
+
|
| 60 |
+
replacements = {
|
| 61 |
+
r'\\chi': 'x', r'\\xi': 'x',
|
| 62 |
+
r'\\gamma': 'y', r'\\beta': 'b',
|
| 63 |
+
r'\\alpha': 'x', r'\\vartheta': '3',
|
| 64 |
+
r'\\cdot': '*', r'โ': '-', r'โ': '-',
|
| 65 |
+
r'\\mathcal': '', r'mathcal': '',
|
| 66 |
+
}
|
| 67 |
+
for wrong, right in replacements.items():
|
| 68 |
+
latex = re.sub(wrong, right, latex)
|
| 69 |
+
|
| 70 |
+
latex = latex.replace('{', '').replace('}', '')
|
| 71 |
+
latex = latex.strip().rstrip(',')
|
| 72 |
+
return latex
|
| 73 |
+
|
| 74 |
def solve_polynomial(image):
|
| 75 |
try:
|
| 76 |
img = preprocess_handwritten_image(image)
|
|
|
|
| 115 |
result, cleaned, prompt = solve_polynomial(img)
|
| 116 |
return result, cleaned, prompt
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
with gr.Blocks() as demo:
|
| 119 |
with gr.Tab("๐ผ๏ธ Parse from Image"):
|
| 120 |
image_input = gr.Image(type="pil", label="๐ท Upload Image of Polynomial")
|
|
|
|
| 123 |
output_box = gr.Markdown(label="๐ Step-by-step Solution")
|
| 124 |
submit_btn = gr.Button("๐ Solve")
|
| 125 |
submit_btn.click(fn=wrapped_solver, inputs=[image_input], outputs=[output_box, hidden_latex, explanation_prompt])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
with gr.Tab("๐ Solve Simultaneous Equations"):
|
| 128 |
sim_image_input = gr.Image(type="pil", label="๐ท Upload Image with Simultaneous Equations")
|
|
|
|
| 138 |
|
| 139 |
raw_latex = raw_latex.replace("\n", " ")
|
| 140 |
equations = re.split(r'[;,]', raw_latex)
|
| 141 |
+
cleaned_equations = [clean_latex2(eq) for eq in equations if '=' in eq]
|
| 142 |
|
| 143 |
sympy_eqs = []
|
| 144 |
symbols = set()
|