tonyassi commited on
Commit
a51fb10
·
verified ·
1 Parent(s): c3dafce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -17
app.py CHANGED
@@ -4,29 +4,43 @@ from PIL import Image
4
  from transformers import BlipProcessor, BlipForConditionalGeneration
5
  import time
6
 
 
 
7
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
8
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
9
 
10
- def caption(img, min_len, max_len):
11
  raw_image = Image.open(img).convert('RGB')
12
-
13
  inputs = processor(raw_image, return_tensors="pt")
14
-
15
- out = model.generate(**inputs, min_length=min_len, max_length=max_len)
 
 
 
16
  return processor.decode(out[0], skip_special_tokens=True)
17
 
18
- def greet(img, min_len, max_len):
 
 
19
  start = time.time()
20
- result = caption(img, min_len, max_len)
21
- end = time.time()
22
- total_time = str(end - start)
23
- result = result + '\n' + total_time + ' seconds'
24
- return result
 
25
 
26
- iface = gr.Interface(fn=greet,
27
- title='Blip Image Captioning Large',
28
- description="[Salesforce/blip-image-captioning-large](https://huggingface.co/Salesforce/blip-image-captioning-large) Runs on CPU",
29
- inputs=[gr.Image(type='filepath', label='Image'), gr.Slider(label='Minimum Length', minimum=1, maximum=1000, value=30), gr.Slider(label='Maximum Length', minimum=1, maximum=1000, value=100)],
30
- outputs=gr.Textbox(label='Caption'),
31
- theme = gr.themes.Base(primary_hue="teal",secondary_hue="teal",neutral_hue="slate"),)
32
- iface.launch()
 
 
 
 
 
 
 
4
  from transformers import BlipProcessor, BlipForConditionalGeneration
5
  import time
6
 
7
+ Image.MAX_IMAGE_PIXELS = None # disable pillow’s size limit
8
+
9
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
10
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
11
 
12
+ def caption(img, min_new, max_new):
13
  raw_image = Image.open(img).convert('RGB')
14
+ raw_image.thumbnail((1024, 1024))
15
  inputs = processor(raw_image, return_tensors="pt")
16
+ out = model.generate(
17
+ **inputs,
18
+ min_new_tokens=min_new,
19
+ max_new_tokens=max_new
20
+ )
21
  return processor.decode(out[0], skip_special_tokens=True)
22
 
23
+ def greet(img, min_new, max_new):
24
+ if img is None:
25
+ return "❌ Please upload an image."
26
  start = time.time()
27
+ try:
28
+ result = caption(img, min_new, max_new)
29
+ except Exception as e:
30
+ return f"⚠️ Error: {e}"
31
+ elapsed = time.time() - start
32
+ return f"{result}\n⏱ Took {elapsed:.2f} seconds"
33
 
34
+ iface = gr.Interface(
35
+ fn=greet,
36
+ title='BLIP Image Captioning (large)',
37
+ description="Uses Salesforce/blip-image-captioning-large on CPU.",
38
+ inputs=[
39
+ gr.Image(type='filepath', label='Image'),
40
+ gr.Slider(label='Min New Tokens', minimum=1, maximum=50, value=5),
41
+ gr.Slider(label='Max New Tokens', minimum=1, maximum=100, value=20),
42
+ ],
43
+ outputs=gr.Textbox(label='Caption'),
44
+ theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate"),
45
+ )
46
+ iface.launch()