Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +37 -0
- requirement.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load BLIP model and processor
|
| 6 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
| 7 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-vqa-base")
|
| 8 |
+
|
| 9 |
+
def answer_question(image, question):
|
| 10 |
+
"""
|
| 11 |
+
Generates an answer based on the given image and question.
|
| 12 |
+
"""
|
| 13 |
+
try:
|
| 14 |
+
if not question.strip():
|
| 15 |
+
return "Please enter a question."
|
| 16 |
+
|
| 17 |
+
# Prepare inputs for the model
|
| 18 |
+
inputs = processor(images=image, text=question, return_tensors="pt")
|
| 19 |
+
|
| 20 |
+
# Generate answer
|
| 21 |
+
outputs = model.generate(**inputs)
|
| 22 |
+
answer = processor.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
+
|
| 24 |
+
return answer
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"An error occurred: {str(e)}"
|
| 27 |
+
|
| 28 |
+
# Gradio Interface
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=answer_question, # Function to process image + question
|
| 31 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(label="Ask a question about the image")],
|
| 32 |
+
outputs="text",
|
| 33 |
+
title="Visual Question Answering with BLIP",
|
| 34 |
+
description="Upload an image and ask a question about it. The model will generate an answer."
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
iface.launch()
|
requirement.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
pillow
|