raygiles3 commited on
Commit
c0fde26
1 Parent(s): 24bbf6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -7
app.py CHANGED
@@ -1,9 +1,33 @@
1
  import gradio as gr
2
- def greet(name, intensity):
3
- return "Hello, " + name + "!" * int(intensity)
4
- demo = gr.Interface(
5
- fn=greet,
6
- inputs=["text", "slider"],
7
- outputs=["text"],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  )
9
- demo.launch()
 
 
1
  import gradio as gr
2
+ from transformers import BlipProcessor, BlipForConditionalGeneration
3
+ from PIL import Image
4
+
5
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
6
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
7
+
8
+ def generate_caption(image):
9
+ # Now directly using the PIL Image object
10
+ inputs = processor(images=image, return_tensors="pt")
11
+ outputs = model.generate(**inputs)
12
+ caption = processor.decode(outputs[0], skip_special_tokens=True)
13
+ return caption
14
+
15
+ def caption_image(image):
16
+ """
17
+ Takes a PIL Image input and returns a caption.
18
+ """
19
+ try:
20
+ caption = generate_caption(image)
21
+ return caption
22
+ except Exception as e:
23
+ return f"An error occurred: {str(e)}"
24
+
25
+ iface = gr.Interface(
26
+ fn=caption_image,
27
+ inputs=gr.Image(type="pil"),
28
+ outputs="text",
29
+ title="Image Captioning with BLIP",
30
+ description="Upload an image to generate a caption."
31
  )
32
+
33
+ iface.launch()