Workpam commited on
Commit
d1406f4
·
verified ·
1 Parent(s): bf892b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -19
app.py CHANGED
@@ -1,35 +1,49 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
 
4
 
5
  # --- Model Setup ---
6
- # Balanced: Lightweight, fast, and detailed
7
- MODEL_NAME = "microsoft/git-large-coco"
 
8
 
9
- # Initialize the image-to-text pipeline
10
  captioner = pipeline(
11
  "image-to-text",
12
- model=MODEL_NAME
 
 
13
  )
14
 
15
  # --- Core Function ---
16
  def generate_alt_text(files, max_new_tokens):
17
  """
18
- Generate captions for a list of uploaded image files using the selected pipeline.
19
  """
20
  captions = []
21
  for f in files or []:
 
22
  img = Image.open(f.name).convert("RGB")
23
- # Generate caption with token limit
24
- result = captioner(img, max_new_tokens=int(max_new_tokens))
25
- text = result[0]["generated_text"].strip().capitalize()
 
 
 
 
 
 
 
26
  captions.append(text)
27
  return "\n\n".join(captions)
28
 
29
  # --- Gradio Interface ---
30
- with gr.Blocks(title="Fast & Detailed Alt Text Generator") as demo:
31
- gr.Markdown("# Fast & Detailed Alt Text Generator")
32
- gr.Markdown(f"**Model:** {MODEL_NAME} (1–2B params, optimized for speed & detail)")
 
 
33
 
34
  with gr.Row():
35
  file_input = gr.File(
@@ -39,10 +53,10 @@ with gr.Blocks(title="Fast & Detailed Alt Text Generator") as demo:
39
  )
40
  max_new_tokens = gr.Slider(
41
  minimum=16,
42
- maximum=256,
43
  step=16,
44
- value=64,
45
- label="Max New Tokens"
46
  )
47
  generate_btn = gr.Button("Generate Alt Text")
48
  clear_btn = gr.Button("Clear All")
@@ -54,14 +68,14 @@ with gr.Blocks(title="Fast & Detailed Alt Text Generator") as demo:
54
  show_copy_button=True
55
  )
56
 
57
- # Preview images in gallery
58
  file_input.change(
59
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
60
  inputs=file_input,
61
  outputs=gallery
62
  )
63
 
64
- # Generate alt text
65
  generate_btn.click(
66
  fn=generate_alt_text,
67
  inputs=[file_input, max_new_tokens],
@@ -69,10 +83,10 @@ with gr.Blocks(title="Fast & Detailed Alt Text Generator") as demo:
69
  queue=True
70
  )
71
 
72
- # Clear inputs/outputs
73
  clear_btn.click(
74
- fn=lambda: ([], 64, [], ""),
75
  outputs=[file_input, max_new_tokens, gallery, output]
76
  )
77
 
78
- demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
4
+ import torch
5
 
6
  # --- Model Setup ---
7
+ # Qwen2.5-VL-7B-Instruct for rich, instruction-tuned image-to-text
8
+ MODEL_NAME = "Qwen/Qwen2.5-VL-7B-Instruct"
9
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
+ # Initialize the pipeline with trust_remote_code to allow Qwen custom layers
12
  captioner = pipeline(
13
  "image-to-text",
14
+ model=MODEL_NAME,
15
+ device=0 if DEVICE == "cuda" else -1,
16
+ trust_remote_code=True
17
  )
18
 
19
  # --- Core Function ---
20
  def generate_alt_text(files, max_new_tokens):
21
  """
22
+ Generate detailed, instruction-tuned alt text for uploaded images.
23
  """
24
  captions = []
25
  for f in files or []:
26
+ # Load and preprocess image
27
  img = Image.open(f.name).convert("RGB")
28
+ # Generate caption using Qwen2.5-VL
29
+ result = captioner(
30
+ img,
31
+ max_new_tokens=int(max_new_tokens),
32
+ do_sample=False
33
+ )
34
+ text = result[0]["generated_text"].strip()
35
+ # Capitalize first letter
36
+ if text:
37
+ text = text[0].upper() + text[1:]
38
  captions.append(text)
39
  return "\n\n".join(captions)
40
 
41
  # --- Gradio Interface ---
42
+ with gr.Blocks(title="Qwen2.5-VL-7B-Instruct Alt Text Generator") as demo:
43
+ gr.Markdown("# Qwen2.5-VL-7B-Instruct Alt Text Generator")
44
+ gr.Markdown(
45
+ f"**Model:** {MODEL_NAME} — Instruction-tuned for detailed, accurate descriptions"
46
+ )
47
 
48
  with gr.Row():
49
  file_input = gr.File(
 
53
  )
54
  max_new_tokens = gr.Slider(
55
  minimum=16,
56
+ maximum=512,
57
  step=16,
58
+ value=128,
59
+ label="Max New Tokens"
60
  )
61
  generate_btn = gr.Button("Generate Alt Text")
62
  clear_btn = gr.Button("Clear All")
 
68
  show_copy_button=True
69
  )
70
 
71
+ # Update gallery preview on file selection
72
  file_input.change(
73
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
74
  inputs=file_input,
75
  outputs=gallery
76
  )
77
 
78
+ # Bind generation function to button
79
  generate_btn.click(
80
  fn=generate_alt_text,
81
  inputs=[file_input, max_new_tokens],
 
83
  queue=True
84
  )
85
 
86
+ # Clear all fields
87
  clear_btn.click(
88
+ fn=lambda: ([], 128, [], ""),
89
  outputs=[file_input, max_new_tokens, gallery, output]
90
  )
91
 
92
+ demo.launch(server_name="0.0.0.0", server_port=7860)