Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,94 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import sympy as sp
|
3 |
|
4 |
# Function for algebraic simplification
|
5 |
def algebra_simplification(expression):
|
6 |
try:
|
7 |
-
# Ensure that the input expression is sympified correctly
|
8 |
expr = sp.sympify(expression)
|
9 |
-
|
10 |
-
|
11 |
-
if isinstance(expr, (sp.Basic, sp.Expr)):
|
12 |
-
simplified_expr = sp.simplify(expr) # Simplify the expression
|
13 |
-
return str(simplified_expr)
|
14 |
-
else:
|
15 |
-
return "Input is not a valid algebraic expression."
|
16 |
except Exception as e:
|
17 |
return f"Error: {str(e)}"
|
18 |
|
19 |
# Function for solving algebraic equations
|
20 |
def algebra_solve(equation):
|
21 |
try:
|
22 |
-
# Ensure that the input equation is sympified correctly
|
23 |
eq = sp.sympify(equation)
|
24 |
-
|
25 |
-
# Extract variables from the equation
|
26 |
variables = list(eq.free_symbols)
|
27 |
-
|
28 |
-
# Solve the equation
|
29 |
solutions = sp.solve(eq, *variables)
|
30 |
return str(solutions)
|
31 |
except Exception as e:
|
32 |
return f"Error: {str(e)}"
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
# Gradio interface
|
|
|
|
|
|
|
|
|
35 |
with gr.Blocks() as demo:
|
36 |
-
gr.Markdown("# Algebra
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
simplify_button = gr.Button("Simplify")
|
42 |
-
simplify_button.click(fn=algebra_simplification, inputs=input_expression, outputs=output_simplification)
|
43 |
-
|
44 |
-
with gr.Tab("Solve"):
|
45 |
-
input_equation = gr.Textbox(label="Enter an Equation", placeholder="e.g., x^2 - 4 = 0")
|
46 |
-
output_solution = gr.Textbox(label="Solution")
|
47 |
-
solve_button = gr.Button("Solve")
|
48 |
-
solve_button.click(fn=algebra_solve, inputs=input_equation, outputs=output_solution)
|
49 |
-
|
50 |
-
# Launch the Gradio interface
|
51 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
import sympy as sp
|
4 |
|
5 |
# Function for algebraic simplification
|
6 |
def algebra_simplification(expression):
|
7 |
try:
|
|
|
8 |
expr = sp.sympify(expression)
|
9 |
+
simplified_expr = sp.simplify(expr)
|
10 |
+
return str(simplified_expr)
|
|
|
|
|
|
|
|
|
|
|
11 |
except Exception as e:
|
12 |
return f"Error: {str(e)}"
|
13 |
|
14 |
# Function for solving algebraic equations
|
15 |
def algebra_solve(equation):
|
16 |
try:
|
|
|
17 |
eq = sp.sympify(equation)
|
|
|
|
|
18 |
variables = list(eq.free_symbols)
|
|
|
|
|
19 |
solutions = sp.solve(eq, *variables)
|
20 |
return str(solutions)
|
21 |
except Exception as e:
|
22 |
return f"Error: {str(e)}"
|
23 |
|
24 |
+
# Gemini API function calling
|
25 |
+
def gemini_api_call(math_problem):
|
26 |
+
url = "https://api.gemini.ai/v1/function_call"
|
27 |
+
headers = {
|
28 |
+
"Authorization": "Bearer YOUR_API_KEY",
|
29 |
+
"Content-Type": "application/json",
|
30 |
+
}
|
31 |
+
|
32 |
+
# Define function descriptors for Gemini's function calling system
|
33 |
+
functions = [
|
34 |
+
{
|
35 |
+
"name": "algebra_simplification",
|
36 |
+
"description": "Simplifies an algebraic expression",
|
37 |
+
"parameters": {
|
38 |
+
"type": "object",
|
39 |
+
"properties": {
|
40 |
+
"expression": {
|
41 |
+
"type": "string",
|
42 |
+
"description": "The algebraic expression to simplify"
|
43 |
+
}
|
44 |
+
},
|
45 |
+
"required": ["expression"]
|
46 |
+
}
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"name": "algebra_solve",
|
50 |
+
"description": "Solves an algebraic equation",
|
51 |
+
"parameters": {
|
52 |
+
"type": "object",
|
53 |
+
"properties": {
|
54 |
+
"equation": {
|
55 |
+
"type": "string",
|
56 |
+
"description": "The algebraic equation to solve"
|
57 |
+
}
|
58 |
+
},
|
59 |
+
"required": ["equation"]
|
60 |
+
}
|
61 |
+
}
|
62 |
+
]
|
63 |
+
|
64 |
+
# Payload for the Gemini API request
|
65 |
+
data = {
|
66 |
+
"model": "gemini-1.5-flash",
|
67 |
+
"prompt": math_problem,
|
68 |
+
"functions": functions,
|
69 |
+
"function_call": "auto" # Automatically choose the function to call
|
70 |
+
}
|
71 |
+
|
72 |
+
response = requests.post(url, headers=headers, json=data)
|
73 |
+
|
74 |
+
if response.status_code == 200:
|
75 |
+
result = response.json()
|
76 |
+
return result['output'] # Adjust according to API response structure
|
77 |
+
else:
|
78 |
+
return f"Error: {response.status_code} - {response.text}"
|
79 |
+
|
80 |
# Gradio interface
|
81 |
+
def process_math_problem(math_problem):
|
82 |
+
# Call Gemini API and return the result
|
83 |
+
return gemini_api_call(math_problem)
|
84 |
+
|
85 |
with gr.Blocks() as demo:
|
86 |
+
gr.Markdown("# Gemini Algebra Solver")
|
87 |
+
|
88 |
+
input_problem = gr.Textbox(label="Enter a Math Problem", placeholder="e.g., Simplify (x^2 - 4)/(x - 2) or Solve x^2 - 4 = 0")
|
89 |
+
output_solution = gr.Textbox(label="Solution")
|
90 |
|
91 |
+
solve_button = gr.Button("Solve")
|
92 |
+
solve_button.click(fn=process_math_problem, inputs=input_problem, outputs=output_solution)
|
93 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
demo.launch()
|