Update app.py
Browse files
app.py
CHANGED
|
@@ -36,7 +36,6 @@ def clean_latex(latex):
|
|
| 36 |
|
| 37 |
# Fallback to external LLM microservice
|
| 38 |
def request_llm_fallback(bad_latex, llm_url):
|
| 39 |
-
# Clean known junk before sending to LLM
|
| 40 |
pre_cleaned = re.sub(
|
| 41 |
r'(\\!?pm|\\not=?|\\!|\\L|\\perp|\\bar|\\Sigma|\\boldmath|G|L(?=\^))', '', bad_latex
|
| 42 |
)
|
|
@@ -48,6 +47,17 @@ def request_llm_fallback(bad_latex, llm_url):
|
|
| 48 |
print(f"LLM fallback failed: {e}")
|
| 49 |
return pre_cleaned
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# Main function
|
| 52 |
def solve_polynomial(image, llm_url):
|
| 53 |
try:
|
|
@@ -55,7 +65,7 @@ def solve_polynomial(image, llm_url):
|
|
| 55 |
latex_result = model(img)
|
| 56 |
|
| 57 |
if not latex_result or len(latex_result.strip()) < 2:
|
| 58 |
-
return "β Could not extract valid LaTeX from image."
|
| 59 |
|
| 60 |
cleaned_latex = clean_latex(latex_result)
|
| 61 |
|
|
@@ -69,7 +79,6 @@ def solve_polynomial(image, llm_url):
|
|
| 69 |
if not lhs_minus_rhs.is_polynomial():
|
| 70 |
raise ValueError("Expression is not a polynomial.")
|
| 71 |
|
| 72 |
-
# Fallback if junk symbols found
|
| 73 |
junk_symbols = {"pm", "cdot", "mathrm", "boldmath", "bar", "L"}
|
| 74 |
symbols = {str(s) for s in expr.free_symbols}
|
| 75 |
if symbols & junk_symbols:
|
|
@@ -96,8 +105,9 @@ def solve_polynomial(image, llm_url):
|
|
| 96 |
|
| 97 |
if expr is None:
|
| 98 |
return (
|
| 99 |
-
"β Could not parse expression even after fallback:\n\n"
|
| 100 |
-
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
output = (
|
|
@@ -122,22 +132,31 @@ def solve_polynomial(image, llm_url):
|
|
| 122 |
simplified = sp.simplify(expr)
|
| 123 |
output += f"## β Simplified Expression\n$$ {sp.latex(simplified)} $$"
|
| 124 |
|
| 125 |
-
return output
|
| 126 |
|
| 127 |
except Exception as e:
|
| 128 |
-
return f"β **Error**: {str(e)}"
|
| 129 |
|
| 130 |
-
#
|
| 131 |
with gr.Blocks() as demo:
|
| 132 |
llm_url = gr.Textbox(label="π Enter LLM Microservice URL (from Colab)", placeholder="https://xxxx.ngrok-free.app")
|
| 133 |
image_input = gr.Image(type="pil", label="π· Upload Image of Polynomial")
|
| 134 |
output_box = gr.Markdown(label="π Step-by-step Solution")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
submit_btn = gr.Button("π Solve")
|
| 137 |
-
submit_btn.click(fn=
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
demo.title = "π§ Polynomial Solver from Image"
|
| 140 |
-
demo.description = "Upload
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
demo.launch()
|
|
|
|
| 36 |
|
| 37 |
# Fallback to external LLM microservice
|
| 38 |
def request_llm_fallback(bad_latex, llm_url):
|
|
|
|
| 39 |
pre_cleaned = re.sub(
|
| 40 |
r'(\\!?pm|\\not=?|\\!|\\L|\\perp|\\bar|\\Sigma|\\boldmath|G|L(?=\^))', '', bad_latex
|
| 41 |
)
|
|
|
|
| 47 |
print(f"LLM fallback failed: {e}")
|
| 48 |
return pre_cleaned
|
| 49 |
|
| 50 |
+
# NEW: Request human-style explanation
|
| 51 |
+
def request_llm_explanation(latex, llm_url):
|
| 52 |
+
try:
|
| 53 |
+
response = requests.post(f"{llm_url}/explain", json={"prompt": latex})
|
| 54 |
+
if response.status_code == 200:
|
| 55 |
+
return response.json().get("explanation", "β No explanation returned.")
|
| 56 |
+
else:
|
| 57 |
+
return f"β LLM explanation failed: {response.status_code}"
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"β LLM explanation error: {e}"
|
| 60 |
+
|
| 61 |
# Main function
|
| 62 |
def solve_polynomial(image, llm_url):
|
| 63 |
try:
|
|
|
|
| 65 |
latex_result = model(img)
|
| 66 |
|
| 67 |
if not latex_result or len(latex_result.strip()) < 2:
|
| 68 |
+
return "β Could not extract valid LaTeX from image.", "", ""
|
| 69 |
|
| 70 |
cleaned_latex = clean_latex(latex_result)
|
| 71 |
|
|
|
|
| 79 |
if not lhs_minus_rhs.is_polynomial():
|
| 80 |
raise ValueError("Expression is not a polynomial.")
|
| 81 |
|
|
|
|
| 82 |
junk_symbols = {"pm", "cdot", "mathrm", "boldmath", "bar", "L"}
|
| 83 |
symbols = {str(s) for s in expr.free_symbols}
|
| 84 |
if symbols & junk_symbols:
|
|
|
|
| 105 |
|
| 106 |
if expr is None:
|
| 107 |
return (
|
| 108 |
+
f"β Could not parse expression even after fallback:\n\n```latex\n{cleaned_latex}\n```",
|
| 109 |
+
cleaned_latex,
|
| 110 |
+
""
|
| 111 |
)
|
| 112 |
|
| 113 |
output = (
|
|
|
|
| 132 |
simplified = sp.simplify(expr)
|
| 133 |
output += f"## β Simplified Expression\n$$ {sp.latex(simplified)} $$"
|
| 134 |
|
| 135 |
+
return output, cleaned_latex, ""
|
| 136 |
|
| 137 |
except Exception as e:
|
| 138 |
+
return f"β **Error**: {str(e)}", "", ""
|
| 139 |
|
| 140 |
+
# UI
|
| 141 |
with gr.Blocks() as demo:
|
| 142 |
llm_url = gr.Textbox(label="π Enter LLM Microservice URL (from Colab)", placeholder="https://xxxx.ngrok-free.app")
|
| 143 |
image_input = gr.Image(type="pil", label="π· Upload Image of Polynomial")
|
| 144 |
output_box = gr.Markdown(label="π Step-by-step Solution")
|
| 145 |
+
explain_box = gr.Markdown(label="π£οΈ Human-style Explanation")
|
| 146 |
+
hidden_latex = gr.Textbox(visible=False)
|
| 147 |
+
|
| 148 |
+
def wrapped_solver(img, url):
|
| 149 |
+
result, cleaned, _ = solve_polynomial(img, url)
|
| 150 |
+
return result, cleaned
|
| 151 |
|
| 152 |
submit_btn = gr.Button("π Solve")
|
| 153 |
+
submit_btn.click(fn=wrapped_solver, inputs=[image_input, llm_url], outputs=[output_box, hidden_latex])
|
| 154 |
+
|
| 155 |
+
explain_btn = gr.Button("π§ Explain Human-Solution")
|
| 156 |
+
explain_btn.click(fn=request_llm_explanation, inputs=[hidden_latex, llm_url], outputs=explain_box)
|
| 157 |
|
| 158 |
demo.title = "π§ Polynomial Solver from Image"
|
| 159 |
+
demo.description = "Upload a polynomial image (typed or handwritten). View symbolic steps and human-style explanation."
|
| 160 |
|
| 161 |
if __name__ == "__main__":
|
| 162 |
demo.launch()
|