Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,6 @@ import streamlit as st
|
|
| 2 |
import tempfile
|
| 3 |
import base64
|
| 4 |
import os
|
| 5 |
-
import nltk
|
| 6 |
-
nltk.download('punkt')
|
| 7 |
from src.utils.ingest_text import create_vector_database
|
| 8 |
from src.utils.ingest_image import extract_and_store_images
|
| 9 |
from src.utils.text_qa import qa_bot
|
|
@@ -13,13 +11,20 @@ from langchain.memory import ConversationBufferWindowMemory
|
|
| 13 |
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
| 14 |
from dotenv import load_dotenv
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
nest_asyncio.apply()
|
| 19 |
load_dotenv()
|
| 20 |
-
|
| 21 |
st.set_page_config(layout='wide', page_title="InsightFusion Chat")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
memory_storage = StreamlitChatMessageHistory(key="chat_messages")
|
| 24 |
memory = ConversationBufferWindowMemory(
|
| 25 |
memory_key="chat_history",
|
|
@@ -28,18 +33,20 @@ memory = ConversationBufferWindowMemory(
|
|
| 28 |
k=3
|
| 29 |
)
|
| 30 |
|
|
|
|
| 31 |
image_bg = r"data/pexels-andreea-ch-371539-1166644.jpg"
|
| 32 |
|
| 33 |
def add_bg_from_local(image_file):
|
| 34 |
-
with open(image_file, "rb") as
|
| 35 |
-
encoded_string = base64.b64encode(
|
| 36 |
st.markdown(f"""<style>.stApp {{
|
| 37 |
-
background-image: url(data:image/
|
| 38 |
-
background-size: cover
|
| 39 |
}}</style>""", unsafe_allow_html=True)
|
| 40 |
|
| 41 |
add_bg_from_local(image_bg)
|
| 42 |
|
|
|
|
| 43 |
st.markdown("""
|
| 44 |
<svg width="600" height="100">
|
| 45 |
<text x="50%" y="50%" font-family="San serif" font-size="42px" fill="Black" text-anchor="middle" stroke="white"
|
|
@@ -48,6 +55,7 @@ st.markdown("""
|
|
| 48 |
</svg>
|
| 49 |
""", unsafe_allow_html=True)
|
| 50 |
|
|
|
|
| 51 |
def get_answer(query, chain):
|
| 52 |
try:
|
| 53 |
response = chain.invoke(query)
|
|
@@ -56,58 +64,56 @@ def get_answer(query, chain):
|
|
| 56 |
st.error(f"Error in get_answer: {e}")
|
| 57 |
return None
|
| 58 |
|
| 59 |
-
|
| 60 |
path = None
|
|
|
|
| 61 |
|
| 62 |
-
# Handle uploaded file
|
| 63 |
if uploaded_file is not None:
|
| 64 |
temp_file_path = os.path.join("temp", uploaded_file.name)
|
| 65 |
os.makedirs("temp", exist_ok=True)
|
| 66 |
with open(temp_file_path, "wb") as f:
|
| 67 |
f.write(uploaded_file.getbuffer())
|
| 68 |
path = os.path.abspath(temp_file_path)
|
|
|
|
| 69 |
st.write(f"File saved to: {path}")
|
| 70 |
-
st.write("Document uploaded successfully!")
|
| 71 |
|
| 72 |
-
#
|
| 73 |
st.markdown("### Or use a demo file:")
|
| 74 |
if st.button("Use Demo PDF"):
|
| 75 |
-
demo_file_path = os.path.join("pdf_resource", "sample.pdf") # Replace with actual demo
|
| 76 |
if os.path.exists(demo_file_path):
|
| 77 |
path = os.path.abspath(demo_file_path)
|
| 78 |
-
st.
|
| 79 |
-
st.success("Demo file loaded successfully!")
|
| 80 |
-
|
| 81 |
with st.spinner("Processing demo file..."):
|
| 82 |
try:
|
| 83 |
client = create_vector_database(path)
|
| 84 |
image_vdb = extract_and_store_images(path)
|
| 85 |
chain = qa_bot(client)
|
| 86 |
-
st.session_state
|
| 87 |
-
st.session_state
|
| 88 |
-
st.success("Demo file
|
| 89 |
except Exception as e:
|
| 90 |
st.error(f"Error processing demo PDF: {e}")
|
| 91 |
else:
|
| 92 |
-
st.error("Demo
|
| 93 |
|
| 94 |
-
#
|
| 95 |
if st.button("Start Processing"):
|
| 96 |
if path is not None:
|
| 97 |
-
with st.spinner("Processing"):
|
| 98 |
try:
|
| 99 |
client = create_vector_database(path)
|
| 100 |
image_vdb = extract_and_store_images(path)
|
| 101 |
chain = qa_bot(client)
|
| 102 |
-
st.session_state
|
| 103 |
-
st.session_state
|
| 104 |
-
st.success("
|
| 105 |
except Exception as e:
|
| 106 |
st.error(f"Error during processing: {e}")
|
| 107 |
else:
|
| 108 |
-
st.error("Please upload a file or
|
| 109 |
|
| 110 |
-
#
|
| 111 |
st.markdown("""
|
| 112 |
<style>
|
| 113 |
.stChatInputContainer > div {
|
|
@@ -116,48 +122,38 @@ st.markdown("""
|
|
| 116 |
</style>
|
| 117 |
""", unsafe_allow_html=True)
|
| 118 |
|
| 119 |
-
# Chat
|
| 120 |
if user_input := st.chat_input("User Input"):
|
| 121 |
-
if
|
| 122 |
-
chain = st.session_state['chain']
|
| 123 |
-
image_vdb = st.session_state['image_vdb']
|
| 124 |
-
|
| 125 |
with st.chat_message("user"):
|
| 126 |
st.markdown(user_input)
|
| 127 |
-
|
| 128 |
with st.spinner("Generating Response..."):
|
| 129 |
-
response = get_answer(user_input, chain)
|
| 130 |
if response:
|
| 131 |
with st.chat_message("assistant"):
|
| 132 |
st.markdown(response)
|
| 133 |
-
|
| 134 |
-
memory.save_context(
|
| 135 |
-
{"input": user_input},
|
| 136 |
-
{"output": response}
|
| 137 |
-
)
|
| 138 |
|
| 139 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 140 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 141 |
|
| 142 |
try:
|
| 143 |
-
query_and_print_results(image_vdb, user_input)
|
| 144 |
except Exception as e:
|
| 145 |
st.error(f"Error querying image database: {e}")
|
| 146 |
else:
|
| 147 |
-
st.error("Failed to generate response.")
|
| 148 |
else:
|
| 149 |
-
st.error("Please
|
| 150 |
-
|
| 151 |
-
# Initialize message state
|
| 152 |
-
if "messages" not in st.session_state:
|
| 153 |
-
st.session_state.messages = []
|
| 154 |
|
| 155 |
-
# Display
|
| 156 |
-
for
|
| 157 |
-
with st.chat_message(
|
| 158 |
-
st.write(
|
| 159 |
|
| 160 |
-
# Display
|
| 161 |
for i, msg in enumerate(memory_storage.messages):
|
| 162 |
-
|
| 163 |
-
st.chat_message(
|
|
|
|
| 2 |
import tempfile
|
| 3 |
import base64
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
from src.utils.ingest_text import create_vector_database
|
| 6 |
from src.utils.ingest_image import extract_and_store_images
|
| 7 |
from src.utils.text_qa import qa_bot
|
|
|
|
| 11 |
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
|
| 14 |
+
# Setup
|
|
|
|
| 15 |
nest_asyncio.apply()
|
| 16 |
load_dotenv()
|
|
|
|
| 17 |
st.set_page_config(layout='wide', page_title="InsightFusion Chat")
|
| 18 |
|
| 19 |
+
# ---- Session State Initialization ----
|
| 20 |
+
if "messages" not in st.session_state:
|
| 21 |
+
st.session_state.messages = []
|
| 22 |
+
if "chain" not in st.session_state:
|
| 23 |
+
st.session_state.chain = None
|
| 24 |
+
if "image_vdb" not in st.session_state:
|
| 25 |
+
st.session_state.image_vdb = None
|
| 26 |
+
|
| 27 |
+
# ---- Chat Memory Setup ----
|
| 28 |
memory_storage = StreamlitChatMessageHistory(key="chat_messages")
|
| 29 |
memory = ConversationBufferWindowMemory(
|
| 30 |
memory_key="chat_history",
|
|
|
|
| 33 |
k=3
|
| 34 |
)
|
| 35 |
|
| 36 |
+
# ---- Background Image Setup ----
|
| 37 |
image_bg = r"data/pexels-andreea-ch-371539-1166644.jpg"
|
| 38 |
|
| 39 |
def add_bg_from_local(image_file):
|
| 40 |
+
with open(image_file, "rb") as img:
|
| 41 |
+
encoded_string = base64.b64encode(img.read())
|
| 42 |
st.markdown(f"""<style>.stApp {{
|
| 43 |
+
background-image: url(data:image/png;base64,{encoded_string.decode()});
|
| 44 |
+
background-size: cover;
|
| 45 |
}}</style>""", unsafe_allow_html=True)
|
| 46 |
|
| 47 |
add_bg_from_local(image_bg)
|
| 48 |
|
| 49 |
+
# ---- Title ----
|
| 50 |
st.markdown("""
|
| 51 |
<svg width="600" height="100">
|
| 52 |
<text x="50%" y="50%" font-family="San serif" font-size="42px" fill="Black" text-anchor="middle" stroke="white"
|
|
|
|
| 55 |
</svg>
|
| 56 |
""", unsafe_allow_html=True)
|
| 57 |
|
| 58 |
+
# ---- Utility ----
|
| 59 |
def get_answer(query, chain):
|
| 60 |
try:
|
| 61 |
response = chain.invoke(query)
|
|
|
|
| 64 |
st.error(f"Error in get_answer: {e}")
|
| 65 |
return None
|
| 66 |
|
| 67 |
+
# ---- File Upload ----
|
| 68 |
path = None
|
| 69 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
|
| 70 |
|
|
|
|
| 71 |
if uploaded_file is not None:
|
| 72 |
temp_file_path = os.path.join("temp", uploaded_file.name)
|
| 73 |
os.makedirs("temp", exist_ok=True)
|
| 74 |
with open(temp_file_path, "wb") as f:
|
| 75 |
f.write(uploaded_file.getbuffer())
|
| 76 |
path = os.path.abspath(temp_file_path)
|
| 77 |
+
st.success("Document uploaded successfully!")
|
| 78 |
st.write(f"File saved to: {path}")
|
|
|
|
| 79 |
|
| 80 |
+
# ---- Demo PDF Option ----
|
| 81 |
st.markdown("### Or use a demo file:")
|
| 82 |
if st.button("Use Demo PDF"):
|
| 83 |
+
demo_file_path = os.path.join("pdf_resource", "sample.pdf") # Replace with actual demo name
|
| 84 |
if os.path.exists(demo_file_path):
|
| 85 |
path = os.path.abspath(demo_file_path)
|
| 86 |
+
st.success(f"Using demo file: {path}")
|
|
|
|
|
|
|
| 87 |
with st.spinner("Processing demo file..."):
|
| 88 |
try:
|
| 89 |
client = create_vector_database(path)
|
| 90 |
image_vdb = extract_and_store_images(path)
|
| 91 |
chain = qa_bot(client)
|
| 92 |
+
st.session_state.chain = chain
|
| 93 |
+
st.session_state.image_vdb = image_vdb
|
| 94 |
+
st.success("Demo file processed successfully.")
|
| 95 |
except Exception as e:
|
| 96 |
st.error(f"Error processing demo PDF: {e}")
|
| 97 |
else:
|
| 98 |
+
st.error("Demo PDF not found in pdf_resource/.")
|
| 99 |
|
| 100 |
+
# ---- Processing Button ----
|
| 101 |
if st.button("Start Processing"):
|
| 102 |
if path is not None:
|
| 103 |
+
with st.spinner("Processing file..."):
|
| 104 |
try:
|
| 105 |
client = create_vector_database(path)
|
| 106 |
image_vdb = extract_and_store_images(path)
|
| 107 |
chain = qa_bot(client)
|
| 108 |
+
st.session_state.chain = chain
|
| 109 |
+
st.session_state.image_vdb = image_vdb
|
| 110 |
+
st.success("File processed successfully.")
|
| 111 |
except Exception as e:
|
| 112 |
st.error(f"Error during processing: {e}")
|
| 113 |
else:
|
| 114 |
+
st.error("Please upload a file or select a demo.")
|
| 115 |
|
| 116 |
+
# ---- Style Customization ----
|
| 117 |
st.markdown("""
|
| 118 |
<style>
|
| 119 |
.stChatInputContainer > div {
|
|
|
|
| 122 |
</style>
|
| 123 |
""", unsafe_allow_html=True)
|
| 124 |
|
| 125 |
+
# ---- Chat Interface ----
|
| 126 |
if user_input := st.chat_input("User Input"):
|
| 127 |
+
if st.session_state.chain and st.session_state.image_vdb:
|
|
|
|
|
|
|
|
|
|
| 128 |
with st.chat_message("user"):
|
| 129 |
st.markdown(user_input)
|
| 130 |
+
|
| 131 |
with st.spinner("Generating Response..."):
|
| 132 |
+
response = get_answer(user_input, st.session_state.chain)
|
| 133 |
if response:
|
| 134 |
with st.chat_message("assistant"):
|
| 135 |
st.markdown(response)
|
| 136 |
+
|
| 137 |
+
memory.save_context({"input": user_input}, {"output": response})
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 140 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 141 |
|
| 142 |
try:
|
| 143 |
+
query_and_print_results(st.session_state.image_vdb, user_input)
|
| 144 |
except Exception as e:
|
| 145 |
st.error(f"Error querying image database: {e}")
|
| 146 |
else:
|
| 147 |
+
st.error("Failed to generate a response.")
|
| 148 |
else:
|
| 149 |
+
st.error("Please process a file before chatting.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
# ---- Display Chat History ----
|
| 152 |
+
for msg in st.session_state.messages:
|
| 153 |
+
with st.chat_message(msg["role"]):
|
| 154 |
+
st.write(msg["content"])
|
| 155 |
|
| 156 |
+
# ---- Display Memory History (LangChain) ----
|
| 157 |
for i, msg in enumerate(memory_storage.messages):
|
| 158 |
+
role = "user" if i % 2 == 0 else "assistant"
|
| 159 |
+
st.chat_message(role).markdown(msg.content)
|