Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,47 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import requests
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
# Importing Hugging Face Spaces components
|
| 6 |
+
from huggingface_hub import hf_spaces
|
| 7 |
|
| 8 |
+
# Load the image question answering model
|
| 9 |
+
model_name = "deepset/roberta-base-squad2"
|
| 10 |
+
hf_spaces.use_model(model_name)
|
| 11 |
|
| 12 |
+
# Function to perform image question answering
|
| 13 |
+
@st.cache(allow_output_mutation=True)
|
| 14 |
+
def perform_image_qa(image, question):
|
| 15 |
+
inputs = {"image": image, "question": question}
|
| 16 |
+
response = hf_spaces.use_model(model_name, inputs)
|
| 17 |
+
return response['answer']
|
| 18 |
+
|
| 19 |
+
def main():
|
| 20 |
+
st.title("Image Question Answering App")
|
| 21 |
+
st.write("Upload an image and ask a question to get answers!")
|
| 22 |
+
|
| 23 |
+
# File uploader for image
|
| 24 |
+
uploaded_image = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
| 25 |
+
|
| 26 |
+
if uploaded_image is not None:
|
| 27 |
+
image = Image.open(uploaded_image)
|
| 28 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 29 |
+
|
| 30 |
+
# Ask question
|
| 31 |
+
question = st.text_input("Ask your question here:")
|
| 32 |
+
|
| 33 |
+
if st.button("Get Answer"):
|
| 34 |
+
if question:
|
| 35 |
+
# Convert image to bytes
|
| 36 |
+
img_bytes = BytesIO()
|
| 37 |
+
image.save(img_bytes, format='PNG')
|
| 38 |
+
img_bytes = img_bytes.getvalue()
|
| 39 |
+
|
| 40 |
+
# Perform image question answering
|
| 41 |
+
answer = perform_image_qa(img_bytes, question)
|
| 42 |
+
st.success("Answer: " + answer)
|
| 43 |
+
else:
|
| 44 |
+
st.warning("Please enter a question.")
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
main()
|