Workpam commited on
Commit
2556988
·
verified ·
1 Parent(s): 9241fc0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -2,31 +2,40 @@ import gradio as gr
2
  from transformers import pipeline
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_new_tokens):
14
  """
15
- Generate concise, descriptive alt text for uploaded images.
 
16
  """
17
  captions = []
18
  for f in files or []:
19
  img = Image.open(f.name).convert("RGB")
20
- # Generate caption with specified max_new_tokens and num_beams
21
- result = captioner(img, max_new_tokens=int(max_new_tokens), num_beams=4)
 
 
 
 
 
 
22
  text = result[0]["generated_text"].strip().capitalize()
23
  captions.append(text)
24
  return "\n\n".join(captions)
25
 
26
- # --- Gradio interface ---
27
- with gr.Blocks(title="Local Vit-GPT2 Image Captioning") as demo:
28
- gr.Markdown("# Local Vit-GPT2 Image Captioning Demo")
29
- gr.Markdown(f"Using **{MODEL_NAME}** — no API keys or gated access needed")
30
 
31
  with gr.Row():
32
  file_input = gr.File(
@@ -36,26 +45,22 @@ with gr.Blocks(title="Local Vit-GPT2 Image Captioning") as demo:
36
  )
37
  max_new_tokens = gr.Slider(
38
  minimum=16,
39
- maximum=128,
40
  step=16,
41
  value=64,
42
  label="Max New Tokens"
43
  )
44
- generate_btn = gr.Button("Generate Captions")
45
- clear_btn = gr.Button("Clear All")
46
 
47
  gallery = gr.Gallery(label="Image Preview", columns=4)
48
- output = gr.Textbox(
49
- label="Generated Alt Texts",
50
- lines=10,
51
- show_copy_button=True
52
- )
53
 
54
- # Preview images in gallery
55
  file_input.change(
56
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
57
  inputs=file_input,
58
- outputs=gallery
59
  )
60
 
61
  # Generate captions
@@ -66,7 +71,7 @@ with gr.Blocks(title="Local Vit-GPT2 Image Captioning") as demo:
66
  queue=True
67
  )
68
 
69
- # Clear all fields
70
  clear_btn.click(
71
  fn=lambda: ([], 64, [], ""),
72
  outputs=[file_input, max_new_tokens, gallery, output]
 
2
  from transformers import pipeline
3
  from PIL import Image
4
 
5
+ # --- Model Setup ---
6
+ MODEL_NAME = "microsoft/Florence-2-large"
7
+ # Florence uses custom code under the hood, so we set trust_remote_code=True
8
  captioner = pipeline(
9
  "image-to-text",
10
+ model=MODEL_NAME,
11
+ trust_remote_code=True,
12
  )
13
 
14
+ # --- Caption Generation ---
15
  def generate_alt_text(files, max_new_tokens):
16
  """
17
+ files: list of uploaded image files
18
+ max_new_tokens: how many new tokens to generate
19
  """
20
  captions = []
21
  for f in files or []:
22
  img = Image.open(f.name).convert("RGB")
23
+ # Pass all generation params in one dict
24
+ result = captioner(
25
+ img,
26
+ generate_kwargs={
27
+ "max_new_tokens": int(max_new_tokens),
28
+ "num_beams": 4,
29
+ }
30
+ )
31
  text = result[0]["generated_text"].strip().capitalize()
32
  captions.append(text)
33
  return "\n\n".join(captions)
34
 
35
+ # --- Gradio UI ---
36
+ with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
37
+ gr.Markdown("# Florence‑2‑large Alt‑Text Generator")
38
+ gr.Markdown("Using Microsoft/Florence‑2‑large via `image-to-text`, with `generate_kwargs`.")
39
 
40
  with gr.Row():
41
  file_input = gr.File(
 
45
  )
46
  max_new_tokens = gr.Slider(
47
  minimum=16,
48
+ maximum=512,
49
  step=16,
50
  value=64,
51
  label="Max New Tokens"
52
  )
53
+ generate_btn = gr.Button("Generate Alt Text")
54
+ clear_btn = gr.Button("Clear All")
55
 
56
  gallery = gr.Gallery(label="Image Preview", columns=4)
57
+ output = gr.Textbox(label="Generated Alt Texts", lines=10, show_copy_button=True)
 
 
 
 
58
 
59
+ # Preview uploaded images
60
  file_input.change(
61
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
62
  inputs=file_input,
63
+ outputs=gallery,
64
  )
65
 
66
  # Generate captions
 
71
  queue=True
72
  )
73
 
74
+ # Clear everything
75
  clear_btn.click(
76
  fn=lambda: ([], 64, [], ""),
77
  outputs=[file_input, max_new_tokens, gallery, output]