File size: 11,647 Bytes
d56abf3
3d26016
1624315
d56abf3
 
1624315
988c460
9e17cc9
988c460
 
 
 
 
 
 
 
 
 
9e17cc9
4288a7d
 
 
 
 
 
 
 
9e17cc9
4288a7d
 
 
 
 
 
 
 
 
9e17cc9
2c23388
 
 
9e17cc9
2c23388
 
 
 
 
9e17cc9
2c23388
 
 
 
9e17cc9
2c23388
 
9e17cc9
2c23388
 
 
 
 
9e17cc9
2c23388
 
 
 
9e17cc9
2c23388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e17cc9
2c23388
 
 
 
 
9e17cc9
2c23388
 
 
 
 
 
9e17cc9
2c23388
 
 
 
 
9e17cc9
a8872f2
d56abf3
9e17cc9
d56abf3
17b69f5
d56abf3
9e17cc9
0120103
 
 
2c23388
 
 
 
 
9e17cc9
2c23388
 
 
 
 
 
 
 
9e17cc9
2c23388
 
 
 
 
 
 
 
 
9e17cc9
2c23388
 
 
 
9e17cc9
2c23388
 
 
 
 
 
 
 
 
9e17cc9
2c23388
 
 
 
 
 
9e17cc9
2c23388
 
 
 
 
9e17cc9
2c23388
 
 
 
 
 
9e17cc9
2c23388
 
 
 
 
9e17cc9
2c23388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e17cc9
2c23388
 
 
 
 
 
 
 
 
0120103
 
9e17cc9
d56abf3
 
 
 
 
 
 
 
 
 
 
4288a7d
d56abf3
9e17cc9
d56abf3
 
 
 
 
9e17cc9
d56abf3
 
 
 
 
 
 
 
9e17cc9
d56abf3
 
9e17cc9
d56abf3
 
 
9e17cc9
d56abf3
 
4288a7d
2c23388
 
 
 
 
9e17cc9
d56abf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e17cc9
d56abf3
 
 
 
 
 
 
9e17cc9
0b9345a
 
 
 
9e17cc9
0b9345a
 
 
f202cee
0b9345a
9e17cc9
0b9345a
 
 
9e17cc9
0b9345a
 
 
 
 
 
 
 
 
 
9e17cc9
0b9345a
 
 
9e17cc9
0b9345a
 
9e17cc9
0b9345a
 
 
 
9e17cc9
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# components/chat.py
import streamlit as st
from datetime import datetime
from langchain_core.messages import HumanMessage, AIMessage
from backend import get_embeddings_model, initialize_qa_system
from utils.persistence import PersistenceManager


def ensure_embeddings_initialized():
    """Ensure embeddings model is initialized in session state."""
    if 'embeddings' not in st.session_state:
        try:
            st.session_state.embeddings = get_embeddings_model()
        except Exception as e:
            st.error(f"Error initializing embeddings model: {e}")
            return False
    return True


def format_session_date(session_id: str) -> str:
    """Format session ID into readable date."""
    try:
        # Handle different possible date formats
        if '_' in session_id:
            date_part = session_id.split('_')[1]
        else:
            date_part = session_id

        if len(date_part) == 8:  # Format: YYYYMMDD
            return datetime.strptime(date_part, '%Y%m%d').strftime('%B %d, %Y')
        elif len(date_part) == 14:  # Format: YYYYMMDD_HHMMSS
            return datetime.strptime(date_part, '%Y%m%d%H%M%S').strftime('%B %d, %Y %I:%M %p')
        else:
            return date_part  # Return original if format unknown
    except Exception as e:
        return f"Session: {session_id}"  # Fallback display


def clean_ai_response(content):
    """Clean up AI response content and remove technical artifacts."""
    content = str(content)

    # Remove common technical artifacts
    if "content='" in content:
        content = content.split("content='")[1]
    if "additional_kwargs" in content:
        content = content.split("additional_kwargs")[0]

    # Clean up any remaining artifacts
    content = content.strip("'")
    content = content.replace('\\n', '\n')
    content = content.replace('\\t', '\t')

    return content


def format_assistant_response(content):
    """Format the assistant's response into a structured layout."""
    try:
        # Clean the content first
        content = clean_ai_response(content)

        # Identify sections and structure
        lines = [line.strip() for line in content.split('\n') if line.strip()]
        formatted_sections = []
        current_section = []

        for line in lines:
            # Handle bullet points and lists
            if line.startswith(('•', '-', '*')):
                line = f"<li>{line.lstrip('•-* ')}</li>"
                if not current_section:
                    current_section.append("<ul>")
                current_section.append(line)
            # Handle section headers
            elif line.startswith('**') and line.endswith('**'):
                if current_section:
                    if current_section[0] == "<ul>":
                        current_section.append("</ul>")
                    formatted_sections.extend(current_section)
                    current_section = []
                header = line.strip('**')
                formatted_sections.append(f'<h4 class="section-header">{header}</h4>')
            # Regular text
            else:
                if current_section and current_section[0] == "<ul>":
                    current_section.append("</ul>")
                    formatted_sections.extend(current_section)
                    current_section = []
                formatted_sections.append(f'<p>{line}</p>')

        # Add any remaining content
        if current_section:
            if current_section[0] == "<ul>":
                current_section.append("</ul>")
            formatted_sections.extend(current_section)

        # Build the final HTML
        formatted_html = f"""
        <div class="response-content">
            {''.join(formatted_sections)}
        </div>
        """

        return formatted_html
    except Exception as e:
        st.error(f"Error formatting response: {str(e)}")
        return str(content)


