cryogenic22 commited on
Commit
502f9a1
·
verified ·
1 Parent(s): fccb2e5

Update components/chat.py

Browse files
Files changed (1) hide show
  1. components/chat.py +103 -38
components/chat.py CHANGED
@@ -2,19 +2,114 @@ import streamlit as st
2
  from langchain_core.messages import HumanMessage, AIMessage
3
  from utils.database import verify_vector_store
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def display_chat_interface():
6
  """Display modern chat interface with clean formatting."""
7
 
8
  # Add custom CSS for modern chat styling
9
  st.markdown("""
10
  <style>
11
- /* Clean, modern chat container */
12
- .chat-container {
13
- max-width: 800px;
14
- margin: auto;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  }
16
 
17
- /* Message styling */
18
  .user-message {
19
  background-color: #f0f2f6;
20
  padding: 1rem;
@@ -25,35 +120,9 @@ def display_chat_interface():
25
  .assistant-message {
26
  background-color: #ffffff;
27
  border: 1px solid #e0e0e0;
28
- padding: 1.5rem;
29
  border-radius: 10px;
30
  margin: 1rem 0;
31
  }
32
-
33
- /* Section styling */
34
- .section-header {
35
- font-weight: bold;
36
- color: #0f52ba;
37
- margin-top: 1rem;
38
- }
39
-
40
- .source-info {
41
- background-color: #f8f9fa;
42
- padding: 0.5rem;
43
- border-left: 3px solid #0f52ba;
44
- margin-top: 1rem;
45
- font-size: 0.9rem;
46
- }
47
-
48
- /* Content sections */
49
- .content-section {
50
- margin: 0.5rem 0;
51
- }
52
-
53
- /* Clean bullets */
54
- .clean-bullet {
55
- margin: 0.3rem 0;
56
- }
57
  </style>
58
  """, unsafe_allow_html=True)
59
 
@@ -75,11 +144,10 @@ def display_chat_interface():
75
  </div>
76
  """, unsafe_allow_html=True)
77
  elif isinstance(message, AIMessage):
78
- content = str(message.content)
79
  st.markdown(f"""
80
  <div class="assistant-message">
81
- 🤖 <strong>Assistant:</strong><br>
82
- {content}
83
  </div>
84
  """, unsafe_allow_html=True)
85
 
@@ -87,19 +155,16 @@ def display_chat_interface():
87
  if prompt := st.chat_input("Ask about your documents..."):
88
  try:
89
  with st.spinner("Analyzing..."):
90
- # Create message objects
91
  human_message = HumanMessage(content=prompt)
92
  st.session_state.messages.append(human_message)
93
 
94
- # Get response
95
  response = st.session_state.qa_system.invoke({
96
  "input": prompt,
97
  "chat_history": st.session_state.messages
98
  })
99
 
100
  if response:
101
- content = str(response)
102
- ai_message = AIMessage(content=content)
103
  st.session_state.messages.append(ai_message)
104
  st.rerun()
105
  else:
 
2
  from langchain_core.messages import HumanMessage, AIMessage
3
  from utils.database import verify_vector_store
4
 
5
+ def format_assistant_response(content):
6
+ """Format the assistant's response into a structured layout."""
7
+ # Clean up the content first
8
+ content = str(content)
9
+
10
+ # Remove metadata and extra markers
11
+ if "content='" in content:
12
+ content = content.split("content='")[1].split("additional_kwargs")[0].strip("'")
13
+
14
+ # Split into sections
15
+ sections = {}
16
+ current_section = "default"
17
+ lines = content.replace('\\n', '\n').split('\n')
18
+
19
+ for line in lines:
20
+ line = line.strip()
21
+ if not line:
22
+ continue
23
+
24
+ if line.startswith('**') and line.endswith('**'):
25
+ current_section = line.strip('**')
26
+ sections[current_section] = []
27
+ elif line.startswith('*') or line.startswith('#'):
28
+ current_section = "main"
29
+ if current_section not in sections:
30
+ sections[current_section] = []
31
+ sections[current_section].append(line.strip('* '))
32
+ else:
33
+ if current_section not in sections:
34
+ sections[current_section] = []
35
+ sections[current_section].append(line)
36
+
37
+ # Build formatted HTML
38
+ formatted_html = """
39
+ <div class="response-container">
40
+ <div class="summary-section">
41
+ <h4>Executive Summary</h4>
42
+ <p>{}</p>
43
+ </div>
44
+ """.format(' '.join(sections.get('Executive Summary', sections.get('default', ['']))))
45
+
46
+ # Add key themes section
47
+ if 'Key Themes' in sections:
48
+ formatted_html += """
49
+ <div class="themes-section">
50
+ <h4>Key Themes</h4>
51
+ <ul>
52
+ """
53
+ for theme in sections['Key Themes']:
54
+ if theme.strip():
55
+ formatted_html += f'<li class="theme-item">{theme}</li>'
56
+ formatted_html += '</ul></div>'
57
+
58
+ # Add any remaining sections
59
+ for section, content in sections.items():
60
+ if section not in ['Executive Summary', 'Key Themes', 'default']:
61
+ formatted_html += f"""
62
+ <div class="content-section">
63
+ <h4>{section}</h4>
64
+ <p>{'<br>'.join(content)}</p>
65
+ </div>
66
+ """
67
+
68
+ formatted_html += '</div>'
69
+ return formatted_html
70
+
71
  def display_chat_interface():
