Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = {
|
11 |
+
"math": "Algebra Ace",
|
12 |
+
"physics": "Physics Phantom",
|
13 |
+
"chemistry": "Chemistry Clue-finder",
|
14 |
+
"coding": "Code Cracker"
|
15 |
+
}
|
16 |
+
detective = detectives.get(subject, "Algebra Ace")
|
17 |
+
|
18 |
+
try:
|
19 |
+
# Convert PIL Image to base64
|
20 |
+
buffered = io.BytesIO()
|
21 |
+
image_format = "PNG"
|
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 |
+
client = OpenAI(
|
30 |
+
base_url="https://openrouter.ai/api/v1",
|
31 |
+
api_key=api_key,
|
32 |
+
)
|
33 |
+
|
34 |
+
completion = client.chat.completions.create(
|
35 |
+
extra_headers={
|
36 |
+
"HTTP-Referer": "https://stem-sleuth.example.com",
|
37 |
+
"X-Title": "STEM Sleuth",
|
38 |
+
},
|
39 |
+
model="google/gemini-2.0-flash-exp:free",
|
40 |
+
messages=[
|
41 |
+
{
|
42 |
+
"role": "user",
|
43 |
+
"content": [
|
44 |
+
{
|
45 |
+
"type": "text",
|
46 |
+
"text": f"Act as {detective} and solve this {subject} problem step-by-step with a detective narrative."
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"type": "image_url",
|
50 |
+
"image_url": {"url": image_url_data}
|
51 |
+
}
|
52 |
+
]
|
53 |
+
}
|
54 |
+
]
|
55 |
+
)
|
56 |
+
|
57 |
+
if completion.choices and len(completion.choices) > 0:
|
58 |
+
return completion.choices[0].message.content
|
59 |
+
else:
|
60 |
+
return "β οΈ Could not retrieve a solution from the API."
|
61 |
+
|
62 |
+
except Exception as e:
|
63 |
+
return f"β API call failed: {str(e)}"
|
64 |
+
|
65 |
+
# Launch Gradio Interface
|
66 |
+
iface = gr.Interface(
|
67 |
+
fn=solve_stem_problem,
|
68 |
+
inputs=[
|
69 |
+
gr.Textbox(label="π OpenRouter API Key", type="password"),
|
70 |
+
gr.Image(label="πΌοΈ Upload STEM Problem Image", type="pil"),
|
71 |
+
gr.Dropdown(["math", "physics", "chemistry", "coding"], label="π Select Subject")
|
72 |
+
],
|
73 |
+
outputs=gr.Textbox(label="π΅οΈββοΈ Detective's Solution"),
|
74 |
+
title="π§ STEM Sleuth Solver",
|
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()
|