Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import ViltProcessor, ViltForQuestionAnswering, BlipForQuestionAnswering, AutoProcessor
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Define available models
|
6 |
+
models = {
|
7 |
+
"ViLT": (ViltProcessor, ViltForQuestionAnswering, "dandelin/vilt-b32-finetuned-vqa"),
|
8 |
+
"BLIP": (AutoProcessor, BlipForQuestionAnswering, "Salesforce/blip-vqa-base"),
|
9 |
+
}
|
10 |
+
|
11 |
+
# Streamlit app
|
12 |
+
st.title("Simple VQA App π€π")
|
13 |
+
st.subheader("A demo app showcasing VQA models. ViLT and BLIP model.")
|
14 |
+
# Sidebar for model selection
|
15 |
+
selected_model = st.sidebar.selectbox("Select Model", list(models.keys()))
|
16 |
+
|
17 |
+
# Load selected model and processor
|
18 |
+
processor, model_class, model_name = models[selected_model]
|
19 |
+
processor = processor.from_pretrained(model_name)
|
20 |
+
model = model_class.from_pretrained(model_name)
|
21 |
+
|
22 |
+
# Image and question input
|
23 |
+
uploaded_image = st.file_uploader("Upload Image")
|
24 |
+
question = st.text_input("Ask a Question about the Image")
|
25 |
+
|
26 |
+
# Process image and question if provided
|
27 |
+
if uploaded_image and question:
|
28 |
+
image = Image.open(uploaded_image)
|
29 |
+
st.image(image, caption="Uploaded Image")
|
30 |
+
encoding = processor(image, question, return_tensors="pt")
|
31 |
+
outputs = model(**encoding)
|
32 |
+
logits = outputs.logits
|
33 |
+
idx = logits.argmax(-1).item()
|
34 |
+
answer = model.config.id2label[idx]
|
35 |
+
# Display answer with animation emoji
|
36 |
+
st.write(f"π€ Model Answer: {answer} π")
|
37 |
+
# Disclaimer
|
38 |
+
st.sidebar.markdown("This is a demo app showcasing VQA models. Actual performance may vary.")
|