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