-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
351 lines (283 loc) · 10.4 KB
/
utils.py
File metadata and controls
351 lines (283 loc) · 10.4 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# Utility functions for BLEnD-VIS
"""
Utility functions for:
1. OpenAI Batch API operations
2. Async image generation
3. File handling and sanitization
"""
import asyncio
import base64
import json
import logging
import os
import random
import re
import time
from io import BytesIO
from pathlib import Path
from typing import Dict, List, Optional
import backoff
from dotenv import load_dotenv
from openai import AsyncOpenAI, OpenAI
from PIL import Image
from tqdm import tqdm
from tqdm.asyncio import tqdm as async_tqdm
# =============================================================================
# FILE UTILITIES
# =============================================================================
def sanitize_filename(text: str, max_length: int = 50) -> str:
"""
Create a safe filename by removing illegal characters and truncating.
Args:
text: Input text to sanitize
max_length: Maximum length of output
Returns:
Sanitized filename string
"""
# Remove illegal characters
text = re.sub(r'[<>:"/\\|?*\n\t]', '', text)
# Replace whitespace with underscore
text = re.sub(r'\s+', '_', text)
# Collapse multiple underscores
text = re.sub(r'__+', '_', text)
# Remove leading/trailing underscores
text = text.strip('_.-')
return text[:max_length]
def load_json(filepath: Path) -> dict:
"""Load JSON file with error handling."""
with open(filepath, 'r', encoding='utf-8') as f:
return json.load(f)
def save_json(data: dict, filepath: Path, indent: int = 2):
"""Save data to JSON file."""
filepath.parent.mkdir(parents=True, exist_ok=True)
with open(filepath, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=indent, ensure_ascii=False)
# =============================================================================
# OPENAI BATCH API
# =============================================================================
def submit_and_retrieve_batch(
tasks: List[dict],
batch_name: str,
client: OpenAI,
output_dir: str = "batch_results",
poll_interval: int = 30
) -> List[dict]:
"""
Submit tasks to OpenAI Batch API, poll until completion, retrieve results.
Args:
tasks: List of task dictionaries for batch processing
batch_name: Unique identifier for this batch
client: OpenAI client instance
output_dir: Directory to save batch files
poll_interval: Seconds between status checks
Returns:
List of result dictionaries from the batch
"""
if not tasks:
print("No tasks provided for batch submission.")
return []
os.makedirs(output_dir, exist_ok=True)
# Save tasks to JSONL
jsonl_path = os.path.join(output_dir, f"{batch_name}_batch_tasks.jsonl")
with open(jsonl_path, 'w', encoding='utf-8') as f:
for task in tasks:
f.write(json.dumps(task) + '\n')
# Upload and create batch
batch_file = client.files.create(
file=open(jsonl_path, "rb"),
purpose="batch"
)
batch_job = client.batches.create(
input_file_id=batch_file.id,
completion_window="24h",
endpoint="/v1/chat/completions",
)
print(f"Submitted batch job: {batch_job.id}")
# Poll for completion
while True:
batch_job = client.batches.retrieve(batch_job.id)
if batch_job.status == "completed":
print("Batch job completed.")
break
elif batch_job.status == "failed":
print("Batch job failed.")
return []
else:
if hasattr(batch_job, 'request_counts'):
completed = batch_job.request_counts.completed
total = batch_job.request_counts.total
print(f"Progress: {completed}/{total} completed. Waiting...")
else:
print("Batch job in progress. Waiting...")
time.sleep(poll_interval)
# Retrieve results
result_file_id = batch_job.output_file_id
result = client.files.content(result_file_id).content
result_path = os.path.join(output_dir, f"{batch_name}_batch_results.jsonl")
with open(result_path, 'wb') as file:
file.write(result)
# Parse results
results = []
with open(result_path, 'r', encoding='utf-8') as f:
for line in f:
results.append(json.loads(line))
return results
# =============================================================================
# ASYNC IMAGE GENERATION (OpenRouter)
# =============================================================================
def parse_base64_data_url(data_url: str) -> bytes:
"""Extract and decode Base64 data from a data URL."""
try:
_header, encoded = data_url.split(",", 1)
return base64.b64decode(encoded)
except (ValueError, IndexError, TypeError) as e:
raise ValueError(f"Invalid Base64 data URL format: {e}")
@backoff.on_exception(
backoff.expo,
Exception,
max_tries=5,
on_giveup=lambda details: logging.error(
f"API call for {details['args'][0].get('output_filename', 'unknown')} "
f"failed after {details['tries']} tries. Error: {details['exception']}"
)
)
async def get_image_completion_async(
task: Dict,
model: str,
client: AsyncOpenAI
) -> Dict:
"""
Make an async API call to OpenRouter for image generation.
Args:
task: Dictionary with 'prompt', 'output_path', 'output_filename'
model: Model identifier
client: AsyncOpenAI client
Returns:
Dictionary with status and output info
"""
prompt_text = task['prompt']
output_path = Path(task['output_path'])
output_filename = task['output_filename']
logging.info(f"Generating image: {output_filename}")
completion = await client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt_text}],
extra_body={"modalities": ["image", "text"]}
)
if not (completion.choices and completion.choices[0].message):
raise ValueError("Invalid API response: No choices or message")
message = completion.choices[0].message
message_dict = message.model_dump()
images_data = message_dict.get("images")
if not images_data or not isinstance(images_data, list):
raise ValueError("No valid image data in response")
image_url = images_data[0].get("image_url", {}).get("url")
if not image_url:
raise ValueError("No image URL in response")
image_bytes = parse_base64_data_url(image_url)
image = Image.open(BytesIO(image_bytes))
if image.mode in ('RGBA', 'P'):
image = image.convert('RGB')
output_path.parent.mkdir(parents=True, exist_ok=True)
image.save(output_path, format='PNG')
logging.info(f"SUCCESS: Saved {output_path}")
return {
'status': 'success',
'output_filename': output_filename,
'output_path': str(output_path)
}
async def generate_images_asynchronously(
tasks: List[Dict],
model: str,
client: AsyncOpenAI,
concurrency_limit: int = 10
) -> List[Dict]:
"""
Run image generation for all tasks asynchronously.
Args:
tasks: List of task dictionaries
model: Model identifier
client: AsyncOpenAI client
concurrency_limit: Max concurrent requests
Returns:
List of result dictionaries
"""
if not client:
raise ValueError("AsyncOpenAI client not initialized")
semaphore = asyncio.Semaphore(concurrency_limit)
async def process_with_semaphore(task: Dict, pbar: async_tqdm) -> Dict:
async with semaphore:
try:
await asyncio.sleep(random.uniform(0.1, 0.5))
result = await get_image_completion_async(task, model, client)
pbar.update(1)
return result
except Exception as e:
logging.error(f"FAILED: {task['output_filename']} - {e}")
pbar.update(1)
return {
'status': 'failure',
'output_filename': task['output_filename'],
'error': str(e)
}
with async_tqdm(total=len(tasks), desc="Generating images") as pbar:
async_tasks = [process_with_semaphore(task, pbar) for task in tasks]
results = await asyncio.gather(*async_tasks)
return results
# =============================================================================
# RESULT SCORING
# =============================================================================
def extract_answer_choice(output: str) -> Optional[str]:
"""
Extract answer choice from model output.
Args:
output: Model output string
Returns:
Extracted choice letter or None
"""
if not output:
return None
# Try JSON parsing first
try:
# Handle markdown code blocks
cleaned = output.strip()
if cleaned.startswith("```"):
cleaned = re.sub(r'```\w*\n?', '', cleaned).strip()
data = json.loads(cleaned)
if isinstance(data, dict) and 'answer_choice' in data:
return data['answer_choice'].strip().upper()
except (json.JSONDecodeError, AttributeError):
pass
# Fallback: look for letter pattern
match = re.search(r'\b([A-D])\b', output.upper())
if match:
return match.group(1)
return None
def score_results(
results: List[Dict],
mcq_data: List[Dict]
) -> List[Dict]:
"""
Score model results against ground truth.
Args:
results: List of result dicts with 'MCQID' and 'output'
mcq_data: List of MCQ dicts with 'MCQID' and 'answer_idx'
Returns:
List of scored results with 'correct' field added
"""
# Build lookup
answer_lookup = {mcq['MCQID']: mcq['answer_idx'] for mcq in mcq_data}
scored = []
for result in results:
mcqid = result.get('MCQID')
output = result.get('output', '')
predicted = extract_answer_choice(output)
correct_answer = answer_lookup.get(mcqid)
scored.append({
'MCQID': mcqid,
'output': output,
'predicted': predicted,
'answer_idx': correct_answer,
'correct': predicted == correct_answer if predicted and correct_answer else False
})
return scored