-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths04e01.py
228 lines (194 loc) · 7.05 KB
/
s04e01.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Standard library imports
import requests
import base64
# Local imports
from utilities.common import AIDevsClient, OpenAIClient
from utilities.config import AI_DEVS_API_KEY, S04E01_TASK_URL, S04E01_REPORT_URL
# =========================================================
# Constants
# =========================================================
TASK_NAME = "photos"
API_URL = S04E01_REPORT_URL
PHOTOS_URL = S04E01_TASK_URL
SUBMIT_URL = S04E01_REPORT_URL
# =========================================================
# Helper Functions
# =========================================================
def get_info(url):
"""Get image description or status (dark/bright/damaged) from URL."""
try:
print(f"\n🔍 Analyzing image: {url}")
url = PHOTOS_URL + url.strip("'")
response = requests.get(url)
if response.status_code != 200:
print("❌ Error downloading image")
return "Error downloading image"
encoded = base64.b64encode(response.content).decode('utf-8')
messages = [{
"role": "user",
"content": [
{
"type": "text",
"text": """
If image is dark - return 'BRIGHTEN'
If bright - return 'DARKEN'
If damaged - return 'REPAIR'
Otherwise describe this person focusing on:
- Physical characteristics (face, hair, eyes)
- Clothing and style
- Distinctive features
"""
},
{
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{encoded}"}
}
]
}]
result = client_openai.get_completion(
messages=messages,
model="gpt-4o",
temperature=0.1
)
print(f"📝 Analysis result: {result}")
return result
except Exception as e:
print(f"❌ Error processing image: {e}")
return None
def process_image_command(image_name, command=None):
"""Process image with optional command and get next image name."""
try:
print(f"\n🔧 Processing image: {image_name}")
print(f"Command: {command if command else 'No command - getting info only'}")
if command:
payload = {
"task": TASK_NAME,
"apikey": AI_DEVS_API_KEY,
"answer": f"{command} {image_name}"
}
response = client_aidevs.submit_answer(
answer=payload,
submit_url=SUBMIT_URL
)
print(f"📥 Response received: {response}")
messages = [
{
"role": "system",
"content": "Extract only the filename from the text."
},
{
"role": "user",
"content": response['message']
}
]
next_image = client_openai.get_completion(
messages=messages,
model="gpt-4o",
temperature=0.1
)
print(f"📸 Next image: {next_image}")
return next_image
else:
return get_info(image_name)
except Exception as e:
print(f"❌ Error processing command for image {image_name}: {e}")
return None
def process_image_until_complete(initial_image):
"""Process a single image until it's fully corrected and returns a description."""
current_image = initial_image
while current_image:
image_status = get_info(current_image)
if image_status in ["REPAIR", "DARKEN", "BRIGHTEN"]:
current_image = process_image_command(current_image, image_status)
else:
return image_status
return None
def main():
"""Main execution function."""
global client_aidevs, client_openai
# =========================================================
# Initialize clients
# =========================================================
client_aidevs = AIDevsClient()
client_openai = OpenAIClient()
# =========================================================
# Step 1: Start conversation and get photos
# =========================================================
payload = {
"task": TASK_NAME,
"apikey": AI_DEVS_API_KEY,
"answer": "START"
}
response = client_aidevs.submit_answer(
answer=payload,
submit_url=SUBMIT_URL
)
message_of_photos = response['message']
messages = [
{
"role": "system",
"content": "Extract image names from the text, return only the filenames separated by spaces"
},
{
"role": "user",
"content": message_of_photos
}
]
list_of_urls = client_openai.get_completion(
messages=messages,
model="gpt-4o",
temperature=0.1
).split()
print(f"📸 Found images: {list_of_urls}")
# =========================================================
# Step 2: Process images and generate descriptions
# =========================================================
descriptions = []
for image in list_of_urls:
description = process_image_until_complete(image)
if description:
descriptions.append(description)
# =========================================================
# Step 3: Generate final description
# =========================================================
messages = [
{
"role": "system",
"content": """
Jesteś ekspertem w tworzeniu rysopisów osób. Na podstawie opisów:
1. Przeanalizuj wszystkie opisy i znajdź powtarzające się cechy
2. Stwórz zwięzły rysopis w języku polskim, skupiając się na:
- Stałych cechach fizycznych (twarz, włosy, oczy)
- Charakterystycznych znakach szczególnych
- Typowym stylu ubioru
3. Użyj wypunktowania dla przejrzystości
4. Pomiń zmienne elementy jak tło czy pozy
"""
},
{
"role": "user",
"content": f"Stwórz rysopis Barbary na podstawie tych opisów:\n\n{descriptions}"
}
]
rysopis = client_openai.get_completion(
messages=messages,
model="gpt-4o",
temperature=0.1
)
print("\n📝 Rysopis Barbary:")
print(rysopis)
# =========================================================
# Step 4: Submit final answer
# =========================================================
final_payload = {
"task": TASK_NAME,
"apikey": AI_DEVS_API_KEY,
"answer": rysopis
}
response = client_aidevs.submit_answer(
answer=final_payload,
submit_url=SUBMIT_URL
)
print(f"Final response: {response}")
if __name__ == "__main__":
main()