Workpam commited on
Commit
7fb5c19
·
verified ·
1 Parent(s): f507848

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -34
app.py CHANGED
@@ -1,46 +1,37 @@
1
  import gradio as gr
2
- from transformers import InstructBlipProcessor, InstructBlipForConditionalGeneration
3
- import torch
4
  from PIL import Image
5
- import easyocr
6
 
7
- # Device and OCR setup
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
- reader = easyocr.Reader(['en'], gpu=(device=="cuda"))
10
-
11
- # Load InstructBLIP Vicuna-7b model for advanced multimodal instruction
12
- model_name = "Salesforce/instructblip-vicuna-7b"
13
- processor = InstructBlipProcessor.from_pretrained(model_name)
14
- model = InstructBlipForConditionalGeneration.from_pretrained(model_name).to(device)
15
 
 
16
  def generate_alt_text(files):
17
- """
18
- Generates alt-text that accurately describes each image and includes any visible text.
19
- """
20
  captions = []
21
  for f in files:
22
  path = f.name if hasattr(f, 'name') else f
23
- image = Image.open(path).convert("RGB")
24
- # 1. OCR to extract on-image text
25
- ocr_results = reader.readtext(path, detail=0)
26
- visible_text = ' '.join(ocr_results).strip() or "<no text detected>"
27
- # 2. Instruction to the model
28
- instruction = f"Describe this image in one sentence, and include the visible text: '{visible_text}'."
29
- # 3. Prepare inputs and generate
30
- inputs = processor(images=image, text=instruction, return_tensors="pt").to(device)
31
- with torch.no_grad():
32
- outputs = model.generate(**inputs, max_new_tokens=100)
33
- desc = processor.decode(outputs[0], skip_special_tokens=True)
34
- captions.append(desc.strip())
35
  return "\n".join([f"{i+1}. {cap}" for i, cap in enumerate(captions)])
36
 
37
- # Build interactive Gradio interface
38
- with gr.Blocks(title="Accurate Alt‑Text Generator with OCR") as demo:
39
- gr.Markdown("# Advanced Alt‑Text Generator with OCR")
40
  gr.Markdown(
41
- "Upload multiple images, preview them below, and generate precise descriptions that include any on‑image text."
42
  )
43
- # File uploader & preview gallery
44
  file_input = gr.File(
45
  label="Upload Images",
46
  file_count="multiple",
@@ -51,14 +42,14 @@ with gr.Blocks(title="Accurate Alt‑Text Generator with OCR") as demo:
51
  with gr.Row():
52
  generate_btn = gr.Button("Generate Alt Text")
53
  clear_btn = gr.Button("Clear All")
 
 
54
  output = gr.Textbox(
55
  label="Generated Alt Texts",
56
  lines=10,
57
  elem_id="output_box"
58
  )
59
- copy_btn = gr.Button("Copy to Clipboard")
60
-
61
- # Bind events
62
  file_input.upload(lambda files: files, inputs=file_input, outputs=preview)
63
  generate_btn.click(fn=generate_alt_text, inputs=file_input, outputs=output)
64
  clear_btn.click(lambda: ([], [], ""), inputs=None, outputs=[file_input, preview, output])
@@ -66,4 +57,6 @@ with gr.Blocks(title="Accurate Alt‑Text Generator with OCR") as demo:
66
  js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }")
67
 
68
  if __name__ == "__main__":
69
- demo.launch()
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
3
  from PIL import Image
4
+ import torch
5
 
6
+ # Use a lightweight, accurate image-to-text captioning model
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ captioner = pipeline(
9
+ task="image-to-text",
10
+ model="nlpconnect/vit-gpt2-image-captioning",
11
+ device=0 if device=="cuda" else -1
12
+ )
 
13
 
14
+ # Core generation function
15
  def generate_alt_text(files):
 
 
 
16
  captions = []
17
  for f in files:
18
  path = f.name if hasattr(f, 'name') else f
19
+ try:
20
+ image = Image.open(path).convert("RGB")
21
+ result = captioner(image)
22
+ text = result[0].get('generated_text') or result[0].get('caption')
23
+ captions.append(text.strip())
24
+ except Exception as e:
25
+ captions.append(f"Error processing {path}: {str(e)}")
 
 
 
 
 
26
  return "\n".join([f"{i+1}. {cap}" for i, cap in enumerate(captions)])
27
 
28
+ # Build Gradio interface
29
+ with gr.Blocks(title="Reliable Alt‑Text Generator") as demo:
30
+ gr.Markdown("# Reliable Alt Text Generator")
31
  gr.Markdown(
32
+ "Upload multiple images, preview them, and get accurate alt-text captions using a robust, CPU-friendly model."
33
  )
34
+ # File upload and preview
35
  file_input = gr.File(
36
  label="Upload Images",
37
  file_count="multiple",
 
42
  with gr.Row():
43
  generate_btn = gr.Button("Generate Alt Text")
44
  clear_btn = gr.Button("Clear All")
45
+ copy_btn = gr.Button("Copy to Clipboard")
46
+ # Output
47
  output = gr.Textbox(
48
  label="Generated Alt Texts",
49
  lines=10,
50
  elem_id="output_box"
51
  )
52
+ # Bindings
 
 
53
  file_input.upload(lambda files: files, inputs=file_input, outputs=preview)
54
  generate_btn.click(fn=generate_alt_text, inputs=file_input, outputs=output)
55
  clear_btn.click(lambda: ([], [], ""), inputs=None, outputs=[file_input, preview, output])
 
57
  js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }")
58
 
59
  if __name__ == "__main__":
60
+ # Enable concurrency and queue
61
+ demo.queue(concurrency_count=3, max_size=10)
62
+ demo.launch(server_name="0.0.0.0", server_port=7860)