-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
690 lines (566 loc) · 22.3 KB
/
app.py
File metadata and controls
690 lines (566 loc) · 22.3 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
import os
import torch
import gradio as gr
from transformers import BitsAndBytesConfig
from qwen_vl_utils import process_vision_info # Ensure this is installed
from PIL import Image
import time
IMAGE_EXTENSIONS = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.webp')
VIDEO_EXTENSIONS = ('.mp4', '.mov', '.avi', '.webm', '.mkv', ".gif", ".flv")
DEFAULT_PROMPT = "Describe this media."
DEFAULT_MODEL_ID = "Qwen/Qwen3-VL-8B-Instruct"
DEFAULT_QUANT = "8-bit" # "None" | "8-bit" | "4-bit"
DEFAULT_ATTN = "eager" if os.name == "nt" else "flash_attention_2"
PRESETS = {
"auto": {
"min_pixels": 256 * 28 * 28,
"max_pixels": 896 * 28 * 28,
},
"fast": {
"resized_height": 392,
"resized_width": 392,
},
"high": {
"resized_height": 728,
"resized_width": 728,
},
}
AVAILABLE_MODELS = [
"Qwen/Qwen3-VL-4B-Instruct",
"Qwen/Qwen3-VL-8B-Instruct",
"Qwen/Qwen2.5-VL-3B-Instruct",
"Qwen/Qwen2.5-VL-7B-Instruct",
"Custom...",
]
# Globals
processor = None
current_model_id = DEFAULT_MODEL_ID
current_quant = DEFAULT_QUANT
model = None
should_abort = False
ui_e = {}
def build_bnb_config(quant_choice: str):
if quant_choice == "8-bit":
return BitsAndBytesConfig(load_in_8bit=True)
if quant_choice == "4-bit":
return BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.float16)
return None
def pick_model_class(model_id: str):
from transformers import AutoModelForVision2Seq
try:
if "Qwen3-VL" in model_id:
from transformers import Qwen3VLForConditionalGeneration
return Qwen3VLForConditionalGeneration
if "Qwen2.5-VL" in model_id or "Qwen2_5-VL" in model_id:
from transformers import Qwen2_5_VLForConditionalGeneration
return Qwen2_5_VLForConditionalGeneration
except Exception:
pass
return AutoModelForVision2Seq
def unload_model():
print ("[DEBUG] Unload currently loaded model")
global model, processor
try:
del model
except Exception:
pass
try:
del processor
except Exception:
pass
model = None
processor = None
if torch.cuda.is_available():
torch.cuda.empty_cache()
def load_selected_model(model_id: str, quant_choice: str, attn_impl: str = DEFAULT_ATTN):
"""
Loads (or reloads) the model + processor with chosen quantization and attention impl.
Falls back to 'eager' if flash_attention_2 fails.
"""
print("[DEBUG] Loading selected model:", model_id)
global model, processor, current_model_id, current_quant
unload_model()
kwargs = {
"dtype": torch.float16,
"device_map": "auto",
"attn_implementation": attn_impl,
}
bnb = build_bnb_config(quant_choice)
if bnb is not None:
kwargs["quantization_config"] = bnb
ModelCls = pick_model_class(model_id)
try:
model = ModelCls.from_pretrained(model_id, **kwargs)
except Exception:
if attn_impl == "flash_attention_2":
kwargs["attn_implementation"] = "eager"
model = ModelCls.from_pretrained(model_id, **kwargs)
else:
raise
from transformers import AutoProcessor as _AP
processor = _AP.from_pretrained(model_id, use_fast=True)
current_model_id = model_id
current_quant = quant_choice
return get_model_info()
def toggle_controls(disabled=True):
updates = {}
for name, component in ui_e.items():
if name == "abort_button":
updates[name] = gr.update(interactive=not disabled)
else:
updates[name] = gr.update(interactive=not disabled if component.visible else False)
return updates
def disable_controls_dict():
return [toggle_controls(disabled=True)[k] for k in control_keys]
def enable_controls_dict():
return [toggle_controls(disabled=False)[k] for k in control_keys]
control_keys = [
"model_dropdown",
"quant_dropdown",
"attn_dropdown",
"load_button",
"reset_button",
"start_button",
"abort_button",
"folder_input",
"prompt_input",
"skip_existing_checkbox",
"max_tokens_slider",
"summary_mode",
"one_sentence_mode",
"retain_preview_checkbox",
"resolution_mode",
"status_output",
]
def finish_process():
updates = enable_controls_dict()
abort_index = control_keys.index("abort_button")
updates[abort_index] = gr.update(interactive=False)
return updates
def abort_process():
global should_abort
should_abort = True
updates = enable_controls_dict()
abort_index = control_keys.index("abort_button")
status_index = control_keys.index("status_output")
updates[abort_index] = gr.update(interactive=False)
updates[status_index] = gr.update(value="⛔ Aborting process...")
return updates
def start_process():
updates = disable_controls_dict()
abort_index = control_keys.index("abort_button")
updates[abort_index] = gr.update(interactive=True)
return updates
def serialize_for_debug(obj):
if isinstance(obj, dict):
return {k: serialize_for_debug(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [serialize_for_debug(i) for i in obj]
elif isinstance(obj, Image.Image):
return f"<Image {obj.size} {obj.mode}>"
else:
return obj
def get_model_info():
global model
if model is None:
return "Model not loaded.", "N/A", "N/A", "N/A", "N/A"
model_name = model.config._name_or_path if hasattr(model.config, "_name_or_path") else "Unknown Model"
device = "CUDA" if torch.cuda.is_available() else "CPU"
if torch.cuda.is_available():
vram_used = f"{torch.cuda.memory_allocated() / 1e9:.2f} GB"
vram_total = f"{torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB"
else:
vram_used, vram_total = "N/A", "N/A"
dtype = str(next(model.parameters()).dtype)
return model_name, device, f"{vram_used} / {vram_total}", dtype, str(model.config)
print("[DEBUG] Cuda available:", torch.cuda.is_available())
def generate_caption(media_path, prompt, max_tokens, summary_mode=False, one_sentence_mode=False, resolution_mode="auto"):
global processor, model
assert model is not None, "Model must be loaded before generating captions."
assert processor is not None, "Processor must be loaded before generating captions."
# Edit custom prompts here if you want
if summary_mode and one_sentence_mode:
prompt += " Give a one-sentence summary of the scene."
elif summary_mode:
prompt += " Give a short summary of the scene."
elif one_sentence_mode:
prompt += " Describe this image in one sentence."
ext = os.path.splitext(media_path)[-1].lower()
is_video = ext in VIDEO_EXTENSIONS
content_type = "video" if is_video else "image"
if is_video:
media_data = media_path
else:
media_data = Image.open(media_path).convert("RGB")
content_block = {"type": content_type, content_type: media_data}
if not is_video:
if resolution_mode == "auto":
content_block["min_pixels"] = 256 * 28 * 28
content_block["max_pixels"] = 896 * 28 * 28
elif resolution_mode == "auto_high":
content_block["min_pixels"] = 256 * 28 * 28
content_block["max_pixels"] = 1280 * 28 * 28
elif resolution_mode == "fast":
content_block["resized_height"] = 392
content_block["resized_width"] = 392
elif resolution_mode == "high":
content_block["resized_height"] = 728
content_block["resized_width"] = 728
messages = [
{
"role": "user",
"content": [
content_block,
{"type": "text", "text": prompt},
],
}
]
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
vision_info = process_vision_info(messages)
assert isinstance(vision_info, tuple), "process_vision_info did not return a tuple"
if len(vision_info) == 3:
image_inputs, video_inputs, _ = vision_info
elif len(vision_info) == 2:
image_inputs, video_inputs = vision_info # type: ignore
else:
raise ValueError(f"Error: Expected 2 or 3 values from process_vision_info, but got {len(vision_info)}.")
inputs = processor(
text=[text],
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt",
).to("cuda")
with torch.no_grad():
generated_ids = model.generate(**inputs, max_new_tokens=max_tokens)
generated_ids_trimmed = [
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
caption = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
return caption
def is_image_file(filename):
return filename.lower().endswith(IMAGE_EXTENSIONS)
def is_video_file(filename):
return filename.lower().endswith(VIDEO_EXTENSIONS)
def build_final_prompt(user_prompt, summary, one_sentence):
parts = [user_prompt.strip()]
if summary:
parts.append("Please provide a short summary.")
if one_sentence:
parts.append("Keep the description to one sentence.")
return " ".join(parts)
def process_folder(folder_path, prompt, skip_existing, max_tokens, summary_mode, one_sentence_mode, retain_preview, resolution_mode):
global should_abort, current_model_id, current_quant
processed_media = 0
skipped_media = 0
failed_media = 0
last_media_to_show = None
last_caption = ""
last_media_name_markdown = ""
elapsed_str = ""
print("[DEBUG] starting folder processing...:")
print("[DEBUG] using model: ", current_model_id)
print("[DEBUG] model quantization: ", current_quant)
print("[DEBUG] folder_path: ", folder_path)
print ("[DEBUG] prompt: ", prompt)
print("[DEBUG] skip_existing: ", skip_existing)
print("[DEBUG] max_tokens: ", max_tokens)
print("[DEBUG] summary_mode: ", summary_mode)
print ("[DEBUG] one_sentence_mode: ", one_sentence_mode)
print("[DEBUG] retain_preview: ", retain_preview)
print("[DEBUG] resolution_mode: ", resolution_mode)
print("[DEBUG] should_abort: ", should_abort)
if not folder_path.strip():
control_updates = finish_process()
yield (
"⚠️ Please enter a valid folder path.",
None,
None,
"No media to process.",
0,
"",
*control_updates
)
return
if not os.path.exists(folder_path):
control_updates = finish_process()
yield (
f"❌ Folder not found: {folder_path}",
None,
None,
"No media to process.",
0,
"",
*control_updates
)
return
media_files = []
for root, _, files in os.walk(folder_path):
for file in files:
if is_image_file(file) or is_video_file(file):
media_files.append(os.path.join(root, file))
total_media = len(media_files)
if total_media == 0:
control_updates = finish_process()
yield (
"📂 No media found in the folder or subfolders.",
None,
None,
"No media to process.",
0,
"",
*control_updates
)
return
start_time = time.time()
for idx, media_path in enumerate(media_files):
if should_abort:
should_abort = False
status_index = control_keys.index("status_output")
abort_index = control_keys.index("abort_button")
control_updates = enable_controls_dict()
control_updates[status_index] = gr.update(value="⛔ Aborted by user.")
control_updates[abort_index] = gr.update(interactive=False)
yield (
"⛔ Aborted by user.",
None,
None,
"Aborted.",
0,
"",
*control_updates
)
return
try:
txt_filename = os.path.splitext(os.path.basename(media_path))[0] + ".txt"
txt_path = os.path.join(os.path.dirname(media_path), txt_filename)
rel_path = os.path.relpath(media_path, folder_path)
media_name_markdown = f"**File:** `{rel_path}`"
if skip_existing and os.path.exists(txt_path):
progress = int(((idx + 1) / total_media) * 100)
elapsed = int(time.time() - start_time)
elapsed_str = f"{elapsed//60:02d}:{elapsed%60:02d}"
skipped_media += 1
control_updates = start_process()
yield (
f"⏭️ Skipped {idx+1}/{total_media}: {rel_path} (already captioned)",
last_media_to_show if retain_preview else None,
last_media_name_markdown if retain_preview else None,
last_caption if retain_preview else "Skipped (already captioned)",
progress,
elapsed_str,
*control_updates
)
continue
caption = generate_caption(media_path, prompt, max_tokens, summary_mode, one_sentence_mode, resolution_mode)
# Only generate previews for images
if is_image_file(media_path):
media_to_show = Image.open(media_path)
else:
media_to_show = None
with open(txt_path, "w", encoding="utf-8") as f:
f.write(caption)
progress = int(((idx + 1) / total_media) * 100)
elapsed = int(time.time() - start_time)
elapsed_str = f"{elapsed//60:02d}:{elapsed%60:02d}"
last_media_to_show = media_to_show
last_caption = caption
last_media_name_markdown = media_name_markdown
processed_media += 1
control_updates = start_process()
yield (
f"🖼️ Processing {idx+1}/{total_media}: {rel_path}",
media_to_show,
media_name_markdown,
caption,
progress,
elapsed_str,
*control_updates
)
except Exception as e:
failed_media += 1
print(f"[ERROR] Failed processing file {media_path}: {e}")
control_updates = start_process()
yield (
f"⚠️ Error processing {media_path}: {str(e)}",
None,
None,
"Error in captioning.",
0,
elapsed_str,
*control_updates
)
end_time = time.time()
total_time = f"{end_time - start_time:.2f}"
control_updates = finish_process()
yield (
f"✅ Processing complete! , processed {processed_media} media in {elapsed_str}, skipped {skipped_media} media. Failed to process {failed_media} media (inaccessible, unknown or broken file)",
last_media_to_show,
last_media_name_markdown,
last_caption,
None,
None,
*control_updates
)
def reset_prompt():
return DEFAULT_PROMPT
css = """
.generating {
border: none;
}
"""
with gr.Blocks(theme=gr.themes.Base(), css=css) as iface: # type: ignore
gr.Markdown("# Simple Captioner")
gr.Markdown("A simple media caption generator for images and video using **[Qwen2.5/3 VL Instruct](https://huggingface.co/Qwen/)**")
gr.Markdown("Supported image formats: png, jpg, jpeg, bmp, gif, webp")
gr.Markdown("Supported video formats: mp4, mov, avi, webm, mkv, gif, flv")
gr.Markdown("Written by [Olli S.](https://github.com/o-l-l-i)")
with gr.Accordion("⚙️ Model Settings", open=True):
ui_e["model_dropdown"] = model_dropdown = gr.Dropdown(
label="Model",
choices=AVAILABLE_MODELS,
value=DEFAULT_MODEL_ID,
allow_custom_value=False,
interactive=True,
info="Pick a model to use for captioning."
)
custom_model_box = gr.Textbox(
label="Custom Model ID (Hugging Face)",
placeholder="e.g. Qwen/Qwen3-VL-4B-Instruct or your-org/my-qwen3-checkpoint",
visible=False,
)
ui_e["quant_dropdown"] = quant_dropdown = gr.Radio(
label="Quantization",
choices=["None", "8-bit", "4-bit"],
value=DEFAULT_QUANT,
interactive=True,
info="Lower-bit quantization reduces VRAM, may slightly affect quality."
)
ui_e["attn_dropdown"] = attn_dropdown = gr.Radio(
label="Attention Implementation",
choices=["flash_attention_2", "eager"],
value=DEFAULT_ATTN,
interactive=True,
info="If FlashAttention isn't installed/working, choose 'eager'. Auto-fallback on load."
)
ui_e["load_button"] = gr.Button("📦 Load / Reload Model")
def _toggle_custom(choice):
return gr.update(visible=(choice == "Custom…"))
model_dropdown.change(_toggle_custom, inputs=[model_dropdown], outputs=[custom_model_box])
def _ui_load_model(sel, custom_id, quant, attn):
model_id = custom_id.strip() if sel == "Custom..." and custom_id and custom_id.strip() else sel
name, device, vram, dtype, cfg = load_selected_model(model_id, quant, attn)
status = f"✅ Loaded '{model_id}' with {quant} quantization ({attn})."
return status, name, device, vram, dtype, cfg
with gr.Accordion("⚙️ Model Information", open=False):
model_name_display = gr.Textbox(label="Model Name", interactive=False)
device_display = gr.Textbox(label="Device", interactive=False)
vram_display = gr.Textbox(label="VRAM Usage", interactive=False)
dtype_display = gr.Textbox(label="Torch Dtype", interactive=False)
config_display = gr.Textbox(label="Model Config", interactive=False, lines=4)
with gr.Row():
ui_e["folder_input"] = gr.Textbox(label="📁 Folder Path", placeholder="e.g. C:\\Users\\you\\Pictures\\input_images")
ui_e["prompt_input"] = gr.Textbox(label="Custom Prompt", value=DEFAULT_PROMPT)
ui_e["skip_existing_checkbox"] = gr.Checkbox(label="Skip already captioned media (.txt exists)", value=False)
with gr.Row():
gr.Markdown("### Prompt Controls")
gr.Markdown("""
- **Summary Mode**: Asks the model to summarize the media content briefly.
- **One-Sentence Mode**: Instructs the model to keep the caption to a single concise sentence.
""")
ui_e["summary_mode"] = gr.Checkbox(label="Summary Mode", value=False)
ui_e["one_sentence_mode"] = gr.Checkbox(label="One-Sentence Mode", value=False)
prompt_preview = gr.Textbox(
label="Final Prompt Preview",
lines=2,
interactive=False
)
with gr.Row():
ui_e["max_tokens_slider"] = gr.Slider(label="🧾 Max Tokens", minimum=32, maximum=512, value=128, step=16)
ui_e["resolution_mode"] = gr.Dropdown(
label="Image Resolution",
choices=["auto", "auto_high", "fast", "high"],
value="auto",
info="Choose the resolution mode for visual input."
)
with gr.Row():
ui_e["reset_button"] = gr.Button("🔄 Reset to Default Prompt")
ui_e["start_button"] = gr.Button("🚀 Start Processing", interactive=True)
ui_e["abort_button"] = gr.Button("⛔ Abort", interactive=False)
ui_e["status_output"] = gr.Textbox(label="Status", interactive=False)
progress_bar = gr.Slider(minimum=0, maximum=100, label="Progress", interactive=False)
time_display = gr.Textbox(label="⏱️ Time Taken (s)", interactive=False)
with gr.Row():
with gr.Column(scale=1):
media_output = gr.Image(label="Current Image", interactive=False)
media_name_markdown = gr.Markdown()
ui_e["retain_preview_checkbox"] = gr.Checkbox(
label="Retain preview on skip",
value=True
)
with gr.Column(scale=1):
caption_output = gr.Textbox(label="Generated Caption", interactive=False)
ui_e["prompt_input"].change(
fn=build_final_prompt,
inputs=[ui_e["prompt_input"], ui_e["summary_mode"], ui_e["one_sentence_mode"]],
outputs=[prompt_preview],
)
ui_e["summary_mode"].change(
fn=build_final_prompt,
inputs=[ui_e["prompt_input"], ui_e["summary_mode"], ui_e["one_sentence_mode"]],
outputs=[prompt_preview],
)
ui_e["one_sentence_mode"].change(
fn=build_final_prompt,
inputs=[ui_e["prompt_input"], ui_e["summary_mode"], ui_e["one_sentence_mode"]],
outputs=[prompt_preview],
)
ui_e["start_button"].click(
start_process,
inputs=[],
outputs=[ui_e[k] for k in control_keys]
)
ui_e["start_button"].click(
process_folder,
inputs=[
ui_e["folder_input"],
ui_e["prompt_input"],
ui_e["skip_existing_checkbox"],
ui_e["max_tokens_slider"],
ui_e["summary_mode"],
ui_e["one_sentence_mode"],
ui_e["retain_preview_checkbox"],
ui_e["resolution_mode"],
],
outputs=[
ui_e["status_output"],
media_output,
media_name_markdown,
caption_output,
progress_bar,
time_display,
*[ui_e[k] for k in control_keys]
]
)
ui_e["abort_button"].click(
fn=abort_process,
inputs=[],
outputs=[ui_e[k] for k in control_keys],
queue=False
)
ui_e["reset_button"].click(reset_prompt, inputs=[], outputs=[ui_e["prompt_input"]])
ui_e["start_button"].click(get_model_info, inputs=[], outputs=[model_name_display, device_display, vram_display, dtype_display, config_display])
ui_e["load_button"].click(
_ui_load_model,
inputs=[model_dropdown, custom_model_box, quant_dropdown, attn_dropdown],
outputs=[ui_e["status_output"], model_name_display, device_display, vram_display, dtype_display, config_display],
)
gr.Blocks.load(
iface,
get_model_info,
inputs=[],
outputs=[model_name_display, device_display, vram_display, dtype_display, config_display]
)
iface.launch(share=False, show_api=False)