Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import requests
|
4 |
+
from PIL import Image
|
5 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
6 |
+
|
7 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
8 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large", torch_dtype=torch.float16).to("cuda")
|
9 |
+
|
10 |
+
def infer(image_input):
|
11 |
+
#img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
|
12 |
+
raw_image = Image.open(requests.get(image_input, stream=True).raw).convert('RGB')
|
13 |
+
|
14 |
+
# unconditional image captioning
|
15 |
+
inputs = processor(raw_image, return_tensors="pt").to("cuda", torch.float16)
|
16 |
+
|
17 |
+
out = model.generate(**inputs)
|
18 |
+
|
19 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
20 |
+
print(caption)
|
21 |
+
|
22 |
+
return caption
|
23 |
+
|
24 |
+
css="""
|
25 |
+
#col-container {max-width: 910px; margin-left: auto; margin-right: auto;}
|
26 |
+
a {text-decoration-line: underline; font-weight: 600;}
|
27 |
+
"""
|
28 |
+
|
29 |
+
with gr.Blocks(css=css) as demo:
|
30 |
+
with gr.Column(elem_id="col-container"):
|
31 |
+
gr.Markdown(
|
32 |
+
"""
|
33 |
+
# Image to Story
|
34 |
+
Upload an image, get a story !
|
35 |
+
<br/>
|
36 |
+
<br/>
|
37 |
+
[![Duplicate this Space](https://huggingface.co/datasets/huggingface/badges/raw/main/duplicate-this-space-sm.svg)](https://huggingface.co/spaces/fffiloni/SplitTrack2MusicGen?duplicate=true) for longer audio, more control and no queue.</p>
|
38 |
+
"""
|
39 |
+
)
|
40 |
+
image_in = gr.Image(label="Image input")
|
41 |
+
submit_btn = gr.Button('Sumbit')
|
42 |
+
story = gr.Textbox(label="Generated Story")
|
43 |
+
submit_btn.click(fn=infer, inputs=[image_in], outputs=[story])
|
44 |
+
|
45 |
+
demo.queue().launch()
|