import gradio as gr from solver import ( generate_polynomial_template, solve_polynomial, solve_linear_system ) from llm_utils import explain_with_llm with gr.Blocks() as demo: gr.Markdown("# 🔢 Math Solver Suite") gr.Markdown("Solve polynomials and systems of linear equations step-by-step with visualizations and explanations.") with gr.Tabs(): with gr.TabItem("📐 Polynomial Solver"): with gr.Row(): degree_slider = gr.Slider(1, 8, value=3, step=1, label="Degree of Polynomial") template_display = gr.Textbox(label="Polynomial Template (auto-updated)", interactive=False) coeff_input = gr.Textbox( label="Enter Coefficients (space-separated, supports pi, e, sqrt(2), I)", placeholder="e.g. 1 -3 sqrt(2) -pi" ) llm_url = gr.Textbox(label="LLM Microservice URL (optional)", placeholder="https://your-llm-url.ngrok.app") steps_md = gr.Markdown() plot_output = gr.Plot() error_box = gr.Textbox(visible=False) poly_solution_txt = gr.Textbox(visible=False) with gr.Row(): solve_button = gr.Button("Plot Polynomial", variant="primary") explain_button = gr.Button("Explain Polynomial with LLM") degree_slider.change( fn=generate_polynomial_template, inputs=degree_slider, outputs=template_display ) solve_button.click( fn=solve_polynomial, inputs=[degree_slider, coeff_input], outputs=[steps_md, plot_output, error_box, poly_solution_txt] ) explain_button.click( fn=lambda sol, url: explain_with_llm(sol, "polynomial", url), inputs=[poly_solution_txt, llm_url], outputs=steps_md ) with gr.TabItem("🧮 Linear Equation Solver"): eq1_input = gr.Textbox(label="Equation 1 (in x and y)", placeholder="e.g. 2*x + 3*y - 6") eq2_input = gr.Textbox(label="Equation 2 (in x and y)", placeholder="e.g. -x + y - 2") llm_url_linear = gr.Textbox(label="LLM Microservice URL (optional)", placeholder="https://your-llm-url.ngrok.app") sys_steps = gr.Markdown() sys_plot = gr.Plot() sys_solution_txt = gr.Textbox(visible=False) with gr.Row(): solve_sys_button = gr.Button("Solve Linear System", variant="primary") explain_sys_button = gr.Button("Explain Linear System with LLM") solve_sys_button.click( fn=solve_linear_system, inputs=[eq1_input, eq2_input], outputs=[sys_steps, sys_plot, sys_solution_txt] ) explain_sys_button.click( fn=lambda sol, url: explain_with_llm(sol, "linear", url), inputs=[sys_solution_txt, llm_url_linear], outputs=sys_steps ) demo.launch()