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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -23
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 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": [
@@ -47,17 +44,27 @@ def solve_stem_problem(api_key, image, subject):
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)}"
@@ -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()