-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt_generator.py
More file actions
397 lines (327 loc) · 13.1 KB
/
prompt_generator.py
File metadata and controls
397 lines (327 loc) · 13.1 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python3
"""Generate image prompts using Gemini image understanding."""
import argparse
import asyncio
import base64
from typing import Optional, Tuple
import aiohttp
import boto3
import config
import storage
GEMINI_MODEL = "gemini-3-pro-preview"
REPLICATE_MODEL = "google/gemini-3-pro"
# Global flag for using Replicate
USE_REPLICATE = False
async def generate_prompt_for_image_replicate_async(
session: aiohttp.ClientSession,
image_bytes: bytes,
category: str = None,
caption: str = None,
) -> str:
"""Use Gemini via Replicate to generate a prompt that describes the image (async)."""
b64_data = base64.b64encode(image_bytes).decode("utf-8")
data_uri = f"data:image/jpeg;base64,{b64_data}"
system_prompt = """You are an expert at describing images in precise detail for image generation AI models.
Analyze this photograph and write a detailed prompt that could be used to recreate this exact image.
Focus on:
- Main subject and its characteristics
- Setting/environment
- Lighting conditions
- Camera angle and perspective
- Colors, textures, and materials
- Any distinctive features or details
Write a single detailed paragraph that captures all visual elements. Be specific and precise. Use 1024 characters or less. Respond only with the description, no other text."""
if caption:
system_prompt += f'\n\nFor reference, the original caption for this image is: "{caption}".\nUse this context to better understand the scene, but focus on describing what you see visually.'
headers = {
"Authorization": f"Bearer {config.REPLICATE_API_TOKEN}",
"Content-Type": "application/json",
"Prefer": "wait",
}
payload = {
"input": {
"prompt": system_prompt,
"images": [data_uri],
"temperature": 0.7,
"max_output_tokens": 2048,
}
}
api_base = "https://api.replicate.com/v1"
async with session.post(
f"{api_base}/models/{REPLICATE_MODEL}/predictions",
json=payload,
headers=headers,
timeout=aiohttp.ClientTimeout(total=120),
) as response:
if response.status not in (200, 201):
text = await response.text()
raise RuntimeError(f"Replicate API error {response.status}: {text}")
result = await response.json()
# Poll if not completed
if result.get("status") not in ("succeeded", "failed", "canceled"):
poll_url = result.get("urls", {}).get("get") or f"{api_base}/predictions/{result['id']}"
for _ in range(60):
await asyncio.sleep(1)
async with session.get(poll_url, headers=headers) as poll_resp:
result = await poll_resp.json()
if result.get("status") in ("succeeded", "failed", "canceled"):
break
if result.get("status") != "succeeded":
raise RuntimeError(f"Replicate prediction failed: {result}")
output = result.get("output")
if isinstance(output, list):
return "".join(output).strip()
return str(output).strip() if output else ""
async def generate_prompt_for_image_async(
session: aiohttp.ClientSession,
image_bytes: bytes,
category: str = None,
caption: str = None,
) -> str:
"""Use Gemini to generate a detailed text prompt that describes the image (async).
Args:
session: aiohttp session
image_bytes: Raw image bytes
category: Optional category hint for better prompt generation
caption: Optional original caption/description from the source
Returns:
Generated text prompt
"""
# Encode image as base64
b64_data = base64.b64encode(image_bytes).decode("utf-8")
# Build the prompt based on category
system_prompt = """You are an expert at describing images in precise detail for image generation AI models.
Analyze this photograph and write a detailed prompt that could be used to recreate this exact image.
Focus on:
- Main subject and its characteristics
- Setting/environment
- Lighting conditions
- Camera angle and perspective
- Colors, textures, and materials
- Any distinctive features or details
Write a single detailed paragraph that captures all visual elements. Be specific and precise. Use 1024 characters or less. Respond only with the description, no other text."""
# If we have a caption, include it as context
if caption:
system_prompt += f'\n\nFor reference, the original caption for this image is: "{caption}".\nUse this context to better understand the scene, but focus on describing what you see visually.'
# Call Gemini API
url = f"https://generativelanguage.googleapis.com/v1beta/models/{GEMINI_MODEL}:generateContent"
payload = {
"contents": [
{
"parts": [
{"text": system_prompt},
{
"inline_data": {
"mime_type": "image/jpeg",
"data": b64_data,
}
},
]
}
],
"generationConfig": {
"temperature": 0.7,
"maxOutputTokens": 2048,
},
}
async with session.post(
url,
json=payload,
params={"key": config.GEMINI_API_KEY},
timeout=aiohttp.ClientTimeout(total=60),
) as response:
if response.status != 200:
text = await response.text()
raise RuntimeError(f"Gemini API error {response.status}: {text}")
result = await response.json()
# Extract text from response
try:
text = result["candidates"][0]["content"]["parts"][0]["text"]
return text.strip()
except (KeyError, IndexError) as e:
raise RuntimeError(f"Unexpected Gemini response: {result}") from e
async def process_single_async(
session: aiohttp.ClientSession,
s3_client,
img: dict,
dry_run: bool = False,
use_replicate: bool = False,
) -> Tuple[str, str, Optional[str]]:
"""Process a single image and generate a prompt (async).
Returns:
Tuple of (image_id, status, prompt or error message)
"""
image_id = img["id"]
try:
# Download from S3
s3_path = img["s3_processed_path"]
if s3_path.startswith("s3://"):
parts = s3_path.replace("s3://", "").split("/", 1)
bucket = parts[0]
key = parts[1]
else:
bucket = config.S3_BUCKET
key = s3_path.lstrip("/")
if key.startswith("data/"):
key = key[5:]
response = s3_client.get_object(Bucket=bucket, Key=key)
image_bytes = response["Body"].read()
# Generate prompt (include caption if available)
caption = img.get("caption")
if use_replicate or USE_REPLICATE:
prompt = await generate_prompt_for_image_replicate_async(
session, image_bytes, category=img["category"], caption=caption
)
else:
prompt = await generate_prompt_for_image_async(
session, image_bytes, category=img["category"], caption=caption
)
if not dry_run:
storage.insert_image_prompt(image_id, prompt, model=GEMINI_MODEL)
return (image_id, "SUCCESS", prompt[:100] + "...")
except Exception as e:
return (image_id, "FAILED", str(e))
async def process_batch_async(
category: str = None,
limit: int = None,
dry_run: bool = False,
concurrency: int = 5,
group: int = None,
use_replicate: bool = False,
):
"""Process all validated images without prompts (async with concurrency).
Args:
category: Filter by category
limit: Max images to process
dry_run: If True, don't save to database
concurrency: Number of parallel requests
group: Filter by group (0, 1, 2, etc.)
use_replicate: If True, use Replicate API instead of direct Gemini
"""
s3_client = boto3.client("s3", region_name=config.S3_REGION)
# Get images without prompts
images = storage.get_images_without_prompt(
model=GEMINI_MODEL, category=category, group=group
)
if limit:
images = images[:limit]
backend = "Replicate" if use_replicate else "Gemini"
print(f"Processing {len(images)} images via {backend} (concurrency={concurrency})")
print("=" * 60)
success_count = 0
failed_count = 0
async with aiohttp.ClientSession() as session:
# Process in batches
for i in range(0, len(images), concurrency):
batch = images[i : i + concurrency]
batch_num = i // concurrency + 1
total_batches = (len(images) + concurrency - 1) // concurrency
print(f"\nBatch {batch_num}/{total_batches} ({len(batch)} images)")
# Create tasks for this batch
tasks = [
process_single_async(session, s3_client, img, dry_run, use_replicate)
for img in batch
]
# Run batch concurrently
results = await asyncio.gather(*tasks)
# Process results
for image_id, status, message in results:
if status == "SUCCESS":
success_count += 1
print(f" [{image_id}] {status}: {message}")
else:
failed_count += 1
print(f" [{image_id}] {status}: {message}")
print(f"\n{'=' * 60}")
print(f"Done! Success: {success_count}, Failed: {failed_count}")
def process_batch(
category: str = None,
limit: int = None,
dry_run: bool = False,
concurrency: int = 5,
group: int = None,
use_replicate: bool = False,
):
"""Process all validated images without prompts."""
asyncio.run(
process_batch_async(
category=category,
limit=limit,
dry_run=dry_run,
concurrency=concurrency,
group=group,
use_replicate=use_replicate,
)
)
def process_single(image_id: str, dry_run: bool = False, use_replicate: bool = False) -> str:
"""Process a single image and generate a prompt.
Args:
image_id: Image ID from database
dry_run: If True, don't save to database
use_replicate: If True, use Replicate API instead of direct Gemini
Returns:
Generated prompt
"""
# Get image from DB
img = storage.get_image_by_id(image_id)
if not img:
raise ValueError(f"Image not found: {image_id}")
s3_client = boto3.client("s3", region_name=config.S3_REGION)
async def run():
async with aiohttp.ClientSession() as session:
return await process_single_async(session, s3_client, img, dry_run, use_replicate)
image_id, status, message = asyncio.run(run())
if status == "SUCCESS":
print(f"Generated prompt: {message}")
else:
print(f"Failed: {message}")
return message
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate image prompts with Gemini")
subparsers = parser.add_subparsers(dest="command")
# Single image
single_parser = subparsers.add_parser("single", help="Process single image")
single_parser.add_argument("image_id", help="Image ID")
single_parser.add_argument("--dry-run", "-n", action="store_true")
single_parser.add_argument("--replicate", "-r", action="store_true", help="Use Replicate API instead of direct Gemini")
# Batch processing
batch_parser = subparsers.add_parser("batch", help="Process batch of images")
batch_parser.add_argument("--category", "-c", help="Filter by category")
batch_parser.add_argument("--limit", "-l", type=int, help="Max images")
batch_parser.add_argument("--dry-run", "-n", action="store_true")
batch_parser.add_argument(
"--concurrency",
"-p",
type=int,
default=5,
help="Number of parallel requests (default: 5)",
)
batch_parser.add_argument(
"--group", "-g", type=int, help="Only process images in this group (0, 1, 2, etc.)"
)
batch_parser.add_argument("--replicate", "-r", action="store_true", help="Use Replicate API instead of direct Gemini")
# List images with prompts
list_parser = subparsers.add_parser("list", help="List images with prompts")
list_parser.add_argument("--category", "-c", help="Filter by category")
list_parser.add_argument("--limit", "-l", type=int, default=10)
args = parser.parse_args()
if args.command == "single":
process_single(args.image_id, dry_run=args.dry_run, use_replicate=args.replicate)
elif args.command == "batch":
process_batch(
category=args.category,
limit=args.limit,
dry_run=args.dry_run,
concurrency=args.concurrency,
group=args.group,
use_replicate=args.replicate,
)
elif args.command == "list":
images = storage.get_images_with_prompt(
model=GEMINI_MODEL, category=args.category
)
for img in images[: args.limit]:
print(f"\n{img['id']} ({img['category']}):")
print(f" {img['prompt'][:200]}...")
else:
parser.print_help()