NEXAS commited on
Commit
66ad911
·
verified ·
1 Parent(s): 1f22481

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -28
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import streamlit as st
2
  import os
3
- import json
4
  import base64
5
  from langchain.memory import ConversationBufferWindowMemory
6
  from langchain_community.chat_message_histories import StreamlitChatMessageHistory
@@ -11,25 +10,14 @@ from utils.qa import QAEngine
11
  # Configure Streamlit page
12
  st.set_page_config(page_title="AI-Powered Document QA", layout="wide")
13
 
14
- # # Background Image
15
- # def add_bg_from_local(image_file):
16
- # with open(image_file, "rb") as image_file:
17
- # encoded_string = base64.b64encode(image_file.read())
18
- # st.markdown(
19
- # f"""
20
- # <style>
21
- # .stApp {{
22
- # background-image: url(data:image/png;base64,{encoded_string.decode()});
23
- # background-size: cover;
24
- # }}
25
- # </style>
26
- # """,
27
- # unsafe_allow_html=True,
28
- # )
29
-
30
- # # Path to background image
31
- # image_bg = "./image/background.jpeg" # Change this path accordingly
32
- # add_bg_from_local(image_bg)
33
 
34
  # Initialize document processing & AI components
35
  document_processor = DocumentProcessor()
@@ -41,7 +29,7 @@ os.makedirs("temp", exist_ok=True)
41
 
42
  # Sidebar for file upload
43
  st.sidebar.header("Upload a PDF")
44
- uploaded_file = st.sidebar.file_uploader("Choose a PDF file", type=["pdf"])
45
 
46
  # Initialize chat memory
47
  memory_storage = StreamlitChatMessageHistory(key="chat_messages")
@@ -64,25 +52,42 @@ if uploaded_file and "document_uploaded" not in st.session_state:
64
  st.sidebar.success("Document processed successfully!")
65
  st.session_state["document_uploaded"] = True
66
 
67
- # Chat interface layout
68
  st.markdown("<h2 style='text-align: center;'>AI Chat Assistant</h2>", unsafe_allow_html=True)
69
  st.markdown("---")
70
 
71
- # Display chat history
72
  for message in memory_storage.messages:
73
  role = "user" if message.type == "human" else "assistant"
 
 
74
  with st.chat_message(role):
75
- st.markdown(message.content)
 
 
 
 
 
 
 
 
76
 
77
  # User input at the bottom
78
  user_input = st.chat_input("Ask me anything...")
79
 
80
  if user_input:
81
- # Store user message in memory
82
  memory_storage.add_user_message(user_input)
83
 
84
  with st.chat_message("user"):
85
- st.markdown(user_input)
 
 
 
 
 
 
 
 
86
 
87
  with st.spinner("Generating response..."):
88
  if st.session_state.get("document_uploaded", False):
@@ -91,8 +96,15 @@ if user_input:
91
  answer = llm_processor.generate_answer("", user_input)
92
  st.warning("No document uploaded. This response is generated from general AI knowledge and may not be document-specific.")
93
 
94
- # Store AI response in memory
95
  memory_storage.add_ai_message(answer)
96
 
97
  with st.chat_message("assistant"):
98
- st.markdown(answer.content)
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import os
 
3
  import base64
4
  from langchain.memory import ConversationBufferWindowMemory
5
  from langchain_community.chat_message_histories import StreamlitChatMessageHistory
 
10
  # Configure Streamlit page
11
  st.set_page_config(page_title="AI-Powered Document QA", layout="wide")
12
 
13
+ # Function to encode image in Base64 for avatars
14
+ def encode_image(image_path):
15
+ with open(image_path, "rb") as file:
16
+ return base64.b64encode(file.read()).decode()
17
+
18
+ # Load avatar images
19
+ user_avatar = encode_image("./icons/user.jpg") # Change path if needed
20
+ ai_avatar = encode_image("./icons/ai.jpg")
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Initialize document processing & AI components
23
  document_processor = DocumentProcessor()
 
29
 
30
  # Sidebar for file upload
31
  st.sidebar.header("Upload a PDF")
32
+ uploaded_file = st.sidebar.file_uploader("Choose a PDF file", type=["pdf", "docx", "html", "pptx"])
33
 
34
  # Initialize chat memory
35
  memory_storage = StreamlitChatMessageHistory(key="chat_messages")
 
52
  st.sidebar.success("Document processed successfully!")
53
  st.session_state["document_uploaded"] = True
54
 
55
+ # Chat UI Header
56
  st.markdown("<h2 style='text-align: center;'>AI Chat Assistant</h2>", unsafe_allow_html=True)
57
  st.markdown("---")
58
 
59
+ # Display chat history with avatars
60
  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
  with st.chat_message(role):
65
+ st.markdown(
66
+ f"""
67
+ <div style="display: flex; align-items: center; margin-bottom: 10px;">
68
+ <img src="data:image/jpeg;base64,{avatar}" width="40" height="40" style="border-radius: 50%; margin-right: 10px;">
69
+ <div style="background-color: #f1f1f1; padding: 10px; border-radius: 10px; max-width: 80%;">{message.content}</div>
70
+ </div>
71
+ """,
72
+ unsafe_allow_html=True
73
+ )
74
 
75
  # User input at the bottom
76
  user_input = st.chat_input("Ask me anything...")
77
 
78
  if user_input:
 
79
  memory_storage.add_user_message(user_input)
80
 
81
  with st.chat_message("user"):
82
+ st.markdown(
83
+ f"""
84
+ <div style="display: flex; align-items: center; margin-bottom: 10px;">
85
+ <img src="data:image/jpeg;base64,{user_avatar}" width="40" height="40" style="border-radius: 50%; margin-right: 10px;">
86
+ <div style="background-color: #d9f7be; padding: 10px; border-radius: 10px; max-width: 80%;">{user_input}</div>
87
+ </div>
88
+ """,
89
+ unsafe_allow_html=True
90
+ )
91
 
92
  with st.spinner("Generating response..."):
93
  if st.session_state.get("document_uploaded", False):
 
96
  answer = llm_processor.generate_answer("", user_input)
97
  st.warning("No document uploaded. This response is generated from general AI knowledge and may not be document-specific.")
98
 
 
99
  memory_storage.add_ai_message(answer)
100
 
101
  with st.chat_message("assistant"):
102
+ st.markdown(
103
+ f"""
104
+ <div style="display: flex; align-items: center; margin-bottom: 10px;">
105
+ <img src="data:image/jpeg;base64,{ai_avatar}" width="40" height="40" style="border-radius: 50%; margin-right: 10px;">
106
+ <div style="background-color: #e6f7ff; padding: 10px; border-radius: 10px; max-width: 80%;">{answer.content}</div>
107
+ </div>
108
+ """,
109
+ unsafe_allow_html=True
110
+ )