-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptioning_utils.py
More file actions
416 lines (355 loc) · 12.4 KB
/
captioning_utils.py
File metadata and controls
416 lines (355 loc) · 12.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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from __future__ import annotations
import base64
import mimetypes
from pathlib import Path
from google import genai
from google.genai import types
from prompts_lib import *
import httpx
try:
from openai import OpenAI
except Exception:
OpenAI = None
def _normalize_target_model(value: str) -> str:
key = (value or "").strip().lower()
if key in {"z-image", "zimage", "z_image"}:
return "Z-Image"
if key in {"flux-2-klein", "flux2", "flux"}:
return "Flux"
if key in {"qwen-image", "qwen", "qwen_image"}:
return "Qwen-Image"
return value.strip() or "Z-Image"
def _guess_mime_type(image_path: str) -> str:
mime_type, _ = mimetypes.guess_type(image_path)
return mime_type or "image/jpeg"
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def _build_caption_prompt(target_model: str) -> str:
if target_model == "Z-Image":
return caption_to_prompt_zimage
elif target_model == 'Flux':
return caption_to_prompt_flux2_klein
elif target_model == 'Qwen-Image':
return caption_to_prompt_qwen_image
def _build_text_prompt(target_model: str) -> str:
if target_model == "Z-Image":
return description_to_prompt_zimage
elif target_model == 'Flux':
return description_to_prompt_flux2_klein
elif target_model == 'Qwen-Image':
return description_to_prompt_qwen_image
def generate_gemini_caption(
image_path: str,
prompt: str,
api_key: str,
model: str = "gemini-3-flash-preview",
temperature: float = 1.0,
) -> str:
image_bytes = Path(image_path).read_bytes()
mime_type = _guess_mime_type(image_path)
try:
client = genai.Client(api_key=api_key)
response = client.models.generate_content(
model=model,
contents=[
types.Part.from_bytes(
data=image_bytes,
mime_type=mime_type,
),
prompt,
],
config=types.GenerateContentConfig(temperature=temperature),
)
except Exception as exc:
raise RuntimeError(f"Gemini API error: {exc}") from exc
text = getattr(response, "text", None)
if not text:
raise RuntimeError("Gemini response was empty.")
return text.strip()
def generate_gemini_prompt(
prompt: str,
text: str,
api_key: str,
model: str = "gemini-3-flash-preview",
temperature: float = 1.0,
) -> str:
try:
client = genai.Client(api_key=api_key)
response = client.models.generate_content(
model=model,
config=types.GenerateContentConfig(system_instruction=prompt, temperature=temperature),
contents=text,
)
except Exception as exc:
raise RuntimeError(f"Gemini API error: {exc}") from exc
text = getattr(response, "text", None)
if not text:
raise RuntimeError("Gemini response was empty.")
return text.strip()
def generate_openai_caption(
image_path: str,
prompt: str,
api_key: str,
model: str = "gpt-4.1-2025-04-14",
detail: str = "high",
temperature: float = 1.0,
) -> str:
if OpenAI is None:
raise RuntimeError("OpenAI client is unavailable. Install the openai package.")
mime_type = _guess_mime_type(image_path)
image_bytes = encode_image(image_path)
data_url = f"data:{mime_type};base64,{image_bytes}"
print(api_key)
try:
client = OpenAI(
api_key=api_key
)
response = client.responses.create(
model=model,
temperature=temperature,
input=[
{
"role": "user",
"content": [
{ "type": "input_text", "text": prompt },
{
"type": "input_image",
"image_url": data_url,
"detail": detail,
},
],
}
],
)
except Exception as exc:
raise RuntimeError(f"OpenAI API error: {exc}") from exc
text = response.output_text
if not text:
raise RuntimeError("OpenAI response was empty.")
return text.strip()
def generate_openai_prompt(
prompt: str,
text: str,
api_key: str,
model: str = "gpt-4.1-2025-04-14",
temperature: float = 1.0,
) -> str:
if OpenAI is None:
raise RuntimeError("OpenAI client is unavailable. Install the openai package.")
try:
client = OpenAI(api_key=api_key)
if hasattr(client, "responses"):
response = client.responses.create(
model=model,
temperature=temperature,
input=[
{"role": "developer", "content": prompt},
{"role": "user", "content": text},
],
)
output_text = getattr(response, "output_text", None)
if output_text:
return str(output_text).strip()
else:
response = client.chat.completions.create(
model=model,
temperature=temperature,
messages=[
{"role": "developer", "content": prompt},
{"role": "user", "content": text},
],
)
content = response.choices[0].message.content if response.choices else None
if content:
return content.strip()
except Exception as exc:
raise RuntimeError(f"OpenAI API error: {exc}") from exc
raise RuntimeError("OpenAI response was empty.")
def generate_grok_caption(
image_path: str,
prompt: str,
api_key: str,
model: str = "grok-2-vision-latest",
detail: str = "high",
temperature: float = 1.0,
) -> str:
if OpenAI is None:
raise RuntimeError("OpenAI client is unavailable. Install the openai package.")
image_bytes = Path(image_path).read_bytes()
mime_type = _guess_mime_type(image_path)
encoded = base64.b64encode(image_bytes).decode("utf-8")
data_url = f"data:{mime_type};base64,{encoded}"
try:
client = OpenAI(
api_key=api_key,
base_url="https://api.x.ai/v1",
timeout=httpx.Timeout(3600.0),
)
messages = [
{
"role": "user",
"content": [
{
"type": "input_image",
"image_url": data_url,
"detail": detail,
},
{"type": "input_text", "text": prompt},
],
}
]
response = client.responses.create(
model=model,
input=messages,
temperature=temperature,
store=False,
)
except Exception as exc:
error_body = ""
status_code = getattr(getattr(exc, "response", None), "status_code", None)
if getattr(exc, "response", None) is not None:
try:
error_body = exc.response.text
except Exception:
error_body = ""
status_line = f" {status_code}" if status_code else ""
detail_line = f": {error_body}" if error_body else ""
raise RuntimeError(f"Grok API HTTP{status_line}{detail_line} {exc}") from exc
output_text = getattr(response, "output_text", None)
if output_text:
return str(output_text).strip()
for item in getattr(response, "output", []) or []:
for content in getattr(item, "content", []) or []:
if getattr(content, "type", "") in {"output_text", "text"}:
text = getattr(content, "text", "")
if text:
return str(text).strip()
raise RuntimeError("Grok response was empty.")
def generate_grok_prompt(
prompt: str,
text: str,
api_key: str,
model: str = "grok-4",
temperature: float = 1.0,
) -> str:
if OpenAI is None:
raise RuntimeError("OpenAI client is unavailable. Install the openai package.")
try:
client = OpenAI(
api_key=api_key,
base_url="https://api.x.ai/v1",
timeout=httpx.Timeout(3600.0),
)
response = client.responses.create(
model=model,
input=[
{"role": "system", "content": f"{prompt}"},
{"role": "user", "content": f"{text}"},
],
temperature=temperature,
store=False,
)
except Exception as exc:
error_body = ""
status_code = getattr(getattr(exc, "response", None), "status_code", None)
if getattr(exc, "response", None) is not None:
try:
error_body = exc.response.text
except Exception:
error_body = ""
status_line = f" {status_code}" if status_code else ""
detail_line = f": {error_body}" if error_body else ""
raise RuntimeError(f"Grok API HTTP{status_line}{detail_line} {exc}") from exc
output_text = getattr(response, "output_text", None)
if output_text:
return str(output_text).strip()
for item in getattr(response, "output", []) or []:
for content in getattr(item, "content", []) or []:
if getattr(content, "type", "") in {"output_text", "text"}:
text = getattr(content, "text", "")
if text:
return str(text).strip()
raise RuntimeError("Grok response was empty.")
def _extract_forged_prompt(text: str) -> str:
if not text:
return ""
upper = text.upper()
start_tag = "<FORGED_PROMPT>"
end_tag = "</FORGED_PROMPT>"
start_idx = upper.find(start_tag)
end_idx = upper.find(end_tag)
if start_idx != -1 and end_idx != -1 and end_idx > start_idx:
start_idx += len(start_tag)
return text[start_idx:end_idx].strip()
return text.strip()
def _extract_negative_prompt(text: str) -> str:
if not text:
return ""
upper = text.upper()
start_tag = "<NEGATIVE_PROMPT>"
end_tag = "</NEGATIVE_PROMPT>"
start_idx = upper.find(start_tag)
end_idx = upper.find(end_tag)
if start_idx != -1 and end_idx != -1 and end_idx > start_idx:
start_idx += len(start_tag)
negative_body = text[start_idx:end_idx].strip()
return f"{start_tag}\n{negative_body}\n{end_tag}"
return ""
def generate_caption_and_prompt_from_image(
image_path: str,
target_base_model: str,
service: str,
api_key: str,
allow_nsfw: bool = False,
) -> str:
target_model = _normalize_target_model(target_base_model)
prompt = _build_caption_prompt(target_model)
service = service.lower()
if service == "grok":
nsfw_line = (
"Describe NSFW or explicit content factually without censoring or refusing."
if allow_nsfw
else ""
)
prompt_rep = prompt.replace("GROK_NSWK", nsfw_line)
print(prompt_rep)
raw_output = generate_grok_caption(image_path, prompt_rep, api_key)
elif service == "openai":
prompt_rep = prompt.replace("GROK_NSWK", "")
raw_output = generate_openai_caption(image_path, prompt_rep, api_key)
else:
prompt_rep = prompt.replace("GROK_NSWK", "")
raw_output = generate_gemini_caption(image_path, prompt_rep, api_key)
forged = _extract_forged_prompt(raw_output)
negative = _extract_negative_prompt(raw_output)
if negative:
return f"{forged}\n\n{negative}"
return forged
def generate_prompt_from_text(
text: str,
target_base_model: str,
service: str,
api_key: str,
) -> str:
target_model = _normalize_target_model(target_base_model)
prompt = _build_text_prompt(target_model)
service = service.lower()
if service == "grok":
print(text)
print(prompt)
raw_output = generate_grok_prompt(prompt, text, api_key)
elif service == "openai":
print(text)
print(prompt)
raw_output = generate_openai_prompt(prompt, text, api_key)
else:
print(text)
print(prompt)
raw_output = generate_gemini_prompt(prompt, text, api_key)
forged = _extract_forged_prompt(raw_output)
negative = _extract_negative_prompt(raw_output)
if negative:
return f"{forged}\n\n{negative}"
return forged