Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
-
from function import bounding_box
|
| 4 |
from tempfile import NamedTemporaryFile
|
| 5 |
import os
|
| 6 |
from function import ImageCaptionTools, ObjectDetectionTool
|
|
@@ -8,10 +8,25 @@ from langchain.agents import initialize_agent, AgentType
|
|
| 8 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 9 |
from langchain.memory import ConversationBufferWindowMemory
|
| 10 |
from htmlTemplate import css, bot_template, user_template
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
DIR_PATH = './temp'
|
| 13 |
-
if not os.path.exists(DIR_PATH):
|
| 14 |
-
os.mkdir(DIR_PATH)
|
| 15 |
|
| 16 |
# initialize Agent
|
| 17 |
def agent_init():
|
|
@@ -30,11 +45,7 @@ def agent_init():
|
|
| 30 |
)
|
| 31 |
return agents
|
| 32 |
|
| 33 |
-
|
| 34 |
-
for filename in os.listdir(DIR_PATH):
|
| 35 |
-
file_path = os.path.join(DIR_PATH, filename)
|
| 36 |
-
if os.path.isfile(file_path):
|
| 37 |
-
os.unlink(file_path)
|
| 38 |
|
| 39 |
def main():
|
| 40 |
st.set_page_config(
|
|
@@ -46,16 +57,18 @@ def main():
|
|
| 46 |
st.title("Chat with an Image 🖼️")
|
| 47 |
agent = agent_init()
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
if "image_processed" not in st.session_state:
|
| 50 |
st.session_state.image_processed = None
|
| 51 |
|
| 52 |
if "result_bounding" not in st.session_state:
|
| 53 |
st.session_state.result_bounding = None
|
| 54 |
|
| 55 |
-
# Delete temp files when session state changes
|
| 56 |
-
if st.session_state.image_processed is None:
|
| 57 |
-
delete_temp_files()
|
| 58 |
-
|
| 59 |
# image_path = 'documentation\photo_1.jpg'
|
| 60 |
|
| 61 |
col1, col2 = st.columns([1, 1])
|
|
@@ -71,7 +84,7 @@ def main():
|
|
| 71 |
click_process = st.button("Process Image", disabled=not image_upload)
|
| 72 |
if click_process:
|
| 73 |
delete_temp_files()
|
| 74 |
-
with NamedTemporaryFile(dir=
|
| 75 |
f.write(image_upload.getbuffer())
|
| 76 |
st.session_state.image_path = f.name
|
| 77 |
st.session_state.image_processed = True
|
|
@@ -93,7 +106,7 @@ def main():
|
|
| 93 |
click_ask = st.button("Ask Question", disabled=not st.session_state.image_processed)
|
| 94 |
if click_ask:
|
| 95 |
st.write(user_template.replace("{{MSG}}", user_question), unsafe_allow_html=True)
|
| 96 |
-
with st.spinner("
|
| 97 |
chat_history = agent.invoke({"input": f"{user_question}, this is the image path: {st.session_state.image_path}"})
|
| 98 |
response = chat_history['output']
|
| 99 |
st.write(bot_template.replace("{{MSG}}", response), unsafe_allow_html=True)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
+
from function import bounding_box
|
| 4 |
from tempfile import NamedTemporaryFile
|
| 5 |
import os
|
| 6 |
from function import ImageCaptionTools, ObjectDetectionTool
|
|
|
|
| 8 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 9 |
from langchain.memory import ConversationBufferWindowMemory
|
| 10 |
from htmlTemplate import css, bot_template, user_template
|
| 11 |
+
import random
|
| 12 |
+
|
| 13 |
+
DIR = './temp'
|
| 14 |
+
if not os.path.exists(DIR):
|
| 15 |
+
os.mkdir(DIR)
|
| 16 |
+
|
| 17 |
+
if "image_processed" not in st.session_state:
|
| 18 |
+
DIR_PATH = os.path.join(DIR, str(random.randint(1,999999999)))
|
| 19 |
+
st.session_state.dirpath = DIR_PATH
|
| 20 |
+
if not os.path.exists(DIR_PATH):
|
| 21 |
+
os.mkdir(DIR_PATH)
|
| 22 |
+
|
| 23 |
+
def delete_temp_files():
|
| 24 |
+
for filename in os.listdir(st.session_state.dirpath):
|
| 25 |
+
file_path = os.path.join(st.session_state.dirpath, filename)
|
| 26 |
+
if os.path.isfile(file_path):
|
| 27 |
+
os.unlink(file_path)
|
| 28 |
+
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# initialize Agent
|
| 32 |
def agent_init():
|
|
|
|
| 45 |
)
|
| 46 |
return agents
|
| 47 |
|
| 48 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def main():
|
| 51 |
st.set_page_config(
|
|
|
|
| 57 |
st.title("Chat with an Image 🖼️")
|
| 58 |
agent = agent_init()
|
| 59 |
|
| 60 |
+
# Check if the page has been reloaded
|
| 61 |
+
if 'reloaded' not in st.session_state:
|
| 62 |
+
st.session_state.reloaded = False
|
| 63 |
+
else:
|
| 64 |
+
st.session_state.reloaded = True
|
| 65 |
+
|
| 66 |
if "image_processed" not in st.session_state:
|
| 67 |
st.session_state.image_processed = None
|
| 68 |
|
| 69 |
if "result_bounding" not in st.session_state:
|
| 70 |
st.session_state.result_bounding = None
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# image_path = 'documentation\photo_1.jpg'
|
| 73 |
|
| 74 |
col1, col2 = st.columns([1, 1])
|
|
|
|
| 84 |
click_process = st.button("Process Image", disabled=not image_upload)
|
| 85 |
if click_process:
|
| 86 |
delete_temp_files()
|
| 87 |
+
with NamedTemporaryFile(dir=st.session_state.dirpath, delete=False) as f:
|
| 88 |
f.write(image_upload.getbuffer())
|
| 89 |
st.session_state.image_path = f.name
|
| 90 |
st.session_state.image_processed = True
|
|
|
|
| 106 |
click_ask = st.button("Ask Question", disabled=not st.session_state.image_processed)
|
| 107 |
if click_ask:
|
| 108 |
st.write(user_template.replace("{{MSG}}", user_question), unsafe_allow_html=True)
|
| 109 |
+
with st.spinner("Doraemon Searching for Answer🔎"):
|
| 110 |
chat_history = agent.invoke({"input": f"{user_question}, this is the image path: {st.session_state.image_path}"})
|
| 111 |
response = chat_history['output']
|
| 112 |
st.write(bot_template.replace("{{MSG}}", response), unsafe_allow_html=True)
|