Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,30 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
4 |
+
import requests
|
5 |
|
6 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
7 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
8 |
+
|
9 |
+
|
10 |
+
def generate_caption(data, text):
|
11 |
+
if not data:
|
12 |
+
return "Please upload image first"
|
13 |
+
try:
|
14 |
+
inputs = processor(data, text, return_tensors="pt")
|
15 |
+
# data = Image.open(requests.get(data, stream=True).raw).convert('RGB')
|
16 |
+
out = model.generate(**inputs)
|
17 |
+
out_txt = processor.decode(out[0], skip_special_tokens=True)
|
18 |
+
return out_txt
|
19 |
+
except Exception as e:
|
20 |
+
print(e)
|
21 |
+
return f"Something went wrong."
|
22 |
+
|
23 |
+
|
24 |
+
iface = gr.Interface(
|
25 |
+
fn=generate_caption,
|
26 |
+
inputs = [gr.Image(label="Image", image_mode="RGB", type="pil"), gr.Textbox(label="Start caption with")],
|
27 |
+
outputs = gr.Textbox(label="Genereated Caption", show_copy_button=True)
|
28 |
+
)
|
29 |
+
|
30 |
+
iface.launch()
|