Workpam commited on
Commit
47fe535
·
verified ·
1 Parent(s): 65037df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -33
app.py CHANGED
@@ -5,20 +5,22 @@ import torch
5
  from PIL import Image
6
  from concurrent.futures import ThreadPoolExecutor
7
  from transformers import BlipProcessor, BlipForConditionalGeneration
8
- import pandas as pd
9
 
10
  # 1. Initialize device
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
 
13
- # 2. Load BLIP model and processor locally (no API token required)
14
- processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
15
- model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
 
 
 
16
 
17
- # 3. Initialize EasyOCR for visible text extraction
18
  gpu_flag = torch.cuda.is_available()
19
  ocr_reader = easyocr.Reader(["en"], gpu=gpu_flag)
20
 
21
- # 4. Optionally load YOLOv5 for object detection (requires 'ultralytics' & 'seaborn')
22
  use_yolo = True
23
  yolo_model = None
24
  try:
@@ -34,15 +36,6 @@ cpu_cores = os.cpu_count() or 4
34
 
35
 
36
  def process_file(file_obj, max_len):
37
- """
38
- Process a single image:
39
- - Verify image
40
- - Extract visible text via OCR
41
- - Detect objects via YOLOv5
42
- - Generate caption via BLIP
43
- - Assemble and truncate
44
- Returns (filename, caption)
45
- """
46
  path = getattr(file_obj, 'name', file_obj)
47
  filename = os.path.basename(path)
48
 
@@ -53,7 +46,7 @@ def process_file(file_obj, max_len):
53
  except Exception:
54
  return filename, "<Error opening file>"
55
 
56
- # OCR extraction
57
  try:
58
  texts = ocr_reader.readtext(path, detail=0)
59
  visible = ' '.join(texts).strip()
@@ -71,7 +64,7 @@ def process_file(file_obj, max_len):
71
  except Exception:
72
  labels = ''
73
 
74
- # Generate BLIP caption
75
  try:
76
  image = Image.open(path).convert('RGB')
77
  inputs = processor(images=image, return_tensors="pt").to(device)
@@ -86,15 +79,13 @@ def process_file(file_obj, max_len):
86
  except Exception as e:
87
  return filename, f"Error generating caption: {e}"
88
 
89
- # Assemble parts
90
  parts = [base.strip()]
91
  if labels:
92
  parts.append(f"Objects: {labels}")
93
  if visible:
94
  parts.append(f"Text: {visible}")
95
  caption = '; '.join(parts)
96
-
97
- # Truncate cleanly without cutting words
98
  if len(caption) > max_len:
99
  caption = caption[:max_len].rsplit(' ', 1)[0] + '…'
100
 
@@ -102,27 +93,19 @@ def process_file(file_obj, max_len):
102
 
103
 
104
  def generate_alt_text(files, max_len):
105
- """
106
- Generate alt text for multiple images in parallel.
107
- Returns a newline-separated string of "filename: caption" lines.
108
- """
109
  if not files:
110
  return ""
111
-
112
  max_workers = min(len(files), cpu_cores)
113
  with ThreadPoolExecutor(max_workers=max_workers) as executor:
114
- results = list(executor.map(lambda f: process_file(f, max_len), files))
115
-
116
- lines = [f"{fname}: {text}" for fname, text in results]
117
  return "\n".join(lines)
118
 
119
  # Build Gradio interface
120
- demo = gr.Blocks(title="AI-Powered Alt Text Generator")
121
- with demo:
122
  gr.Markdown("# AI-Powered Alt Text Generator")
123
- gr.Markdown(
124
- "Generate rich alt text with OCR, optional object detection, and BLIP captions."
125
- )
126
 
127
  with gr.Row():
128
  file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
@@ -139,3 +122,10 @@ with demo:
139
  file_input.upload(lambda files: files, inputs=file_input, outputs=gallery)
140
  gen_btn.click(fn=generate_alt_text, inputs=[file_input, max_len], outputs=[output_text])
141
  copy_btn.click(None, None, None, js="() => { navigator.clipboard.writeText(document.getElementById('output_text').value); }")
 
 
 
 
 
 
 
 
5
  from PIL import Image
6
  from concurrent.futures import ThreadPoolExecutor
7
  from transformers import BlipProcessor, BlipForConditionalGeneration
 
8
 
9
  # 1. Initialize device
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
 
12
+ # 2. Load BLIP model and processor locally
13
+ def load_blip():
14
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
15
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
16
+ return processor, model
17
+ processor, model = load_blip()
18
 
19
+ # 3. Initialize EasyOCR
20
  gpu_flag = torch.cuda.is_available()
21
  ocr_reader = easyocr.Reader(["en"], gpu=gpu_flag)
22
 
23
+ # 4. Optionally load YOLOv5 for object detection
24
  use_yolo = True
25
  yolo_model = None
26
  try:
 
36
 
37
 
38
  def process_file(file_obj, max_len):
 
 
 
 
 
 
 
 
 
39
  path = getattr(file_obj, 'name', file_obj)
40
  filename = os.path.basename(path)
41
 
 
46
  except Exception:
47
  return filename, "<Error opening file>"
48
 
49
+ # OCR
50
  try:
51
  texts = ocr_reader.readtext(path, detail=0)
52
  visible = ' '.join(texts).strip()
 
64
  except Exception:
65
  labels = ''
66
 
67
+ # BLIP caption\
68
  try:
69
  image = Image.open(path).convert('RGB')
70
  inputs = processor(images=image, return_tensors="pt").to(device)
 
79
  except Exception as e:
80
  return filename, f"Error generating caption: {e}"
81
 
82
+ # Assemble and truncate
83
  parts = [base.strip()]
84
  if labels:
85
  parts.append(f"Objects: {labels}")
86
  if visible:
87
  parts.append(f"Text: {visible}")
88
  caption = '; '.join(parts)
 
 
89
  if len(caption) > max_len:
90
  caption = caption[:max_len].rsplit(' ', 1)[0] + '…'
91
 
 
93
 
94
 
95
  def generate_alt_text(files, max_len):
 
 
 
 
96
  if not files:
97
  return ""
 
98
  max_workers = min(len(files), cpu_cores)
99
  with ThreadPoolExecutor(max_workers=max_workers) as executor:
100
+ results = executor.map(lambda f: process_file(f, max_len), files)
101
+ lines = [f"{fn}: {txt}" for fn, txt in results]
 
102
  return "\n".join(lines)
103
 
104
  # Build Gradio interface
105
+ app = gr.Blocks(title="AI-Powered Alt Text Generator")
106
+ with app:
107
  gr.Markdown("# AI-Powered Alt Text Generator")
108
+ gr.Markdown("Generate rich alt text with OCR, optional object detection, and BLIP captions.")
 
 
109
 
110
  with gr.Row():
111
  file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
 
122
  file_input.upload(lambda files: files, inputs=file_input, outputs=gallery)
123
  gen_btn.click(fn=generate_alt_text, inputs=[file_input, max_len], outputs=[output_text])
124
  copy_btn.click(None, None, None, js="() => { navigator.clipboard.writeText(document.getElementById('output_text').value); }")
125
+
126
+ # For local testing
127
+ def main():
128
+ app.launch(server_name="0.0.0.0", server_port=7860, share=True)
129
+
130
+ if __name__ == "__main__":
131
+ main()