Update app.py
Browse files
app.py
CHANGED
|
@@ -50,7 +50,7 @@ def clean_latex(latex):
|
|
| 50 |
latex = latex.replace('e', '2.7183')
|
| 51 |
return latex
|
| 52 |
|
| 53 |
-
# β
Tab 2 specific cleaner
|
| 54 |
def clean_latex2(latex):
|
| 55 |
latex = latex.replace('\\ ', '')
|
| 56 |
latex = latex.replace('\\\\', '\\')
|
|
@@ -115,34 +115,21 @@ def wrapped_solver(img):
|
|
| 115 |
result, cleaned, prompt = solve_polynomial(img)
|
| 116 |
return result, cleaned, prompt
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
image_input = gr.Image(type="pil", label="π· Upload Image of Polynomial")
|
| 121 |
-
hidden_latex = gr.Textbox(visible=False)
|
| 122 |
-
explanation_prompt = gr.Textbox(visible=False)
|
| 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")
|
| 129 |
-
sim_output = gr.Markdown(label="π Solved System Output")
|
| 130 |
-
sim_button = gr.Button("π Solve System")
|
| 131 |
-
|
| 132 |
-
def solve_system_of_equations(image):
|
| 133 |
try:
|
| 134 |
img = preprocess_handwritten_image(image)
|
| 135 |
raw_latex = model(img)
|
| 136 |
if not raw_latex or len(raw_latex.strip()) < 3:
|
| 137 |
return "β Could not extract valid LaTeX."
|
| 138 |
|
| 139 |
-
#
|
| 140 |
if r"\begin{array" in raw_latex:
|
| 141 |
lines = re.findall(r'\\begin{array}{[^}]+}(.+?)\\end{array}', raw_latex, re.DOTALL)
|
| 142 |
if lines:
|
| 143 |
raw_latex = lines[0]
|
| 144 |
-
raw_latex = raw_latex.replace(r'\\', '\n')
|
| 145 |
-
raw_latex = re.sub(r'&', '', raw_latex)
|
| 146 |
|
| 147 |
raw_latex = raw_latex.replace("\n", " ")
|
| 148 |
equations = re.split(r'[;,]', raw_latex)
|
|
@@ -173,6 +160,20 @@ with gr.Blocks() as demo:
|
|
| 173 |
except Exception as e:
|
| 174 |
return f"β Error: {str(e)}"
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
sim_button.click(fn=solve_system_of_equations, inputs=[sim_image_input], outputs=[sim_output])
|
| 177 |
|
| 178 |
if __name__ == "__main__":
|
|
|
|
| 50 |
latex = latex.replace('e', '2.7183')
|
| 51 |
return latex
|
| 52 |
|
| 53 |
+
# β
Tab 2 specific cleaner
|
| 54 |
def clean_latex2(latex):
|
| 55 |
latex = latex.replace('\\ ', '')
|
| 56 |
latex = latex.replace('\\\\', '\\')
|
|
|
|
| 115 |
result, cleaned, prompt = solve_polynomial(img)
|
| 116 |
return result, cleaned, prompt
|
| 117 |
|
| 118 |
+
# β
FIXED: moved outside of gr.Blocks()
|
| 119 |
+
def solve_system_of_equations(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
try:
|
| 121 |
img = preprocess_handwritten_image(image)
|
| 122 |
raw_latex = model(img)
|
| 123 |
if not raw_latex or len(raw_latex.strip()) < 3:
|
| 124 |
return "β Could not extract valid LaTeX."
|
| 125 |
|
| 126 |
+
# Handle array environment
|
| 127 |
if r"\begin{array" in raw_latex:
|
| 128 |
lines = re.findall(r'\\begin{array}{[^}]+}(.+?)\\end{array}', raw_latex, re.DOTALL)
|
| 129 |
if lines:
|
| 130 |
raw_latex = lines[0]
|
| 131 |
+
raw_latex = raw_latex.replace(r'\\', '\n')
|
| 132 |
+
raw_latex = re.sub(r'&', '', raw_latex)
|
| 133 |
|
| 134 |
raw_latex = raw_latex.replace("\n", " ")
|
| 135 |
equations = re.split(r'[;,]', raw_latex)
|
|
|
|
| 160 |
except Exception as e:
|
| 161 |
return f"β Error: {str(e)}"
|
| 162 |
|
| 163 |
+
# === UI ===
|
| 164 |
+
with gr.Blocks() as demo:
|
| 165 |
+
with gr.Tab("πΌοΈ Parse from Image"):
|
| 166 |
+
image_input = gr.Image(type="pil", label="π· Upload Image of Polynomial")
|
| 167 |
+
hidden_latex = gr.Textbox(visible=False)
|
| 168 |
+
explanation_prompt = gr.Textbox(visible=False)
|
| 169 |
+
output_box = gr.Markdown(label="π Step-by-step Solution")
|
| 170 |
+
submit_btn = gr.Button("π Solve")
|
| 171 |
+
submit_btn.click(fn=wrapped_solver, inputs=[image_input], outputs=[output_box, hidden_latex, explanation_prompt])
|
| 172 |
+
|
| 173 |
+
with gr.Tab("π Solve Simultaneous Equations"):
|
| 174 |
+
sim_image_input = gr.Image(type="pil", label="π· Upload Image with Simultaneous Equations")
|
| 175 |
+
sim_output = gr.Markdown(label="π Solved System Output")
|
| 176 |
+
sim_button = gr.Button("π Solve System")
|
| 177 |
sim_button.click(fn=solve_system_of_equations, inputs=[sim_image_input], outputs=[sim_output])
|
| 178 |
|
| 179 |
if __name__ == "__main__":
|