Bruce Wayne commited on
Commit
9676dc7
·
verified ·
1 Parent(s): 4bddd33

Upload final.py

Browse files
Files changed (1) hide show
  1. final.py +45 -0
final.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ from transformers import BlipProcessor, BlipForQuestionAnswering
4
+ from PIL import Image
5
+
6
+ # Load the processor and model
7
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
8
+ model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
9
+
10
+ # Initialize session state to store chat history
11
+ if 'history' not in st.session_state:
12
+ st.session_state.history = []
13
+
14
+ st.title("Conversational Image Recognition Chatbot")
15
+
16
+ # Upload image
17
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
18
+
19
+ if uploaded_file is not None:
20
+ # Display the uploaded image
21
+ image = Image.open(uploaded_file)
22
+ st.image(image, caption='Uploaded Image.', use_column_width=True)
23
+
24
+ # Store the uploaded image in session state
25
+ st.session_state.image = image
26
+
27
+ # Chat interface
28
+ user_input = st.text_input("You: ", key="input")
29
+
30
+ if st.button("Send"):
31
+ if user_input:
32
+
33
+ # Process the image and question
34
+ inputs = processor(st.session_state.image, user_input, return_tensors="pt")
35
+ output = model.generate(**inputs)
36
+ answer = processor.decode(output[0], skip_special_tokens=True)
37
+
38
+ # Add user question and model answer to chat history
39
+ st.session_state.history.append({"You": user_input, "chatbot": answer})
40
+
41
+ # Display the chat history
42
+ if st.session_state.history:
43
+ for i, chat in enumerate(st.session_state.history):
44
+ st.write(f"**You:** {chat['You']}")
45
+ st.write(f"**chatbot:** {chat['chatbot']}")