Workpam commited on
Commit
f6ac175
·
verified ·
1 Parent(s): 4cb8890

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -23
app.py CHANGED
@@ -3,54 +3,55 @@ from transformers import pipeline
3
  from PIL import Image
4
  import torch
5
 
6
- # Set up device (GPU if available)
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
 
9
- # Initialize a reliable image-to-text captioning pipeline
10
  captioner = pipeline(
11
  task="image-to-text",
12
  model="nlpconnect/vit-gpt2-image-captioning",
13
- device=0 if device == "cuda" else -1 # GPU=0, CPU=-1
14
  )
15
 
16
  def generate_alt_text(files):
17
  """
18
- Takes a list of uploaded image files, generates alt-text captions,
19
- and handles errors gracefully.
20
  """
21
  captions = []
22
  for f in files:
23
- path = f.name if hasattr(f, 'name') else f
24
  try:
25
- image = Image.open(path).convert("RGB")
26
- result = captioner(image)
27
- text = result[0].get('generated_text') or result[0].get('caption')
28
- captions.append(text.strip())
29
  except Exception as e:
30
- captions.append(f"Error processing {path}: {str(e)}")
31
- return "\n".join([f"{i+1}. {cap}" for i, cap in enumerate(captions)])
 
32
 
33
  # Build the Gradio interface
34
  demo = gr.Blocks(title="Reliable Alt‑Text Generator")
35
  with demo:
36
  gr.Markdown("# Reliable Alt Text Generator")
37
- gr.Markdown(
38
- "Upload multiple images, preview them below, and generate accurate alt-text captions."
39
- )
40
- # File upload for multiple images
41
  file_input = gr.File(
42
  label="Upload Images",
43
  file_count="multiple",
44
  file_types=["image"]
45
  )
46
- # Preview gallery (4 columns)
 
47
  preview = gr.Gallery(label="Image Preview", columns=4)
48
- # Buttons
 
49
  with gr.Row():
50
  generate_btn = gr.Button("Generate Alt Text")
51
- clear_btn = gr.Button("Clear All")
52
- copy_btn = gr.Button("Copy to Clipboard")
53
- # Output textbox
 
54
  output = gr.Textbox(
55
  label="Generated Alt Texts",
56
  lines=10,
@@ -68,6 +69,7 @@ with demo:
68
  js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }"
69
  )
70
 
71
- # Enable concurrency and launch
72
- demo.queue(concurrency_count=3, max_size=10)
 
73
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
3
  from PIL import Image
4
  import torch
5
 
6
+ # Device setup (GPU if available)
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
 
9
+ # Lightweight, reliable captioning pipeline
10
  captioner = pipeline(
11
  task="image-to-text",
12
  model="nlpconnect/vit-gpt2-image-captioning",
13
+ device=0 if device == "cuda" else -1
14
  )
15
 
16
  def generate_alt_text(files):
17
  """
18
+ Generate alt-text captions for each uploaded image file.
 
19
  """
20
  captions = []
21
  for f in files:
22
+ path = f.name if hasattr(f, "name") else f
23
  try:
24
+ img = Image.open(path).convert("RGB")
25
+ result = captioner(img)
26
+ text = result[0].get("generated_text") or result[0].get("caption")
 
27
  except Exception as e:
28
+ text = f"<Error processing {path}: {e}>"
29
+ captions.append(text.strip())
30
+ return "\n".join(f"{i+1}. {cap}" for i, cap in enumerate(captions))
31
 
32
  # Build the Gradio interface
33
  demo = gr.Blocks(title="Reliable Alt‑Text Generator")
34
  with demo:
35
  gr.Markdown("# Reliable Alt Text Generator")
36
+ gr.Markdown("Upload multiple images, preview them, and get accurate captions.")
37
+
38
+ # 1) File uploader
 
39
  file_input = gr.File(
40
  label="Upload Images",
41
  file_count="multiple",
42
  file_types=["image"]
43
  )
44
+
45
+ # 2) Preview gallery (4 columns) — NO .style() here
46
  preview = gr.Gallery(label="Image Preview", columns=4)
47
+
48
+ # 3) Controls
49
  with gr.Row():
50
  generate_btn = gr.Button("Generate Alt Text")
51
+ clear_btn = gr.Button("Clear All")
52
+ copy_btn = gr.Button("Copy to Clipboard")
53
+
54
+ # 4) Output
55
  output = gr.Textbox(
56
  label="Generated Alt Texts",
57
  lines=10,
 
69
  js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }"
70
  )
71
 
72
+ # (Optional) enable queuing of up to 10 requests
73
+ demo.queue(max_size=10)
74
+
75
  demo.launch(server_name="0.0.0.0", server_port=7860)