Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BlipForQuestionAnswering, AutoProcessor
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
|
| 7 |
+
processor = AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
| 8 |
+
|
| 9 |
+
def answer_question(image, question):
|
| 10 |
+
|
| 11 |
+
inputs = processor(image, question, return_tensors="pt")
|
| 12 |
+
|
| 13 |
+
out = model.generate(**inputs)
|
| 14 |
+
|
| 15 |
+
answer = processor.decode(out[0], skip_special_tokens=True)
|
| 16 |
+
return answer
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
iface = gr.Interface(
|
| 20 |
+
fn=answer_question,
|
| 21 |
+
inputs=[
|
| 22 |
+
gr.inputs.Image(type="pil", label="Upload Image"),
|
| 23 |
+
gr.inputs.Textbox(label="Enter Your Question")
|
| 24 |
+
],
|
| 25 |
+
outputs="text",
|
| 26 |
+
title="BLIP Question Answering",
|
| 27 |
+
description="Upload an image and ask a question to get an answer."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
iface.launch()
|