import gradio as gr import matplotlib matplotlib.use('Agg') # Set Agg backend to avoid Qt issues import matplotlib.pyplot as plt from polynomial import polynomial_tab from linear import linear_tab from image import image_tab from llm_interface import explain_with_llm # ✅ LLM imports (added) import requests from llm_utils import build_prompt EXPLAIN_API_URL = "http:///explain" # 🔁 Replace with your actual Colab endpoint def explain_with_llm(latex_str): if not latex_str.strip(): return "⚠️ No LaTeX input provided." prompt = build_prompt(latex_str) try: response = requests.post(EXPLAIN_API_URL, json={"latex": prompt}) if response.status_code == 200: return response.json().get("explanation", "No explanation returned.") else: return f"❌ Error: {response.status_code} - {response.text}" except Exception as e: return f"❌ Exception: {e}" with gr.Blocks(title="Polynomial and Linear System Solver") as demo: # Create all tabs poly_components = polynomial_tab() linear_components = linear_tab() image_components = image_tab() # ✅ Attach LLM buttons and outputs (added) with gr.Tab("Polynomial Solver"): llm_button_poly = gr.Button("Explain with LLM") llm_output_poly = gr.Textbox(label="LLM Explanation", lines=4) llm_button_poly.click(fn=explain_with_llm, inputs=poly_components[0], outputs=llm_output_poly) with gr.Tab("Linear System Solver"): llm_button_lin = gr.Button("Explain with LLM") llm_output_lin = gr.Textbox(label="LLM Explanation", lines=4) llm_button_lin.click(fn=explain_with_llm, inputs=linear_components[0], outputs=llm_output_lin) with gr.Tab("Image Upload Solver"): llm_button_img = gr.Button("Explain with LLM") llm_output_img = gr.Textbox(label="LLM Explanation", lines=4) llm_button_img.click(fn=explain_with_llm, inputs=image_components[1], outputs=llm_output_img) if __name__ == "__main__": demo.launch() # Removed server_port=7860 to allow automatic port selection