Update app.py
Browse files
app.py
CHANGED
|
@@ -174,20 +174,47 @@ def wrapped_solver(img, url):
|
|
| 174 |
return result, cleaned
|
| 175 |
|
| 176 |
with gr.Blocks() as demo:
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
if __name__ == "__main__":
|
| 193 |
demo.launch()
|
|
|
|
| 174 |
return result, cleaned
|
| 175 |
|
| 176 |
with gr.Blocks() as demo:
|
| 177 |
+
with gr.Tab("๐ผ๏ธ Parse from Image"):
|
| 178 |
+
llm_url = gr.Textbox(label="๐ Enter LLM Microservice URL (from Colab)", placeholder="https://xxxx.ngrok-free.app")
|
| 179 |
+
image_input = gr.Image(type="pil", label="๐ท Upload Image of Polynomial")
|
| 180 |
+
hidden_latex = gr.Textbox(visible=False)
|
| 181 |
|
| 182 |
+
output_box = gr.Markdown(label="๐ Step-by-step Solution")
|
| 183 |
+
submit_btn = gr.Button("๐ Solve")
|
| 184 |
+
submit_btn.click(fn=wrapped_solver, inputs=[image_input, llm_url], outputs=[output_box, hidden_latex])
|
| 185 |
|
| 186 |
+
explain_box = gr.Markdown(label="๐ฃ๏ธ Human-style Explanation")
|
| 187 |
+
explain_btn = gr.Button("๐ง Explain Human-Solution")
|
| 188 |
+
explain_btn.click(fn=request_llm_explanation, inputs=[hidden_latex, llm_url], outputs=explain_box)
|
| 189 |
|
| 190 |
+
demo.title = "๐ง Polynomial Solver from Image"
|
| 191 |
+
demo.description = "Upload a polynomial image (typed or handwritten). View symbolic steps and human-style explanation."
|
| 192 |
+
|
| 193 |
+
with demo:
|
| 194 |
+
with gr.Tab("๐งฎ Solve by Coefficients"):
|
| 195 |
+
degree_input = gr.Number(label="Enter Degree of Polynomial (e.g. 3)")
|
| 196 |
+
coeffs_input = gr.Textbox(label="Enter Coefficients (comma-separated)", placeholder="e.g. 1, 2, 0, -4")
|
| 197 |
+
roots_output = gr.Markdown(label="โ
Roots")
|
| 198 |
+
|
| 199 |
+
def solve_from_coeffs(degree, coeff_str):
|
| 200 |
+
try:
|
| 201 |
+
coeffs = [float(c.strip()) for c in coeff_str.split(",")]
|
| 202 |
+
if len(coeffs) != int(degree) + 1:
|
| 203 |
+
return f"โ You entered {len(coeffs)} coefficients, but degree {degree} needs {int(degree)+1}."
|
| 204 |
+
|
| 205 |
+
x = sp.Symbol("x")
|
| 206 |
+
poly_expr = sum(coeffs[i] * x**(int(degree) - i) for i in range(len(coeffs)))
|
| 207 |
+
roots = sp.solve(poly_expr, x)
|
| 208 |
+
|
| 209 |
+
result = "### Roots:\n"
|
| 210 |
+
for i, r in enumerate(roots, 1):
|
| 211 |
+
result += f"- Root {i}: $${sp.latex(sp.N(r, 6))}$$\n"
|
| 212 |
+
return result
|
| 213 |
+
except Exception as e:
|
| 214 |
+
return f"โ Error: {str(e)}"
|
| 215 |
+
|
| 216 |
+
coeff_btn = gr.Button("๐ Solve")
|
| 217 |
+
coeff_btn.click(fn=solve_from_coeffs, inputs=[degree_input, coeffs_input], outputs=roots_output)
|
| 218 |
|
| 219 |
if __name__ == "__main__":
|
| 220 |
demo.launch()
|