shukdevdatta123 commited on
Commit
417a068
Β·
verified Β·
1 Parent(s): c08e01b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -54
app.py CHANGED
@@ -1,12 +1,16 @@
1
  import gradio as gr
 
2
  import base64
3
  import io
4
- import requests
5
-
6
- def solve_stem_problem(api_key, image, subject):
7
- if not api_key.strip():
8
- return "❌ Error: API key is required."
9
 
 
 
 
 
 
 
 
 
10
  detectives = {
11
  "math": "Algebra Ace",
12
  "physics": "Physics Phantom",
@@ -14,27 +18,29 @@ def solve_stem_problem(api_key, image, subject):
14
  "coding": "Code Cracker"
15
  }
16
  detective = detectives.get(subject, "Algebra Ace")
17
-
 
18
  try:
19
- # Convert image to base64 string
20
- buffered = io.BytesIO()
21
- image.save(buffered, format="PNG")
22
- encoded_image = base64.b64encode(buffered.getvalue()).decode('utf-8')
 
 
 
23
  image_url_data = f"data:image/png;base64,{encoded_image}"
24
  except Exception as e:
25
- return f"❌ Error encoding image: {str(e)}"
26
-
 
27
  try:
28
- headers = {
29
- "Authorization": f"Bearer {api_key}",
30
- "Content-Type": "application/json",
31
- "HTTP-Referer": "https://stem-sleuth.example.com",
32
- "X-Title": "STEM Sleuth"
33
- }
34
-
35
- payload = {
36
- "model": "google/gemini-2.0-flash-exp:free",
37
- "messages": [
38
  {
39
  "role": "user",
40
  "content": [
@@ -44,42 +50,45 @@ def solve_stem_problem(api_key, image, subject):
44
  },
45
  {
46
  "type": "image_url",
47
- "image_url": {
48
- "url": image_url_data
49
- }
50
  }
51
  ]
52
  }
53
  ]
54
- }
55
-
56
- response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
57
-
58
- if response.status_code != 200:
59
- return f"❌ API Error {response.status_code}: {response.text}"
60
-
61
- completion = response.json()
62
- message = completion.get("choices", [{}])[0].get("message", {}).get("content")
63
-
64
- if not message:
65
- return "⚠️ No response content received. Try another image or check the model output."
66
-
67
- return message
68
-
69
  except Exception as e:
70
- return f"❌ API call failed: {str(e)}"
 
 
71
 
72
- # Launch Gradio Interface
73
- iface = gr.Interface(
74
- fn=solve_stem_problem,
75
- inputs=[
76
- gr.Textbox(label="πŸ” OpenRouter API Key", type="password"),
77
- gr.Image(label="πŸ–ΌοΈ Upload STEM Problem Image", type="pil"),
78
- gr.Dropdown(["math", "physics", "chemistry", "coding"], label="πŸ“š Select Subject")
79
- ],
80
- outputs=gr.Textbox(label="πŸ•΅οΈβ€β™‚οΈ Detective's Solution"),
81
- title="🧠 STEM Sleuth Solver",
82
- description="Upload a math, physics, chemistry, or coding problem image and solve it with a detective twist using OpenRouter Gemini model."
83
- )
 
 
 
 
 
 
 
 
 
 
84
 
85
- iface.launch()
 
 
1
  import gradio as gr
2
+ from openai import OpenAI
3
  import base64
4
  import io
 
 
 
 
 
5
 
6
+ def solve_stem_problem(api_key, image, subject="math"):
7
+ # Initialize OpenAI client with user-provided API key
8
+ client = OpenAI(
9
+ base_url="https://openrouter.ai/api/v1",
10
+ api_key=api_key,
11
+ )
12
+
13
+ # Define detective based on subject
14
  detectives = {
15
  "math": "Algebra Ace",
16
  "physics": "Physics Phantom",
 
18
  "coding": "Code Cracker"
19
  }
20
  detective = detectives.get(subject, "Algebra Ace")
21
+
22
+ # Encode the uploaded image to base64
23
  try:
24
+ # Convert the image to bytes
25
+ img_byte_arr = io.BytesIO()
26
+ image.save(img_byte_arr, format='PNG')
27
+ img_byte_arr = img_byte_arr.getvalue()
28
+
29
+ # Encode to base64
30
+ encoded_image = base64.b64encode(img_byte_arr).decode('utf-8')
31
  image_url_data = f"data:image/png;base64,{encoded_image}"
32
  except Exception as e:
33
+ return f"Error encoding image: {str(e)}"
34
+
35
+ # Call the Gemini model
36
  try:
37
+ completion = client.chat.completions.create(
38
+ extra_headers={
39
+ "HTTP-Referer": "https://stem-sleuth.example.com",
40
+ "X-Title": "STEM Sleuth",
41
+ },
42
+ model="google/gemini-2.0-flash-exp:free",
43
+ messages=[
 
 
 
44
  {
45
  "role": "user",
46
  "content": [
 
50
  },
51
  {
52
  "type": "image_url",
53
+ "image_url": {"url": image_url_data}
 
 
54
  }
55
  ]
56
  }
57
  ]
58
+ )
59
+
60
+ # Check for valid response
61
+ if completion.choices and len(completion.choices) > 0 and completion.choices[0].message:
62
+ solution = completion.choices[0].message.content
63
+ else:
64
+ solution = "Could not retrieve a solution from the API."
 
 
 
 
 
 
 
 
65
  except Exception as e:
66
+ solution = f"Error calling API: {str(e)}"
67
+
68
+ return solution
69
 
70
+ # Create Gradio interface
71
+ with gr.Blocks() as app:
72
+ gr.Markdown("# STEM Sleuth Problem Solver")
73
+ gr.Markdown("Upload an image of a STEM problem, select the subject, and provide your API key to get a step-by-step solution.")
74
+
75
+ with gr.Row():
76
+ api_key_input = gr.Textbox(label="OpenRouter API Key", type="password", placeholder="Enter your API key")
77
+ subject_input = gr.Dropdown(
78
+ choices=["math", "physics", "chemistry", "coding"],
79
+ label="Subject",
80
+ value="math"
81
+ )
82
+
83
+ image_input = gr.Image(type="pil", label="Upload Problem Image")
84
+ solve_button = gr.Button("Solve Problem")
85
+ output = gr.Textbox(label="Solution", lines=10)
86
+
87
+ solve_button.click(
88
+ fn=solve_stem_problem,
89
+ inputs=[api_key_input, image_input, subject_input],
90
+ outputs=output
91
+ )
92
 
93
+ # Launch the app
94
+ app.launch()