Workpam commited on
Commit
835f727
·
verified ·
1 Parent(s): 272422f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -31
app.py CHANGED
@@ -1,57 +1,53 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  from PIL import Image
 
4
 
5
- # Initialize the Hugging Face image-to-text (captioning) pipeline
6
- captioner = pipeline(
7
- task="image-to-text",
8
- model="nlpconnect/vit-gpt2-image-captioning"
9
- )
10
 
11
- def generate_alt_text(files):
12
  """
13
- Generates alt text captions for a list of uploaded image files.
14
- Each file is opened with PIL before captioning.
15
  """
16
- captions = []
17
  for f in files:
18
- # f may be a temporary file object or file path
19
  path = f.name if hasattr(f, 'name') else f
20
  img = Image.open(path).convert("RGB")
21
- result = captioner(img)
22
- text = result[0].get('generated_text') or result[0].get('caption')
23
- captions.append(text.strip())
24
- return "\n".join([f"{i+1}. {cap}" for i, cap in enumerate(captions)])
 
 
 
 
 
25
 
26
- # Build Gradio Blocks app with custom JS support
27
- with gr.Blocks(title="Multi-Image Alt Text Generator") as demo:
28
- gr.Markdown("# Alt Text Generator for Multiple Images")
29
- gr.Markdown(
30
- "Upload one or more images below and click **Generate Alt Text** to get descriptive captions."
31
- )
32
- # Use File component to allow multiple image uploads
33
  file_input = gr.File(
34
  label="Upload Images",
35
  file_count="multiple",
36
  file_types=["image"]
37
  )
38
- # Textbox to display the generated alt texts
39
  output_box = gr.Textbox(
40
- label="Generated Alt Texts",
41
- placeholder="Your alt text will appear here...",
42
  lines=10,
43
  elem_id="output_box"
44
  )
45
  with gr.Row():
46
- generate_btn = gr.Button("Generate Alt Text")
47
  copy_btn = gr.Button("Copy to Clipboard")
48
- # Generate captions on click
49
- generate_btn.click(
50
- fn=generate_alt_text,
51
  inputs=file_input,
52
  outputs=output_box
53
  )
54
- # Copy to clipboard using the 'js' parameter (no underscore)
55
  copy_btn.click(
56
  fn=None,
57
  inputs=None,
@@ -59,6 +55,5 @@ with gr.Blocks(title="Multi-Image Alt Text Generator") as demo:
59
  js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }"
60
  )
61
 
62
- # Launch the app
63
  if __name__ == "__main__":
64
  demo.launch()
 
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,
 
55
  js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }"
56
  )
57
 
 
58
  if __name__ == "__main__":
59
  demo.launch()