Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,32 @@
|
|
|
|
1 |
import requests
|
2 |
from PIL import Image
|
3 |
-
from transformers import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
def visual_qna():
|
35 |
-
st.title("Visual Q&A")
|
36 |
-
img = load_image()
|
37 |
-
if img:
|
38 |
-
if query := st.chat_input("Enter your message"):
|
39 |
-
response = model(question=query, image=img)
|
40 |
-
with st.chat_message("assistant"):
|
41 |
-
st.write(response)
|
42 |
-
else:
|
43 |
-
st.warning("Please enter an image URL and click 'Load Image' before asking a question.")
|
|
|
1 |
+
import streamlit as st
|
2 |
import requests
|
3 |
from PIL import Image
|
4 |
+
from transformers import BlipProcessor, BlipForQuestionAnswering
|
5 |
+
|
6 |
+
# Model Loading
|
7 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-capfilt-large")
|
8 |
+
model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-capfilt-large")
|
9 |
+
|
10 |
+
# Streamlit App Structure
|
11 |
+
st.title("Visual Question Answering ")
|
12 |
+
|
13 |
+
def get_image():
|
14 |
+
img_url = st.text_input("Enter Image URL", value='https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg')
|
15 |
+
if img_url:
|
16 |
+
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
|
17 |
+
st.image(raw_image)
|
18 |
+
return raw_image
|
19 |
+
|
20 |
+
def process_vqa(image, question):
|
21 |
+
if image and question:
|
22 |
+
inputs = processor(image, question, return_tensors="pt")
|
23 |
+
output = model.generate(**inputs)
|
24 |
+
answer = processor.decode(output[0], skip_special_tokens=True)
|
25 |
+
st.write("Answer:", answer)
|
26 |
+
|
27 |
+
# User Input
|
28 |
+
image = get_image()
|
29 |
+
question = st.text_input("Ask your question about the image:")
|
30 |
+
|
31 |
+
# Process Question and Generate Answer
|
32 |
+
process_vqa(image, question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|