Workpam commited on
Commit
0387d92
·
verified ·
1 Parent(s): dbfebfe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -3,7 +3,7 @@ from transformers import pipeline
3
  from PIL import Image
4
 
5
  # --- Model Setup ---
6
- # Using an open, unrestricted image-to-text model
7
  MODEL_NAME = "microsoft/git-large-coco"
8
 
9
  # Initialize the image-to-text pipeline
@@ -15,22 +15,21 @@ captioner = pipeline(
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 specified pipeline.
19
  """
20
  captions = []
21
  for f in files or []:
22
- # Load image
23
  img = Image.open(f.name).convert("RGB")
24
- # Generate caption with a token limit
25
  result = captioner(img, max_new_tokens=int(max_new_tokens))
26
  text = result[0]["generated_text"].strip().capitalize()
27
  captions.append(text)
28
  return "\n\n".join(captions)
29
 
30
  # --- Gradio Interface ---
31
- with gr.Blocks(title="Image-to-Text Captioning Demo") as demo:
32
- gr.Markdown("# Image-to-Text Captioning Demo")
33
- gr.Markdown(f"**Model:** {MODEL_NAME}")
34
 
35
  with gr.Row():
36
  file_input = gr.File(
@@ -43,26 +42,26 @@ with gr.Blocks(title="Image-to-Text Captioning Demo") as demo:
43
  maximum=256,
44
  step=16,
45
  value=64,
46
- label="Max New Tokens for Caption"
47
  )
48
- generate_btn = gr.Button("Generate Captions")
49
  clear_btn = gr.Button("Clear All")
50
 
51
  gallery = gr.Gallery(label="Image Preview", columns=4)
52
  output = gr.Textbox(
53
- label="Generated Captions",
54
  lines=10,
55
  show_copy_button=True
56
  )
57
 
58
- # Preview images in gallery when selected
59
  file_input.change(
60
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
61
  inputs=file_input,
62
  outputs=gallery
63
  )
64
 
65
- # Generate captions on click
66
  generate_btn.click(
67
  fn=generate_alt_text,
68
  inputs=[file_input, max_new_tokens],
@@ -70,7 +69,7 @@ with gr.Blocks(title="Image-to-Text Captioning Demo") as demo:
70
  queue=True
71
  )
72
 
73
- # Clear inputs and outputs
74
  clear_btn.click(
75
  fn=lambda: ([], 64, [], ""),
76
  outputs=[file_input, max_new_tokens, gallery, output]
 
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
 
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(
 
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")
49
 
50
  gallery = gr.Gallery(label="Image Preview", columns=4)
51
  output = gr.Textbox(
52
+ label="Generated Alt Texts",
53
  lines=10,
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
  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]