Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import transformers
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
from transformers import BlipForImageTextRetrieval
|
| 7 |
+
from transformers import AutoProcessor
|
| 8 |
+
from transformers.utils import logging
|
| 9 |
+
from PIL import Image
|
| 10 |
+
|
| 11 |
+
model = BlipForConditionalGeneration.from_pretrained(
|
| 12 |
+
"Salesforce/blip-image-captioning-base")
|
| 13 |
+
processor = AutoProcessor.from_pretrained(
|
| 14 |
+
"Salesforce/blip-image-captioning-base")
|
| 15 |
+
|
| 16 |
+
def process_image(input_type, image_url, image_upload):
|
| 17 |
+
if input_type == "URL":
|
| 18 |
+
raw_image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
|
| 19 |
+
else:
|
| 20 |
+
raw_image = image_upload
|
| 21 |
+
|
| 22 |
+
inputs = processor(raw_image,return_tensors="pt")
|
| 23 |
+
out = model.generate(**inputs)[0]
|
| 24 |
+
description = processor.decode(out, skip_special_tokens=True).capitalize()
|
| 25 |
+
|
| 26 |
+
formatted_description = (
|
| 27 |
+
f"""<div><h1 style='text-align: center; font-size: 40px; color: orange;'>
|
| 28 |
+
{description}
|
| 29 |
+
</h1></div>"""
|
| 30 |
+
)
|
| 31 |
+
return formatted_description
|
| 32 |
+
|
| 33 |
+
def display_image_from_url(image_url):
|
| 34 |
+
if image_url:
|
| 35 |
+
image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
|
| 36 |
+
return image
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
def toggle_inputs(input_type):
|
| 40 |
+
if input_type == "URL":
|
| 41 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
|
| 42 |
+
else:
|
| 43 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
| 44 |
+
|
| 45 |
+
with gr.Blocks() as demo:
|
| 46 |
+
gr.Markdown(
|
| 47 |
+
"""
|
| 48 |
+
# Image Captioning - test & demo app by Srinivas.V..
|
| 49 |
+
Paste either URL of an image or upload the image and submit.
|
| 50 |
+
""")
|
| 51 |
+
|
| 52 |
+
input_type = gr.Radio(choices=["URL", "Upload"], label="Input Type")
|
| 53 |
+
image_url = gr.Textbox(label="Image URL", visible=False)
|
| 54 |
+
url_image = gr.Image(type="pil", label="URL Image", visible=False)
|
| 55 |
+
image_upload = gr.Image(type="pil", label="Upload Image", visible=False)
|
| 56 |
+
|
| 57 |
+
input_type.change(fn=toggle_inputs, inputs=input_type, outputs=[image_url, url_image, image_upload])
|
| 58 |
+
image_url.change(fn=display_image_from_url, inputs=image_url, outputs=url_image)
|
| 59 |
+
|
| 60 |
+
submit_btn = gr.Button("Submit")
|
| 61 |
+
processed_image = gr.HTML(label="Caption for the Image")
|
| 62 |
+
submit_btn.click(fn=process_image, inputs=[input_type, image_url, image_upload], outputs=processed_image)
|
| 63 |
+
|
| 64 |
+
demo.launch(debug=True, share=True)
|