Workpam commited on
Commit
16dba33
·
verified ·
1 Parent(s): 67a3a70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -17
app.py CHANGED
@@ -4,50 +4,55 @@ import replicate
4
  import easyocr
5
  from PIL import Image
6
 
7
- # Load Replicate client for BLIP model
8
- # Ensure REPLICATE_API_TOKEN is set in your environment (or .env)
9
- client = replicate.Client(api_token=os.getenv("REPLICATE_API_TOKEN"))
 
 
 
 
 
 
 
10
  # Initialize EasyOCR for visible text extraction
11
- reader = easyocr.Reader(['en'], gpu=False)
 
12
 
13
  def generate_alt_text(files):
14
  """
15
- Generates alt text using Replicate's BLIP model, and includes any visible text via OCR.
16
  """
17
  captions = []
18
  for f in files:
19
- # Attempt to open image and extract visible text
20
  try:
21
  path = f.name if hasattr(f, 'name') else f
22
  image = Image.open(path).convert('RGB')
23
- buf = None
24
  except Exception:
25
  captions.append(f"<Error opening file {f}>")
26
  continue
27
-
28
  # OCR extraction
29
  try:
30
  ocr_list = reader.readtext(path, detail=0)
31
  visible_text = ' '.join(ocr_list).strip() or '<no text>'
32
  except Exception:
33
  visible_text = '<ocr error>'
34
-
35
  # Generate base caption via Replicate BLIP
36
  try:
37
- # Replicate expects a file-like or URL, here we send the file path
38
- output = client.run(
39
- "salesforce/blip-image-captioning-base",
40
- input={"image": open(path, "rb")}
41
- )
42
- # replicate.run returns a list of generated captions
43
- base_caption = output[0] if isinstance(output, list) else str(output)
44
  except Exception as e:
45
  captions.append(f"Error generating caption: {e}")
46
  continue
47
-
48
  # Compose final caption
49
  final = f"{base_caption.strip()} (Text: {visible_text})"
50
  captions.append(final)
 
 
51
  return "\n".join([f"{i+1}. {c}" for i, c in enumerate(captions)])
52
 
53
  # Build Gradio interface
@@ -57,19 +62,23 @@ with demo:
57
  gr.Markdown(
58
  "Upload multiple images, and get detailed alt text including visible in‑image text. Powered by Replicate and EasyOCR."
59
  )
 
60
  # File upload
61
  file_input = gr.File(
62
  label="Upload Images",
63
  file_count="multiple",
64
  file_types=["image"]
65
  )
 
66
  # Preview gallery
67
  gallery = gr.Gallery(label="Image Preview", columns=4)
 
68
  # Controls
69
  with gr.Row():
70
  gen_btn = gr.Button("Generate Alt Text")
71
  clr_btn = gr.Button("Clear All")
72
  copy_btn = gr.Button("Copy to Clipboard")
 
73
  # Output
74
  output = gr.Textbox(label="Alt Texts", lines=10, elem_id="output_box")
75
 
 
4
  import easyocr
5
  from PIL import Image
6
 
7
+ # 1. Ensure the API token is set or crash early
8
+ if "REPLICATE_API_TOKEN" not in os.environ:
9
+ raise RuntimeError("Missing REPLICATE_API_TOKEN in environment!")
10
+
11
+ # 2. Inject it into the replicate module so every call is authenticated
12
+ replicate.api_token = os.environ["REPLICATE_API_TOKEN"]
13
+
14
+ # 3. Initialize the Replicate client
15
+ client = replicate.Client()
16
+
17
  # Initialize EasyOCR for visible text extraction
18
+ detector_langs = ['en'] # add more languages if needed
19
+ reader = easyocr.Reader(detector_langs, gpu=False)
20
 
21
  def generate_alt_text(files):
22
  """
23
+ Generates alt text using Replicate's BLIP model and includes any visible text via OCR.
24
  """
25
  captions = []
26
  for f in files:
27
+ # Attempt to open image
28
  try:
29
  path = f.name if hasattr(f, 'name') else f
30
  image = Image.open(path).convert('RGB')
 
31
  except Exception:
32
  captions.append(f"<Error opening file {f}>")
33
  continue
34
+
35
  # OCR extraction
36
  try:
37
  ocr_list = reader.readtext(path, detail=0)
38
  visible_text = ' '.join(ocr_list).strip() or '<no text>'
39
  except Exception:
40
  visible_text = '<ocr error>'
41
+
42
  # Generate base caption via Replicate BLIP
43
  try:
44
+ model = client.models.get("salesforce/blip-image-captioning-base")
45
+ result = model.predict(image=open(path, "rb"))
46
+ base_caption = result[0] if isinstance(result, list) else str(result)
 
 
 
 
47
  except Exception as e:
48
  captions.append(f"Error generating caption: {e}")
49
  continue
50
+
51
  # Compose final caption
52
  final = f"{base_caption.strip()} (Text: {visible_text})"
53
  captions.append(final)
54
+
55
+ # Format numbered list
56
  return "\n".join([f"{i+1}. {c}" for i, c in enumerate(captions)])
57
 
58
  # Build Gradio interface
 
62
  gr.Markdown(
63
  "Upload multiple images, and get detailed alt text including visible in‑image text. Powered by Replicate and EasyOCR."
64
  )
65
+
66
  # File upload
67
  file_input = gr.File(
68
  label="Upload Images",
69
  file_count="multiple",
70
  file_types=["image"]
71
  )
72
+
73
  # Preview gallery
74
  gallery = gr.Gallery(label="Image Preview", columns=4)
75
+
76
  # Controls
77
  with gr.Row():
78
  gen_btn = gr.Button("Generate Alt Text")
79
  clr_btn = gr.Button("Clear All")
80
  copy_btn = gr.Button("Copy to Clipboard")
81
+
82
  # Output
83
  output = gr.Textbox(label="Alt Texts", lines=10, elem_id="output_box")
84