File size: 2,156 Bytes
39c84a2
ac7ce3b
 
 
39c84a2
ae6f53a
 
a8b70d3
ae6f53a
39c84a2
ae6f53a
39c84a2
3bf7b96
a0862f6
 
3bf7b96
 
 
 
ae6f53a
3bf7b96
 
 
 
 
39c84a2
 
 
ae6f53a
 
 
a0862f6
ae6f53a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0862f6
a8b70d3
a0862f6
ae6f53a
 
 
 
 
39c84a2
a0862f6
a8b70d3
a0862f6
 
39c84a2
a0862f6
 
 
39c84a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import gradio as gr
import google.generativeai as genai
from google.generativeai.types import HarmCategory, HarmBlockThreshold
import sympy as sp

# Set up the Gemini generative model using your API key
model = genai.GenerativeModel("gemini-1.5-flash", api_key="AIzaSyCjAahw7YvVFWSYHWRaGwZ9cA9emobfNok")

# Define the functions Gemini will call
def algebra_simplification(expression):
    import sympy as sp
    try:
        expr = sp.sympify(expression)
        simplified_expr = sp.simplify(expr)
        return str(simplified_expr)
    except Exception as e:
        return f"Error: {str(e)}"

def algebra_solve(equation):
    import sympy as sp
    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)}"

# Chat model and function calling logic
def gemini_chat(math_problem):
    chat = model.start_chat(history=[])
    
    # Gemini model should figure out whether to call the appropriate function
    response = chat.send_message(math_problem, functions=[
        {
            "name": "algebra_simplification",
            "parameters": {
                "expression": math_problem
            },
            "fn": algebra_simplification
        },
        {
            "name": "algebra_solve",
            "parameters": {
                "equation": math_problem
            },
            "fn": algebra_solve
        }
    ], function_call="auto")  # Set function_call to 'auto'
    
    return response.text

# Gradio Interface
def process_math_problem(math_problem):
    # Use Gemini's auto function calling to process the math problem
    return gemini_chat(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()