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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -59
app.py CHANGED
@@ -1,7 +1,10 @@
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:
@@ -21,74 +24,32 @@ def algebra_solve(equation):
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()
 
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:
 
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()