omprasadmahapatra/Ai_Math_Tutor
0
1import gradio as gr2import sympy as sp3from transformers import pipeline4 5# AI Model for Math Explanation6math_ai = pipeline("text-generation", model="gpt-3.5-turbo") # Use OpenAI API or similar7 8def solve_math_problem(problem):9 try:10 expr = sp.sympify(problem) # Convert input to a math expression11 solution = sp.simplify(expr) # Solve it12 explanation = math_ai(f"Explain this math problem step-by-step: {problem} = {solution}")13 return str(solution), explanation[0]['generated_text']14 except Exception as e:15 return "Invalid Input", str(e)16 17# Web UI with Gradio18with gr.Blocks() as app:19 gr.Markdown("# ๐ค AI Math Tutor ๐")20 problem = gr.Textbox(label="Enter a Math Problem (e.g., 2x + 5 = 15)")21 solution = gr.Textbox(label="Solution")22 explanation = gr.Textbox(label="AI Explanation")23 submit = gr.Button("Solve")24 25 submit.click(solve_math_problem, inputs=[problem], outputs=[solution, explanation])26 27app.launch()28 