import gradio as gr import requests import sympy as sp # Function for algebraic simplification def algebra_simplification(expression): try: expr = sp.sympify(expression) simplified_expr = sp.simplify(expr) return str(simplified_expr) except Exception as e: return f"Error: {str(e)}" # Function for solving algebraic equations def algebra_solve(equation): try: eq = sp.sympify(equation) variables = list(eq.free_symbols) solutions = sp.solve(eq, *variables) return str(solutions) except Exception as e: return f"Error: {str(e)}" # Gemini API function calling def gemini_api_call(math_problem): url = "https://api.gemini.ai/v1/function_call" headers = { "Authorization": "Bearer YOUR_API_KEY", "Content-Type": "application/json", } # Define function descriptors for Gemini's function calling system functions = [ { "name": "algebra_simplification", "description": "Simplifies an algebraic expression", "parameters": { "type": "object", "properties": { "expression": { "type": "string", "description": "The algebraic expression to simplify" } }, "required": ["expression"] } }, { "name": "algebra_solve", "description": "Solves an algebraic equation", "parameters": { "type": "object", "properties": { "equation": { "type": "string", "description": "The algebraic equation to solve" } }, "required": ["equation"] } } ] # Payload for the Gemini API request data = { "model": "gemini-1.5-flash", "prompt": math_problem, "functions": functions, "function_call": "auto" # Automatically choose the function to call } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: result = response.json() return result['output'] # Adjust according to API response structure else: return f"Error: {response.status_code} - {response.text}" # Gradio interface def process_math_problem(math_problem): # Call Gemini API and return the result return gemini_api_call(math_problem) with gr.Blocks() as demo: gr.Markdown("# Gemini Algebra Solver") input_problem = gr.Textbox(label="Enter a Math Problem", placeholder="e.g., Simplify (x^2 - 4)/(x - 2) or Solve x^2 - 4 = 0") output_solution = gr.Textbox(label="Solution") solve_button = gr.Button("Solve") solve_button.click(fn=process_math_problem, inputs=input_problem, outputs=output_solution) demo.launch()