Workpam commited on
Commit
1c2695d
·
verified ·
1 Parent(s): 6c8c955

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -27
app.py CHANGED
@@ -1,53 +1,61 @@
1
  import gradio as gr
 
2
  from PIL import Image
3
- import easyocr
4
 
5
- # Initialize EasyOCR reader (supports multiple languages; here English)
6
- reader = easyocr.Reader(['en'], gpu=False)
7
 
8
- def extract_text(files):
 
 
 
 
 
 
 
 
 
 
9
  """
10
- Extracts text from each uploaded image file using EasyOCR.
11
- Returns numbered list of detected text strings.
12
  """
13
- results = []
14
  for f in files:
15
  path = f.name if hasattr(f, 'name') else f
16
- img = Image.open(path).convert("RGB")
17
- # Run OCR
18
- ocr_results = reader.readtext(path, detail=0)
19
- # Join text segments with spaces
20
- text = ' '.join(ocr_results).strip()
21
- if not text:
22
- text = "<No text detected>"
23
- results.append(text)
24
- # Format as numbered list
25
- return "\n".join([f"{i+1}. {t}" for i, t in enumerate(results)])
26
 
27
- # Build Gradio Blocks app
28
- with gr.Blocks(title="Image Text Extractor (OCR)") as demo:
29
- gr.Markdown("# Accurate Text Extraction from Multiple Images")
30
- gr.Markdown("Upload images and extract all visible text using EasyOCR.")
 
 
31
  file_input = gr.File(
32
  label="Upload Images",
33
  file_count="multiple",
34
  file_types=["image"]
35
  )
36
  output_box = gr.Textbox(
37
- label="Extracted Text",
38
- placeholder="Detected text will appear here...",
39
  lines=10,
 
40
  elem_id="output_box"
41
  )
42
  with gr.Row():
43
- extract_btn = gr.Button("Extract Text")
44
  copy_btn = gr.Button("Copy to Clipboard")
45
- extract_btn.click(
46
- fn=extract_text,
47
  inputs=file_input,
48
  outputs=output_box
49
  )
50
- # Copy to clipboard
51
  copy_btn.click(
52
  fn=None,
53
  inputs=None,
 
1
  import gradio as gr
2
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
3
  from PIL import Image
4
+ import torch
5
 
6
+ # Use GPU if available for faster inference
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
 
9
+ # Load BLIP-2 base model for balanced speed and accuracy
10
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-base")
11
+ model = (
12
+ Blip2ForConditionalGeneration.from_pretrained(
13
+ "Salesforce/blip2-flan-t5-base",
14
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
15
+ )
16
+ .to(device)
17
+ )
18
+
19
+ def generate_alt_text(files):
20
  """
21
+ Generates descriptive alt-text captions for uploaded images using BLIP-2.
 
22
  """
23
+ captions = []
24
  for f in files:
25
  path = f.name if hasattr(f, 'name') else f
26
+ image = Image.open(path).convert("RGB")
27
+ inputs = processor(images=image, return_tensors="pt").to(device)
28
+ with torch.no_grad():
29
+ outputs = model.generate(**inputs, max_new_tokens=50)
30
+ text = processor.decode(outputs[0], skip_special_tokens=True)
31
+ captions.append(text.strip())
32
+ return "\n".join([f"{i+1}. {cap}" for i, cap in enumerate(captions)])
 
 
 
33
 
34
+ # Build Gradio interface
35
+ with gr.Blocks(title="Efficient & Accurate Alt Text Generator") as demo:
36
+ gr.Markdown("# Alt‑Text Generator (BLIP‑2 Base)")
37
+ gr.Markdown(
38
+ "Upload multiple images for fast, accurate descriptions powered by Salesforce BLIP‑2."
39
+ )
40
  file_input = gr.File(
41
  label="Upload Images",
42
  file_count="multiple",
43
  file_types=["image"]
44
  )
45
  output_box = gr.Textbox(
46
+ label="Generated Alt Texts",
 
47
  lines=10,
48
+ placeholder="Your generated descriptions will appear here...",
49
  elem_id="output_box"
50
  )
51
  with gr.Row():
52
+ generate_btn = gr.Button("Generate Alt Text")
53
  copy_btn = gr.Button("Copy to Clipboard")
54
+ generate_btn.click(
55
+ fn=generate_alt_text,
56
  inputs=file_input,
57
  outputs=output_box
58
  )
 
59
  copy_btn.click(
60
  fn=None,
61
  inputs=None,