Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
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):
|
7 |
-
if api_key.strip()
|
8 |
return "β Error: API key is required."
|
9 |
|
10 |
detectives = {
|
@@ -16,28 +16,25 @@ def solve_stem_problem(api_key, image, subject):
|
|
16 |
detective = detectives.get(subject, "Algebra Ace")
|
17 |
|
18 |
try:
|
19 |
-
# Convert
|
20 |
buffered = io.BytesIO()
|
21 |
-
|
22 |
-
image.save(buffered, format=image_format)
|
23 |
encoded_image = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
24 |
image_url_data = f"data:image/png;base64,{encoded_image}"
|
25 |
except Exception as e:
|
26 |
return f"β Error encoding image: {str(e)}"
|
27 |
|
28 |
try:
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
"X-Title": "STEM Sleuth",
|
38 |
-
},
|
39 |
-
model="google/gemini-2.0-flash-exp:free",
|
40 |
-
messages=[
|
41 |
{
|
42 |
"role": "user",
|
43 |
"content": [
|
@@ -47,17 +44,27 @@ def solve_stem_problem(api_key, image, subject):
|
|
47 |
},
|
48 |
{
|
49 |
"type": "image_url",
|
50 |
-
"image_url": {
|
|
|
|
|
51 |
}
|
52 |
]
|
53 |
}
|
54 |
]
|
55 |
-
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
except Exception as e:
|
63 |
return f"β API call failed: {str(e)}"
|
@@ -75,4 +82,4 @@ iface = gr.Interface(
|
|
75 |
description="Upload a math, physics, chemistry, or coding problem image and solve it with a detective twist using OpenRouter Gemini model."
|
76 |
)
|
77 |
|
78 |
-
iface.launch()
|
|
|
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 = {
|
|
|
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 |
},
|
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)}"
|
|
|
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()
|