Workpam commited on
Commit
06377e3
·
verified ·
1 Parent(s): 7b0058e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -45
app.py CHANGED
@@ -3,74 +3,42 @@ from transformers import pipeline
3
  from PIL import Image
4
 
5
  # --- Setup local image-to-text pipeline ---
6
- # No authentication or gated models required
7
  MODEL_NAME = "nlpconnect/vit-gpt2-image-captioning"
8
  captioner = pipeline(
9
  "image-to-text",
10
- model=MODEL_NAME,
11
- return_full_text=False
12
  )
13
 
14
  # --- Caption generation function ---
15
  def generate_alt_text(files, max_length):
16
- """
17
- Generate concise, descriptive alt text for uploaded images.
18
-
19
- Args:
20
- files (List[UploadedFile]): List of uploaded image files
21
- max_length (int): Maximum length of the generated caption
22
-
23
- Returns:
24
- str: Alt text for each image, separated by blank lines
25
- """
26
  captions = []
27
  for f in files or []:
28
- # Load image
29
  img = Image.open(f.name).convert("RGB")
30
- # Generate caption
31
- result = captioner(img, max_length=int(max_length), num_beams=4)
32
- text = result[0]["generated_text"].strip().capitalize()
33
  captions.append(text)
34
  return "\n\n".join(captions)
35
 
36
  # --- Gradio interface ---
37
- with gr.Blocks(title="Local Vit-GPT2 Image Captioning") as demo:
38
- gr.Markdown("# Local Vit-GPT2 Image Captioning Demo")
39
- gr.Markdown(
40
- f"Using **{MODEL_NAME}** — no API keys or gated access needed"
41
- )
42
 
43
  with gr.Row():
44
- file_input = gr.File(
45
- label="Upload Images",
46
- file_count="multiple",
47
- file_types=["image"]
48
- )
49
- max_length = gr.Slider(
50
- minimum=16,
51
- maximum=128,
52
- step=16,
53
- value=64,
54
- label="Max Caption Length"
55
- )
56
  generate_btn = gr.Button("Generate Captions")
57
- clear_btn = gr.Button("Clear All")
58
 
59
  gallery = gr.Gallery(label="Image Preview", columns=4)
60
- output = gr.Textbox(
61
- label="Generated Alt Texts",
62
- lines=10,
63
- show_copy_button=True
64
- )
65
 
66
- # Update gallery when files are selected
67
  file_input.change(
68
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
69
- inputs=file_input,
70
- outputs=gallery
71
  )
72
 
73
- # Generate captions on button click
74
  generate_btn.click(
75
  fn=generate_alt_text,
76
  inputs=[file_input, max_length],
@@ -78,7 +46,6 @@ with gr.Blocks(title="Local Vit-GPT2 Image Captioning") as demo:
78
  queue=True
79
  )
80
 
81
- # Clear all fields
82
  clear_btn.click(
83
  fn=lambda: ([], 64, [], ""),
84
  outputs=[file_input, max_length, gallery, output]
 
3
  from PIL import Image
4
 
5
  # --- Setup local image-to-text pipeline ---
 
6
  MODEL_NAME = "nlpconnect/vit-gpt2-image-captioning"
7
  captioner = pipeline(
8
  "image-to-text",
9
+ model=MODEL_NAME
 
10
  )
11
 
12
  # --- Caption generation function ---
13
  def generate_alt_text(files, max_length):
 
 
 
 
 
 
 
 
 
 
14
  captions = []
15
  for f in files or []:
 
16
  img = Image.open(f.name).convert("RGB")
17
+ # Only pass max_length and num_beams here
18
+ out = captioner(img, max_length=int(max_length), num_beams=4)
19
+ text = out[0]["generated_text"].strip().capitalize()
20
  captions.append(text)
21
  return "\n\n".join(captions)
22
 
23
  # --- Gradio interface ---
24
+ with gr.Blocks(title="Vit‑GPT2 Image Captioning") as demo:
25
+ gr.Markdown("# Vit‑GPT2 Image Captioning Demo")
26
+ gr.Markdown(f"Using **{MODEL_NAME}** — no special kwargs on pipeline creation")
 
 
27
 
28
  with gr.Row():
29
+ file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
30
+ max_length = gr.Slider(16, 128, value=64, step=16, label="Max Caption Length")
 
 
 
 
 
 
 
 
 
 
31
  generate_btn = gr.Button("Generate Captions")
32
+ clear_btn = gr.Button("Clear All")
33
 
34
  gallery = gr.Gallery(label="Image Preview", columns=4)
35
+ output = gr.Textbox(label="Generated Captions", lines=10, show_copy_button=True)
 
 
 
 
36
 
 
37
  file_input.change(
38
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
39
+ inputs=file_input, outputs=gallery
 
40
  )
41
 
 
42
  generate_btn.click(
43
  fn=generate_alt_text,
44
  inputs=[file_input, max_length],
 
46
  queue=True
47
  )
48
 
 
49
  clear_btn.click(
50
  fn=lambda: ([], 64, [], ""),
51
  outputs=[file_input, max_length, gallery, output]