File size: 2,901 Bytes
f29b99e
 
 
c08e01b
f29b99e
 
c08e01b
f29b99e
 
 
 
 
 
 
 
 
 
 
c08e01b
f29b99e
c08e01b
f29b99e
 
 
 
 
 
c08e01b
 
 
 
 
 
f29b99e
c08e01b
 
 
f29b99e
 
 
 
 
 
 
 
 
c08e01b
 
 
f29b99e
 
 
 
c08e01b
f29b99e
c08e01b
 
 
 
 
 
 
 
 
 
 
 
f29b99e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c08e01b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import gradio as gr
import base64
import io
import requests

def solve_stem_problem(api_key, image, subject):
    if not api_key.strip():
        return "❌ Error: API key is required."

    detectives = {
        "math": "Algebra Ace",
        "physics": "Physics Phantom",
        "chemistry": "Chemistry Clue-finder",
        "coding": "Code Cracker"
    }
    detective = detectives.get(subject, "Algebra Ace")

    try:
        # Convert image to base64 string
        buffered = io.BytesIO()
        image.save(buffered, format="PNG")
        encoded_image = base64.b64encode(buffered.getvalue()).decode('utf-8')
        image_url_data = f"data:image/png;base64,{encoded_image}"
    except Exception as e:
        return f"❌ Error encoding image: {str(e)}"

    try:
        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json",
            "HTTP-Referer": "https://stem-sleuth.example.com",
            "X-Title": "STEM Sleuth"
        }

        payload = {
            "model": "google/gemini-2.0-flash-exp:free",
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "text",
                            "text": f"Act as {detective} and solve this {subject} problem step-by-step with a detective narrative."
                        },
                        {
                            "type": "image_url",
                            "image_url": {
                                "url": image_url_data
                            }
                        }
                    ]
                }
            ]
        }

        response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)

        if response.status_code != 200:
            return f"❌ API Error {response.status_code}: {response.text}"

        completion = response.json()
        message = completion.get("choices", [{}])[0].get("message", {}).get("content")

        if not message:
            return "⚠️ No response content received. Try another image or check the model output."

        return message

    except Exception as e:
        return f"❌ API call failed: {str(e)}"

# Launch Gradio Interface
iface = gr.Interface(
    fn=solve_stem_problem,
    inputs=[
        gr.Textbox(label="πŸ” OpenRouter API Key", type="password"),
        gr.Image(label="πŸ–ΌοΈ Upload STEM Problem Image", type="pil"),
        gr.Dropdown(["math", "physics", "chemistry", "coding"], label="πŸ“š Select Subject")
    ],
    outputs=gr.Textbox(label="πŸ•΅οΈβ€β™‚οΈ Detective's Solution"),
    title="🧠 STEM Sleuth Solver",
    description="Upload a math, physics, chemistry, or coding problem image and solve it with a detective twist using OpenRouter Gemini model."
)

iface.launch()