NEXAS commited on
Commit
3571b6c
·
verified ·
1 Parent(s): 0111201

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -35
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 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")
@@ -39,15 +39,15 @@ memory = ConversationBufferWindowMemory(
39
 
40
  # Document upload & processing
41
  if uploaded_file and "document_uploaded" not in st.session_state:
42
- pdf_path = os.path.join("temp", uploaded_file.name)
43
 
44
- with open(pdf_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(pdf_path)
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
- 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...")
@@ -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.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):
@@ -98,13 +97,13 @@ if user_input:
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
- )
 
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