Spaces:
Build error
Build error
File size: 1,647 Bytes
9676dc7 |
1 2 3 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 35 36 37 38 39 40 41 42 43 44 45 46 |
import streamlit as st
from transformers import BlipProcessor, BlipForQuestionAnswering
from PIL import Image
# Load the processor and model
processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
# Initialize session state to store chat history
if 'history' not in st.session_state:
st.session_state.history = []
st.title("Conversational Image Recognition Chatbot")
# Upload image
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Display the uploaded image
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image.', use_column_width=True)
# Store the uploaded image in session state
st.session_state.image = image
# Chat interface
user_input = st.text_input("You: ", key="input")
if st.button("Send"):
if user_input:
# Process the image and question
inputs = processor(st.session_state.image, user_input, return_tensors="pt")
output = model.generate(**inputs)
answer = processor.decode(output[0], skip_special_tokens=True)
# Add user question and model answer to chat history
st.session_state.history.append({"You": user_input, "chatbot": answer})
# Display the chat history
if st.session_state.history:
for i, chat in enumerate(st.session_state.history):
st.write(f"**You:** {chat['You']}")
st.write(f"**chatbot:** {chat['chatbot']}")
|