Update app.py
Browse files
app.py
CHANGED
|
@@ -82,14 +82,13 @@ def solve_polynomial(image, llm_url):
|
|
| 82 |
expr = expr.subs(sp.pi, sp.Float(3.1416)).subs(sp.E, sp.Float(2.7183))
|
| 83 |
if not isinstance(expr, sp.Equality):
|
| 84 |
raise ValueError("Expression is not an equation.")
|
| 85 |
-
|
| 86 |
-
if not
|
| 87 |
-
raise ValueError("
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
except Exception:
|
| 93 |
fixed_latex = request_llm_fallback(cleaned_latex, llm_url)
|
| 94 |
cleaned_latex = clean_latex(fixed_latex)
|
| 95 |
try:
|
|
@@ -97,51 +96,76 @@ def solve_polynomial(image, llm_url):
|
|
| 97 |
expr = expr.subs(sp.pi, sp.Float(3.1416)).subs(sp.E, sp.Float(2.7183))
|
| 98 |
if not isinstance(expr, sp.Equality):
|
| 99 |
raise ValueError("Expression is not an equation.")
|
| 100 |
-
|
| 101 |
-
if not
|
| 102 |
-
raise ValueError("
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
except Exception as e:
|
| 140 |
-
return f"โ
|
| 141 |
|
| 142 |
def wrapped_solver(img, url):
|
| 143 |
-
result, cleaned,
|
| 144 |
-
return result, cleaned
|
| 145 |
|
| 146 |
def solve_from_coeffs(degree, coeff_str):
|
| 147 |
try:
|
|
@@ -189,12 +213,13 @@ with gr.Blocks() as demo:
|
|
| 189 |
llm_url = gr.Textbox(label="๐ Enter LLM Microservice URL (from Colab)", placeholder="https://xxxx.ngrok-free.app")
|
| 190 |
image_input = gr.Image(type="pil", label="๐ท Upload Image of Polynomial")
|
| 191 |
hidden_latex = gr.Textbox(visible=False)
|
|
|
|
| 192 |
output_box = gr.Markdown(label="๐ Step-by-step Solution")
|
| 193 |
submit_btn = gr.Button("๐ Solve")
|
| 194 |
-
submit_btn.click(fn=wrapped_solver, inputs=[image_input, llm_url], outputs=[output_box, hidden_latex])
|
| 195 |
explain_box = gr.Markdown(label="๐ฃ๏ธ Human-style Explanation")
|
| 196 |
explain_btn = gr.Button("๐ง Explain Human-Solution")
|
| 197 |
-
explain_btn.click(fn=request_llm_explanation, inputs=[
|
| 198 |
|
| 199 |
with gr.Tab("๐งฎ Solve by Coefficients"):
|
| 200 |
degree_input = gr.Number(label="Enter Degree of Polynomial (e.g. 3)")
|
|
|
|
| 82 |
expr = expr.subs(sp.pi, sp.Float(3.1416)).subs(sp.E, sp.Float(2.7183))
|
| 83 |
if not isinstance(expr, sp.Equality):
|
| 84 |
raise ValueError("Expression is not an equation.")
|
| 85 |
+
lhs = expr.lhs - expr.rhs
|
| 86 |
+
if not lhs.is_polynomial():
|
| 87 |
+
raise ValueError("Not a polynomial")
|
| 88 |
+
junk = {"pm", "cdot", "mathrm", "boldmath", "bar", "L"}
|
| 89 |
+
if {str(s) for s in expr.free_symbols} & junk:
|
| 90 |
+
raise ValueError("Junk in symbols")
|
| 91 |
+
except:
|
|
|
|
| 92 |
fixed_latex = request_llm_fallback(cleaned_latex, llm_url)
|
| 93 |
cleaned_latex = clean_latex(fixed_latex)
|
| 94 |
try:
|
|
|
|
| 96 |
expr = expr.subs(sp.pi, sp.Float(3.1416)).subs(sp.E, sp.Float(2.7183))
|
| 97 |
if not isinstance(expr, sp.Equality):
|
| 98 |
raise ValueError("Expression is not an equation.")
|
| 99 |
+
lhs = expr.lhs - expr.rhs
|
| 100 |
+
if not lhs.is_polynomial():
|
| 101 |
+
raise ValueError("Not a polynomial")
|
| 102 |
+
except:
|
| 103 |
+
return f"โ Could not parse expression after fallback:\n\n```latex\n{cleaned_latex}\n```", cleaned_latex, ""
|
| 104 |
+
|
| 105 |
+
output = f"## ๐ Extracted LaTeX\n```latex\n{latex_result}\n```\n"
|
| 106 |
+
output += f"---\n## ๐งน Cleaned LaTeX\n```latex\n{cleaned_latex}\n```\n"
|
| 107 |
+
output += f"---\n## ๐ง Parsed Expression\n$$ {sp.latex(expr)} $$\n"
|
| 108 |
+
|
| 109 |
+
lhs = expr.lhs - expr.rhs
|
| 110 |
+
factor = sp.factor(lhs)
|
| 111 |
+
output += f"---\n## โ๏ธ Standard Form\n$$ {sp.latex(lhs)} = 0 $$\n"
|
| 112 |
+
output += f"---\n## ๐งฉ Factorized\n$$ {sp.latex(factor)} = 0 $$\n"
|
| 113 |
+
|
| 114 |
+
x = next(iter(lhs.free_symbols))
|
| 115 |
+
roots = sp.solve(lhs, x)
|
| 116 |
+
output += "## โ
Roots\n"
|
| 117 |
+
output += "$$\n\\begin{aligned}\n"
|
| 118 |
+
root_strs = []
|
| 119 |
+
for i, r in enumerate(roots, 1):
|
| 120 |
+
root_val = sp.N(r, 6)
|
| 121 |
+
output += f"\\text{{Root {i}}}:\\quad x &\\approx {sp.latex(root_val)} \\\\\n"
|
| 122 |
+
root_strs.append(str(root_val))
|
| 123 |
+
output += "\\end{aligned}\n$$\n"
|
| 124 |
+
|
| 125 |
+
# ๐ Build structured LLM prompt
|
| 126 |
+
prompt = ""
|
| 127 |
+
try:
|
| 128 |
+
coeffs = sp.Poly(lhs, x).all_coeffs()
|
| 129 |
+
if len(coeffs) == 3:
|
| 130 |
+
a, b, c = [float(k) for k in coeffs]
|
| 131 |
+
prod = a * c
|
| 132 |
+
found = False
|
| 133 |
+
for m in range(-100, 101):
|
| 134 |
+
if m == 0 or prod % m != 0:
|
| 135 |
+
continue
|
| 136 |
+
n = prod / m
|
| 137 |
+
if m + n == b:
|
| 138 |
+
found = True
|
| 139 |
+
prompt = (
|
| 140 |
+
f"Equation: {lhs} = 0\n"
|
| 141 |
+
f"Factor: {factor} = 0\n"
|
| 142 |
+
f"Roots: {root_strs}\n\n"
|
| 143 |
+
f"Middle-term factorization is applicable: "
|
| 144 |
+
f"a = {a}, b = {b}, c = {c}, a*c = {prod}. "
|
| 145 |
+
f"{m} and {n} satisfy m*n = ac and m+n = b.\n"
|
| 146 |
+
f"Explain this step-by-step in human style."
|
| 147 |
+
)
|
| 148 |
+
break
|
| 149 |
+
if not found:
|
| 150 |
+
raise Exception("No middle term")
|
| 151 |
+
else:
|
| 152 |
+
raise Exception("Not quadratic")
|
| 153 |
+
except:
|
| 154 |
+
prompt = (
|
| 155 |
+
f"Equation: {lhs} = 0\n"
|
| 156 |
+
f"Factor: {factor} = 0\n"
|
| 157 |
+
f"Roots: {root_strs}\n\n"
|
| 158 |
+
f"Middle-term factorization is not suitable. "
|
| 159 |
+
f"Explain using quadratic formula (Sreedhar Acharya) with step-by-step clarity."
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
return output, cleaned_latex, prompt
|
| 163 |
except Exception as e:
|
| 164 |
+
return f"โ Error: {str(e)}", "", ""
|
| 165 |
|
| 166 |
def wrapped_solver(img, url):
|
| 167 |
+
result, cleaned, prompt = solve_polynomial(img, url)
|
| 168 |
+
return result, cleaned, prompt
|
| 169 |
|
| 170 |
def solve_from_coeffs(degree, coeff_str):
|
| 171 |
try:
|
|
|
|
| 213 |
llm_url = gr.Textbox(label="๐ Enter LLM Microservice URL (from Colab)", placeholder="https://xxxx.ngrok-free.app")
|
| 214 |
image_input = gr.Image(type="pil", label="๐ท Upload Image of Polynomial")
|
| 215 |
hidden_latex = gr.Textbox(visible=False)
|
| 216 |
+
explanation_prompt = gr.Textbox(visible=False)
|
| 217 |
output_box = gr.Markdown(label="๐ Step-by-step Solution")
|
| 218 |
submit_btn = gr.Button("๐ Solve")
|
| 219 |
+
submit_btn.click(fn=wrapped_solver, inputs=[image_input, llm_url], outputs=[output_box, hidden_latex, explanation_prompt])
|
| 220 |
explain_box = gr.Markdown(label="๐ฃ๏ธ Human-style Explanation")
|
| 221 |
explain_btn = gr.Button("๐ง Explain Human-Solution")
|
| 222 |
+
explain_btn.click(fn=request_llm_explanation, inputs=[explanation_prompt, llm_url], outputs=explain_box)
|
| 223 |
|
| 224 |
with gr.Tab("๐งฎ Solve by Coefficients"):
|
| 225 |
degree_input = gr.Number(label="Enter Degree of Polynomial (e.g. 3)")
|