Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +111 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import base64
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from langchain.memory import ConversationSummaryBufferMemory
|
| 7 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
from langchain_core.messages import HumanMessage
|
| 10 |
+
|
| 11 |
+
os.environ["GOOGLE_API_KEY"] = "AIzaSyAc0VslmJlmiTFx7GB8QPYEHUZ5nZb5_Nk"
|
| 12 |
+
st.title("Vision Bot")
|
| 13 |
+
|
| 14 |
+
llm = ChatGoogleGenerativeAI(
|
| 15 |
+
model="gemini-1.5-flash",
|
| 16 |
+
max_tokens=4000
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
IMAGE_SAVE_FOLDER = "./uploaded_images"
|
| 20 |
+
if not os.path.exists(IMAGE_SAVE_FOLDER):
|
| 21 |
+
os.makedirs(IMAGE_SAVE_FOLDER)
|
| 22 |
+
|
| 23 |
+
st.markdown(
|
| 24 |
+
"""
|
| 25 |
+
<style>
|
| 26 |
+
.st-emotion-cache-janbn0 {
|
| 27 |
+
flex-direction: row-reverse;
|
| 28 |
+
text-align: right;
|
| 29 |
+
}
|
| 30 |
+
</style>
|
| 31 |
+
""",
|
| 32 |
+
unsafe_allow_html=True,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Initialize session states
|
| 36 |
+
if "messages" not in st.session_state:
|
| 37 |
+
st.session_state.messages = []
|
| 38 |
+
if "llm" not in st.session_state:
|
| 39 |
+
st.session_state.llm = llm
|
| 40 |
+
if "rag_memory" not in st.session_state:
|
| 41 |
+
st.session_state.rag_memory = ConversationSummaryBufferMemory(llm=st.session_state.llm, max_token_limit=5000)
|
| 42 |
+
if "current_image" not in st.session_state:
|
| 43 |
+
st.session_state.current_image = None
|
| 44 |
+
if "last_displayed_image" not in st.session_state:
|
| 45 |
+
st.session_state.last_displayed_image = None
|
| 46 |
+
|
| 47 |
+
container = st.container()
|
| 48 |
+
|
| 49 |
+
# Upload image
|
| 50 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"], key="image_uploader")
|
| 51 |
+
|
| 52 |
+
# Check if a new image is uploaded
|
| 53 |
+
if uploaded_image and uploaded_image != st.session_state.current_image:
|
| 54 |
+
st.session_state.current_image = uploaded_image
|
| 55 |
+
st.image(uploaded_image, caption="Newly Uploaded Image")
|
| 56 |
+
|
| 57 |
+
# Add a system message to mark the new image in the conversation
|
| 58 |
+
st.session_state.messages.append({
|
| 59 |
+
"role": "system",
|
| 60 |
+
"content": f"New image uploaded: {uploaded_image.name}",
|
| 61 |
+
"image": uploaded_image
|
| 62 |
+
})
|
| 63 |
+
|
| 64 |
+
# Display messages
|
| 65 |
+
for message in st.session_state.messages:
|
| 66 |
+
with container.chat_message(message["role"]):
|
| 67 |
+
if message["role"] == "system" and "image" in message:
|
| 68 |
+
st.image(message["image"])
|
| 69 |
+
st.write(message["content"])
|
| 70 |
+
|
| 71 |
+
# Take prompt
|
| 72 |
+
if prompt := st.chat_input("Enter your query here..."):
|
| 73 |
+
with container.chat_message("user"):
|
| 74 |
+
st.write(prompt)
|
| 75 |
+
|
| 76 |
+
# Save user input in session state
|
| 77 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 78 |
+
|
| 79 |
+
if st.session_state.current_image:
|
| 80 |
+
# Save uploaded image to disk
|
| 81 |
+
image = Image.open(st.session_state.current_image)
|
| 82 |
+
current_date = datetime.now().strftime("%Y%m%d")
|
| 83 |
+
image_name = f"{current_date}_{st.session_state.current_image.name}"
|
| 84 |
+
image_path = os.path.join(IMAGE_SAVE_FOLDER, image_name)
|
| 85 |
+
image.save(image_path)
|
| 86 |
+
|
| 87 |
+
# Encode image in base64
|
| 88 |
+
with open(image_path, "rb") as image_file:
|
| 89 |
+
encoded_string = base64.b64encode(image_file.read()).decode()
|
| 90 |
+
|
| 91 |
+
# Send image and text to the model
|
| 92 |
+
chat = HumanMessage(
|
| 93 |
+
content=[
|
| 94 |
+
{"type": "text", "text": prompt},
|
| 95 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_string}"}},
|
| 96 |
+
]
|
| 97 |
+
)
|
| 98 |
+
else:
|
| 99 |
+
# Send only text to the model if no image is uploaded
|
| 100 |
+
chat = HumanMessage(content=prompt)
|
| 101 |
+
|
| 102 |
+
# Get AI response
|
| 103 |
+
ai_msg = llm.invoke([chat]).content
|
| 104 |
+
with container.chat_message("assistant"):
|
| 105 |
+
st.write(ai_msg)
|
| 106 |
+
|
| 107 |
+
# Save the conversation context in memory
|
| 108 |
+
st.session_state.rag_memory.save_context({'input': prompt}, {'output': ai_msg})
|
| 109 |
+
|
| 110 |
+
# Append the assistant's message to the session state
|
| 111 |
+
st.session_state.messages.append({"role": "assistant", "content": ai_msg})
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
langchain
|
| 3 |
+
langchain_google_genai
|