Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,8 +28,8 @@ qa_engine = QAEngine()
|
|
| 28 |
os.makedirs("temp", exist_ok=True)
|
| 29 |
|
| 30 |
# Sidebar for file upload
|
| 31 |
-
st.sidebar.header("Upload a
|
| 32 |
-
uploaded_file = st.sidebar.file_uploader("Choose a
|
| 33 |
|
| 34 |
# Initialize chat memory
|
| 35 |
memory_storage = StreamlitChatMessageHistory(key="chat_messages")
|
|
@@ -39,15 +39,15 @@ memory = ConversationBufferWindowMemory(
|
|
| 39 |
|
| 40 |
# Document upload & processing
|
| 41 |
if uploaded_file and "document_uploaded" not in st.session_state:
|
| 42 |
-
|
| 43 |
|
| 44 |
-
with open(
|
| 45 |
f.write(uploaded_file.read())
|
| 46 |
|
| 47 |
st.sidebar.success("File uploaded successfully!")
|
| 48 |
|
| 49 |
with st.spinner("Processing document..."):
|
| 50 |
-
document_processor.process_document(
|
| 51 |
|
| 52 |
st.sidebar.success("Document processed successfully!")
|
| 53 |
st.session_state["document_uploaded"] = True
|
|
@@ -61,16 +61,15 @@ for message in memory_storage.messages:
|
|
| 61 |
role = "user" if message.type == "human" else "assistant"
|
| 62 |
avatar = user_avatar if role == "user" else ai_avatar # Assign appropriate avatar
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
<
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
)
|
| 74 |
|
| 75 |
# User input at the bottom
|
| 76 |
user_input = st.chat_input("Ask me anything...")
|
|
@@ -78,16 +77,16 @@ user_input = st.chat_input("Ask me anything...")
|
|
| 78 |
if user_input:
|
| 79 |
memory_storage.add_user_message(user_input)
|
| 80 |
|
| 81 |
-
with st.
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
<
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
with st.spinner("Generating response..."):
|
| 93 |
if st.session_state.get("document_uploaded", False):
|
|
@@ -98,13 +97,13 @@ if user_input:
|
|
| 98 |
|
| 99 |
memory_storage.add_ai_message(answer)
|
| 100 |
|
| 101 |
-
with st.
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
<
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
| 28 |
os.makedirs("temp", exist_ok=True)
|
| 29 |
|
| 30 |
# Sidebar for file upload
|
| 31 |
+
st.sidebar.header("Upload a Document")
|
| 32 |
+
uploaded_file = st.sidebar.file_uploader("Choose a file", type=["pdf", "docx", "html", "pptx", "txt"])
|
| 33 |
|
| 34 |
# Initialize chat memory
|
| 35 |
memory_storage = StreamlitChatMessageHistory(key="chat_messages")
|
|
|
|
| 39 |
|
| 40 |
# Document upload & processing
|
| 41 |
if uploaded_file and "document_uploaded" not in st.session_state:
|
| 42 |
+
file_path = os.path.join("temp", uploaded_file.name)
|
| 43 |
|
| 44 |
+
with open(file_path, "wb") as f:
|
| 45 |
f.write(uploaded_file.read())
|
| 46 |
|
| 47 |
st.sidebar.success("File uploaded successfully!")
|
| 48 |
|
| 49 |
with st.spinner("Processing document..."):
|
| 50 |
+
document_processor.process_document(file_path)
|
| 51 |
|
| 52 |
st.sidebar.success("Document processed successfully!")
|
| 53 |
st.session_state["document_uploaded"] = True
|
|
|
|
| 61 |
role = "user" if message.type == "human" else "assistant"
|
| 62 |
avatar = user_avatar if role == "user" else ai_avatar # Assign appropriate avatar
|
| 63 |
|
| 64 |
+
st.markdown(
|
| 65 |
+
f"""
|
| 66 |
+
<div style="display: flex; align-items: center; margin-bottom: 10px;">
|
| 67 |
+
<img src="data:image/jpeg;base64,{avatar}" width="40" height="40" style="border-radius: 50%; margin-right: 10px;">
|
| 68 |
+
<div style="background-color: #f1f1f1; padding: 10px; border-radius: 10px; max-width: 80%;">{message.content}</div>
|
| 69 |
+
</div>
|
| 70 |
+
""",
|
| 71 |
+
unsafe_allow_html=True
|
| 72 |
+
)
|
|
|
|
| 73 |
|
| 74 |
# User input at the bottom
|
| 75 |
user_input = st.chat_input("Ask me anything...")
|
|
|
|
| 77 |
if user_input:
|
| 78 |
memory_storage.add_user_message(user_input)
|
| 79 |
|
| 80 |
+
with st.markdown(
|
| 81 |
+
f"""
|
| 82 |
+
<div style="display: flex; align-items: center; margin-bottom: 10px;">
|
| 83 |
+
<img src="data:image/jpeg;base64,{user_avatar}" width="40" height="40" style="border-radius: 50%; margin-right: 10px;">
|
| 84 |
+
<div style="background-color: #d9f7be; padding: 10px; border-radius: 10px; max-width: 80%;">{user_input}</div>
|
| 85 |
+
</div>
|
| 86 |
+
""",
|
| 87 |
+
unsafe_allow_html=True
|
| 88 |
+
):
|
| 89 |
+
pass
|
| 90 |
|
| 91 |
with st.spinner("Generating response..."):
|
| 92 |
if st.session_state.get("document_uploaded", False):
|
|
|
|
| 97 |
|
| 98 |
memory_storage.add_ai_message(answer)
|
| 99 |
|
| 100 |
+
with st.markdown(
|
| 101 |
+
f"""
|
| 102 |
+
<div style="display: flex; align-items: center; margin-bottom: 10px;">
|
| 103 |
+
<img src="data:image/jpeg;base64,{ai_avatar}" width="40" height="40" style="border-radius: 50%; margin-right: 10px;">
|
| 104 |
+
<div style="background-color: #e6f7ff; padding: 10px; border-radius: 10px; max-width: 80%;">{answer.content}</div>
|
| 105 |
+
</div>
|
| 106 |
+
""",
|
| 107 |
+
unsafe_allow_html=True
|
| 108 |
+
):
|
| 109 |
+
pass
|