Update tools/analyze.py

#29
Files changed (1) hide show
  1. tools/analyze.py +87 -17
tools/analyze.py CHANGED
@@ -1,21 +1,29 @@
 
1
  import base64
2
  import requests
3
- from PIL import Image
4
- import os
5
 
6
  OPENROUTER_API_KEY = "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"
7
 
8
- MODEL = "openai/gpt-4.1-mini"
 
9
 
10
 
11
- def analyze_image(image_path):
 
 
 
 
 
12
 
13
  with open(image_path, "rb") as f:
14
- image_base64 = base64.b64encode(f.read()).decode()
15
 
16
  headers = {
17
  "Authorization": f"Bearer {OPENROUTER_API_KEY}",
18
  "Content-Type": "application/json",
 
 
19
  }
20
 
21
  payload = {
@@ -26,7 +34,18 @@ def analyze_image(image_path):
26
  "content": [
27
  {
28
  "type": "text",
29
- "text": "Analyze this image in detail. Return description, objects, style and lighting."
 
 
 
 
 
 
 
 
 
 
 
30
  },
31
  {
32
  "type": "image_url",
@@ -36,18 +55,69 @@ def analyze_image(image_path):
36
  }
37
  ]
38
  }
39
- ]
 
 
40
  }
41
 
42
- response = requests.post(
43
- "https://openrouter.ai/api/v1/chat/completions",
44
- headers=headers,
45
- json=payload,
46
- timeout=120
47
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- result = response.json()
50
 
51
- return {
52
- "description": result["choices"][0]["message"]["content"]
53
- }
 
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}",
24
  "Content-Type": "application/json",
25
+ "HTTP-Referer": "http://localhost",
26
+ "X-Title": "AI Image Studio"
27
  }
28
 
29
  payload = {
 
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",
 
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
+ )
101
+ }
102
+
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
+ }