NihalGazi commited on
Commit
ae6f53a
·
verified ·
1 Parent(s): a8b70d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -1,12 +1,12 @@
1
  import gradio as gr
2
- import genai # Assuming this is the correct library for Gemini API
3
- import sympy as sp
4
 
5
- # Set up the Gemini model
6
- model = genai.GenerativeModel("gemini-1.5-flash")
7
 
8
- # Function for algebraic simplification
9
  def algebra_simplification(expression):
 
10
  try:
11
  expr = sp.sympify(expression)
12
  simplified_expr = sp.simplify(expr)
@@ -14,8 +14,8 @@ def algebra_simplification(expression):
14
  except Exception as e:
15
  return f"Error: {str(e)}"
16
 
17
- # Function for solving algebraic equations
18
  def algebra_solve(equation):
 
19
  try:
20
  eq = sp.sympify(equation)
21
  variables = list(eq.free_symbols)
@@ -24,32 +24,42 @@ def algebra_solve(equation):
24
  except Exception as e:
25
  return f"Error: {str(e)}"
26
 
27
- # Function to handle the chat with Gemini
28
- def process_math_problem(math_problem):
29
- chat = model.start_chat(
30
- history=[
31
- {"role": "user", "parts": "Hello"},
32
- {"role": "model", "parts": "Great to meet you. What would you like to know?"},
33
- ]
34
- )
35
 
36
- # Send the math problem to Gemini
37
- response = chat.send_message(math_problem)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- # Return Gemini's response
40
  return response.text
41
 
42
- # Gradio interface
 
 
 
 
43
  with gr.Blocks() as demo:
44
  gr.Markdown("# Gemini Algebra Solver")
45
 
46
- # Single input field for math problems
47
  input_problem = gr.Textbox(label="Enter a Math Problem", placeholder="e.g., Simplify (x^2 - 4)/(x - 2) or Solve x^2 - 4 = 0")
48
  output_solution = gr.Textbox(label="Solution")
49
 
50
- # Button to trigger the solution process
51
  solve_button = gr.Button("Solve")
52
  solve_button.click(fn=process_math_problem, inputs=input_problem, outputs=output_solution)
53
 
54
- # Launch the Gradio interface
55
  demo.launch()
 
1
  import gradio as gr
2
+ import genai # Assuming you are using a `genai` library for Gemini
 
3
 
4
+ # Set up the Gemini generative model using your API key
5
+ model = genai.GenerativeModel("gemini-1.5-flash", api_key="AIzaSyCjAahw7YvVFWSYHWRaGwZ9cA9emobfNok")
6
 
7
+ # Define the functions Gemini will call
8
  def algebra_simplification(expression):
9
+ import sympy as sp
10
  try:
11
  expr = sp.sympify(expression)
12
  simplified_expr = sp.simplify(expr)
 
14
  except Exception as e:
15
  return f"Error: {str(e)}"
16
 
 
17
  def algebra_solve(equation):
18
+ import sympy as sp
19
  try:
20
  eq = sp.sympify(equation)
21
  variables = list(eq.free_symbols)
 
24
  except Exception as e:
25
  return f"Error: {str(e)}"
26
 
27
+ # Chat model and function calling logic
28
+ def gemini_chat(math_problem):
29
+ chat = model.start_chat(history=[])
 
 
 
 
 
30
 
31
+ # Gemini model should figure out whether to call the appropriate function
32
+ response = chat.send_message(math_problem, functions=[
33
+ {
34
+ "name": "algebra_simplification",
35
+ "parameters": {
36
+ "expression": math_problem
37
+ },
38
+ "fn": algebra_simplification
39
+ },
40
+ {
41
+ "name": "algebra_solve",
42
+ "parameters": {
43
+ "equation": math_problem
44
+ },
45
+ "fn": algebra_solve
46
+ }
47
+ ], function_call="auto") # Set function_call to 'auto'
48
 
 
49
  return response.text
50
 
51
+ # Gradio Interface
52
+ def process_math_problem(math_problem):
53
+ # Use Gemini's auto function calling to process the math problem
54
+ return gemini_chat(math_problem)
55
+
56
  with gr.Blocks() as demo:
57
  gr.Markdown("# Gemini Algebra Solver")
58
 
 
59
  input_problem = gr.Textbox(label="Enter a Math Problem", placeholder="e.g., Simplify (x^2 - 4)/(x - 2) or Solve x^2 - 4 = 0")
60
  output_solution = gr.Textbox(label="Solution")
61
 
 
62
  solve_button = gr.Button("Solve")
63
  solve_button.click(fn=process_math_problem, inputs=input_problem, outputs=output_solution)
64
 
 
65
  demo.launch()