def display_chat_interface():
    """Display modern chat interface with clean formatting."""

    # Initialize persistence if needed
    if 'persistence' not in st.session_state:
        st.session_state.persistence = PersistenceManager()

    # Add custom CSS for modern chat styling
    st.markdown("""
    <style>
        /* Chat container */
        .chat-container {
            max-width: 800px;
            margin: auto;
        }

        /* User message */
        .user-message {
            background-color: #f0f2f6;
            padding: 1rem;
            border-radius: 10px;
            margin: 1rem 0;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }

        /* Assistant message */
        .assistant-message {
            background-color: #ffffff;
            border: 1px solid #e0e0e0;
            padding: 1.5rem;
            border-radius: 10px;
            margin: 1rem 0;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }

        /* Response content */
        .response-content {
            line-height: 1.6;
        }

        /* Section headers */
        .section-header {
            color: #0f52ba;
            margin: 1.5rem 0 1rem 0;
            font-size: 1.1rem;
            font-weight: 600;
            border-bottom: 2px solid #e0e0e0;
            padding-bottom: 0.5rem;
        }

        /* Lists */
        .response-content ul {
            margin: 1rem 0;
            padding-left: 1.5rem;
            list-style-type: none;
        }

        .response-content li {
            margin: 0.5rem 0;
            position: relative;
            padding-left: 1rem;
        }

        .response-content li:before {
            content: "•";
            position: absolute;
            left: -1rem;
            color: #0f52ba;
        }

        /* Paragraphs */
        .response-content p {
            margin: 1rem 0;
            color: #2c3e50;
        }

        /* Source citations */
        .source-citation {
            font-style: italic;
            color: #666;
            border-top: 1px solid #e0e0e0;
            margin-top: 1rem;
            padding-top: 0.5rem;
            font-size: 0.9rem;
        }

        /* Session selector */
        .session-selector {
            padding: 1rem;
            background-color: #f8f9fa;
            border-radius: 10px;
            margin-bottom: 1rem;
            border: 1px solid #e0e0e0;
        }

        /* Current session info */
        .session-info {
            margin-bottom: 1rem;
            padding: 0.5rem 1rem;
            background-color: #e3f2fd;
            border-radius: 5px;
            font-size: 0.9rem;
            color: #1565c0;
        }
    </style>
    """, unsafe_allow_html=True)

    try:
        # Check if QA system is initialized
        if 'qa_system' not in st.session_state or st.session_state.qa_system is None:
            # Show available sessions if any
            sessions = st.session_state.persistence.list_available_sessions()
            if sessions:
                st.markdown('<div class="session-selector">', unsafe_allow_html=True)
                st.subheader("💾 Load Previous Chat")
                selected_session = st.selectbox(
                    "Select a previous conversation:",
                    options=[s['session_id'] for s in sessions],
                    format_func=lambda x: f"Chat from {format_session_date(x)}"
                )

                if selected_session and st.button("Load Selected Chat"):
                    with st.spinner("Loading previous chat..."):
                        messages = st.session_state.persistence.load_chat_history(selected_session)
                        if messages:
                            st.session_state.messages = messages

                        # Load vector store
                        vector_store = st.session_state.persistence.load_vector_store(selected_session)
                        if vector_store:
                            st.session_state.vector_store = vector_store
                            st.session_state.qa_system = initialize_qa_system(vector_store)
                            st.session_state.current_session_id = selected_session
                            st.rerun()
                st.markdown('</div>', unsafe_allow_html=True)

            st.warning("Please upload documents or select a previous chat to begin.")
            return

        # Initialize chat history
        if 'messages' not in st.session_state:
            st.session_state.messages = []

        # Display current session info if available
        if 'current_session_id' in st.session_state:
            session_date = format_session_date(st.session_state.current_session_id)
            st.markdown(f"""
                <div class="session-info">
                    📅 Current Session: {session_date}
                </div>
            """, unsafe_allow_html=True)

        # Display chat history
        for message in st.session_state.messages:
            if isinstance(message, HumanMessage):
                st.markdown(f"""
                <div class="user-message">
                    🧑‍💼 <strong>You:</strong><br>{message.content}
                </div>
                """, unsafe_allow_html=True)
            elif isinstance(message, AIMessage):
                st.markdown(f"""
                <div class="assistant-message">
                    🤖 <strong>Assistant:</strong>
                    {format_assistant_response(message.content)}
                </div>
                """, unsafe_allow_html=True)

        # Chat input
        if prompt := st.chat_input("Ask about your documents..."):
            with st.spinner("Analyzing..."):
                # Validate input
                if not prompt.strip():
                    st.warning("Please enter a valid question.")
                    return

                try:
                    # Create and append human message
                    human_message = HumanMessage(content=prompt)
                    st.session_state.messages.append(human_message)

                    # Get response from QA system
                    response = st.session_state.qa_system.invoke({
                        "input": prompt,
                        "chat_history": []
                    })

                    if response:
                        ai_message = AIMessage(content=str(response))
                        st.session_state.messages.append(ai_message)

                        # Save chat history
                        if 'current_session_id' in st.session_state:
                            st.session_state.persistence.save_chat_history(
                                st.session_state.messages,
                                st.session_state.current_session_id,
                                metadata={
                                    'last_question': prompt,
                                    'last_updated': datetime.now().isoformat()
                                }
                            )

                        st.rerun()
                    else:
                        st.error("No valid response received. Please try again.")

                except Exception as e:
                    st.error(f"Error processing response: {str(e)}")
                    import traceback
                    st.error(traceback.format_exc())
                    # Remove the failed message attempt
                    if st.session_state.messages:
                        st.session_state.messages.pop()
    except Exception as e:
        st.error(f"An unexpected error occurred: {e}")
        import traceback
        st.error(traceback.format_exc())