Update tools/analyze.py

#30
Files changed (1) hide show
  1. tools/analyze.py +96 -46
tools/analyze.py CHANGED
@@ -1,23 +1,31 @@
1
  import os
 
2
  import base64
3
  import requests
4
 
5
-
6
  OPENROUTER_API_KEY = "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"
7
 
8
- # Change this if you use another vision model
9
  MODEL = "google/gemini-2.5-flash"
10
 
 
 
11
 
12
  def analyze_image(image_path: str):
13
 
 
 
 
 
 
 
14
  if not os.path.exists(image_path):
15
  return {
16
- "description": "Image not found."
 
17
  }
18
 
19
  with open(image_path, "rb") as f:
20
- image_base64 = base64.b64encode(f.read()).decode("utf-8")
21
 
22
  headers = {
23
  "Authorization": f"Bearer {OPENROUTER_API_KEY}",
@@ -26,6 +34,40 @@ def analyze_image(image_path: str):
26
  "X-Title": "AI Image Studio"
27
  }
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  payload = {
30
  "model": MODEL,
31
  "messages": [
@@ -34,67 +76,44 @@ def analyze_image(image_path: str):
34
  "content": [
35
  {
36
  "type": "text",
37
- "text": """
38
- Analyze this image.
39
-
40
- Return:
41
-
42
- Description:
43
- Objects:
44
- Style:
45
- Lighting:
46
- Colors:
47
- Suggestions:
48
- """
49
  },
50
  {
51
  "type": "image_url",
52
  "image_url": {
53
- "url": f"data:image/jpeg;base64,{image_base64}"
54
  }
55
  }
56
  ]
57
  }
58
  ],
59
  "temperature": 0.2,
60
- "max_tokens": 500
61
  }
62
 
63
  try:
64
 
65
  response = requests.post(
66
- "https://openrouter.ai/api/v1/chat/completions",
67
  headers=headers,
68
  json=payload,
69
- timeout=120
70
  )
71
 
72
  print("Status:", response.status_code)
73
 
74
  result = response.json()
75
 
76
- print("========== OPENROUTER RESPONSE ==========")
77
  print(result)
78
- print("=========================================")
79
 
80
  if response.status_code != 200:
81
 
82
- message = result.get(
83
- "error",
84
- {}
85
- ).get(
86
- "message",
87
- "Unknown OpenRouter error."
88
- )
89
-
90
- return {
91
- "description": f"OpenRouter Error ({response.status_code}): {message}"
92
- }
93
-
94
- if "error" in result:
95
-
96
  return {
97
- "description": result["error"].get(
 
 
 
 
98
  "message",
99
  "Unknown OpenRouter error."
100
  )
@@ -103,21 +122,52 @@ Suggestions:
103
  if "choices" not in result:
104
 
105
  return {
106
- "description": f"Unexpected response:\n{result}"
 
 
107
  }
108
 
109
- content = (
110
- result["choices"][0]
111
- .get("message", {})
112
- .get("content", "")
113
- )
114
 
115
- return {
116
- "description": content
117
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  except Exception as e:
120
 
121
  return {
 
122
  "description": str(e)
123
  }
 
1
  import os
2
+ import json
3
  import base64
4
  import requests
5
 
 
6
  OPENROUTER_API_KEY = "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"
7
 
 
8
  MODEL = "google/gemini-2.5-flash"
9
 
10
+ API_URL = "https://openrouter.ai/api/v1/chat/completions"
11
+
12
 
13
  def analyze_image(image_path: str):
14
 
15
+ if not image_path:
16
+ return {
17
+ "success": False,
18
+ "description": "No image provided."
19
+ }
20
+
21
  if not os.path.exists(image_path):
22
  return {
23
+ "success": False,
24
+ "description": f"Image not found: {image_path}"
25
  }
26
 
27
  with open(image_path, "rb") as f:
28
+ image = base64.b64encode(f.read()).decode("utf-8")
29
 
30
  headers = {
31
  "Authorization": f"Bearer {OPENROUTER_API_KEY}",
 
34
  "X-Title": "AI Image Studio"
35
  }
36
 
37
+ prompt = """
38
+ You are an expert computer vision assistant.
39
+
40
+ Analyze this image.
41
+
42
+ Return ONLY valid JSON.
43
+
44
+ {
45
+ "description":"",
46
+ "objects":[],
47
+ "people":"",
48
+ "style":"",
49
+ "lighting":"",
50
+ "colors":[],
51
+ "background":"",
52
+ "camera_angle":"",
53
+ "quality":"",
54
+ "editing_prompt":"",
55
+ "suggestions":[]
56
+ }
57
+
58
+ Rules:
59
+
60
+ Return JSON only.
61
+
62
+ Do not write markdown.
63
+
64
+ Do not use ```.
65
+
66
+ editing_prompt should preserve every important detail so future editing keeps the same image.
67
+
68
+ suggestions should contain 6 editing ideas.
69
+ """
70
+
71
  payload = {
72
  "model": MODEL,
73
  "messages": [
 
76
  "content": [
77
  {
78
  "type": "text",
79
+ "text": prompt
 
 
 
 
 
 
 
 
 
 
 
80
  },
81
  {
82
  "type": "image_url",
83
  "image_url": {
84
+ "url": f"data:image/jpeg;base64,{image}"
85
  }
86
  }
87
  ]
88
  }
89
  ],
90
  "temperature": 0.2,
91
+ "max_tokens": 1500
92
  }
93
 
94
  try:
95
 
96
  response = requests.post(
97
+ API_URL,
98
  headers=headers,
99
  json=payload,
100
+ timeout=180
101
  )
102
 
103
  print("Status:", response.status_code)
104
 
105
  result = response.json()
106
 
 
107
  print(result)
 
108
 
109
  if response.status_code != 200:
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  return {
112
+ "success": False,
113
+ "description": result.get(
114
+ "error",
115
+ {}
116
+ ).get(
117
  "message",
118
  "Unknown OpenRouter error."
119
  )
 
122
  if "choices" not in result:
123
 
124
  return {
125
+ "success": False,
126
+ "description": "No choices returned.",
127
+ "raw": result
128
  }
129
 
130
+ content = result["choices"][0]["message"]["content"]
 
 
 
 
131
 
132
+ content = content.replace("```json", "")
133
+ content = content.replace("```", "")
134
+ content = content.strip()
135
+
136
+ try:
137
+
138
+ analysis = json.loads(content)
139
+
140
+ analysis["success"] = True
141
+
142
+ return analysis
143
+
144
+ except Exception:
145
+
146
+ return {
147
+ "success": True,
148
+ "description": content,
149
+ "objects": [],
150
+ "people": "",
151
+ "style": "",
152
+ "lighting": "",
153
+ "colors": [],
154
+ "background": "",
155
+ "camera_angle": "",
156
+ "quality": "",
157
+ "editing_prompt": content,
158
+ "suggestions": [
159
+ "Make it Anime",
160
+ "Pixar Style",
161
+ "Remove Background",
162
+ "Replace Background",
163
+ "Enhance Quality",
164
+ "Change Colors"
165
+ ]
166
+ }
167
 
168
  except Exception as e:
169
 
170
  return {
171
+ "success": False,
172
  "description": str(e)
173
  }