File size: 2,968 Bytes
39c84a2
a0862f6
39c84a2
 
 
 
 
3bf7b96
a0862f6
 
3bf7b96
 
 
 
 
 
 
 
 
 
39c84a2
 
 
a0862f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39c84a2
a0862f6
 
 
 
39c84a2
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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()