72
  """Display modern chat interface with clean formatting."""
73
 
74
  # Add custom CSS for modern chat styling
75
  st.markdown("""
76
  <style>
77
+ .response-container {
78
+ background-color: white;
79
+ border-radius: 10px;
80
+ padding: 20px;
81
+ margin: 10px 0;
82
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
83
+ }
84
+
85
+ .summary-section {
86
+ background-color: #f8f9fa;
87
+ padding: 15px;
88
+ border-radius: 8px;
89
+ margin-bottom: 20px;
90
+ }
91
+
92
+ .themes-section {
93
+ margin: 20px 0;
94
+ }
95
+
96
+ .theme-item {
97
+ margin: 10px 0;
98
+ padding: 5px 0;
99
+ line-height: 1.5;
100
+ }
101
+
102
+ .content-section {
103
+ margin: 15px 0;
104
+ padding: 10px;
105
+ border-left: 3px solid #0f52ba;
106
+ }
107
+
108
+ .content-section h4 {
109
+ color: #0f52ba;
110
+ margin-bottom: 10px;
111
  }
112
 
 
113
  .user-message {
114
  background-color: #f0f2f6;
115
  padding: 1rem;
 
120
  .assistant-message {
121
  background-color: #ffffff;
122
  border: 1px solid #e0e0e0;
 
123
  border-radius: 10px;
124
  margin: 1rem 0;
125
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  </style>
127
  """, unsafe_allow_html=True)
128
 
 
144
  </div>
145
  """, unsafe_allow_html=True)
146
  elif isinstance(message, AIMessage):
 
147
  st.markdown(f"""
148
  <div class="assistant-message">
149
+ 🤖 <strong>Assistant:</strong>
150
+ {format_assistant_response(message.content)}
151
  </div>
152
  """, unsafe_allow_html=True)
153
 
 
155
  if prompt := st.chat_input("Ask about your documents..."):
156
  try:
157
  with st.spinner("Analyzing..."):
 
158
  human_message = HumanMessage(content=prompt)
159
  st.session_state.messages.append(human_message)
160
 
 
161
  response = st.session_state.qa_system.invoke({
162
  "input": prompt,
163
  "chat_history": st.session_state.messages
164
  })
165
 
166
  if response:
167
+ ai_message = AIMessage(content=str(response))
 
168
  st.session_state.messages.append(ai_message)
169
  st.rerun()
170
  else: