STEM-Sleuth / app.py
shukdevdatta123's picture
Create app.py
f29b99e verified
raw
history blame
2.65 kB
import gradio as gr
from openai import OpenAI
import base64
import io
def solve_stem_problem(api_key, image, subject):
if 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 PIL Image to base64
buffered = io.BytesIO()
image_format = "PNG"
image.save(buffered, format=image_format)
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:
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=api_key,
)
completion = client.chat.completions.create(
extra_headers={
"HTTP-Referer": "https://stem-sleuth.example.com",
"X-Title": "STEM Sleuth",
},
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}
}
]
}
]
)
if completion.choices and len(completion.choices) > 0:
return completion.choices[0].message.content
else:
return "⚠️ Could not retrieve a solution from the API."
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()