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
 
 
 
 
 
 
 
 
 
 
 
 
 
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import os
import streamlit as st
from datetime import datetime
import re
from werkzeug.utils import secure_filename

from src.gpp import GPP, GPPConfig
from src.qa import AnswerGenerator

# Check if we need to modify the AnswerGenerator class to accept conversation context
# If the original implementation doesn't support this, we'll create a wrapper

class ContextAwareAnswerGenerator:
    """Wrapper around AnswerGenerator to include conversation context"""
    
    def __init__(self, chunks):
        self.chunks = chunks
        self.original_generator = AnswerGenerator(chunks)
    
    def answer(self, question, conversation_context=None):
        """
        Generate answer with conversation context
        
        Args:
            chunks: Document chunks to search
            question: Current question
            conversation_context: List of previous Q&A for context
            
        Returns:
            answer, supporting_chunks
        """
        # If no conversation context or original implementation supports it directly
        if conversation_context is None or len(conversation_context) <= 1:
            return self.original_generator.answer(question)
            
        # Otherwise, enhance the question with context
        # Create a contextual prompt by summarizing previous exchanges
        context_prompt = "Based on our conversation so far:\n"
        
        # Include the last few exchanges (limiting to prevent context getting too large)
        max_history = min(len(conversation_context) - 1, 4)  # Last 4 exchanges maximum
        for i in range(max(0, len(conversation_context) - max_history - 1), len(conversation_context) - 1, 2):
            if i < len(conversation_context) and i+1 < len(conversation_context):
                user_q = conversation_context[i]["content"]
                assistant_a = conversation_context[i+1]["content"]
                context_prompt += f"You were asked: '{user_q}'\n"
                context_prompt += f"You answered: '{assistant_a}'\n"
        
        context_prompt += f"\nNow answer this follow-up question: {question}"
        
        # Use the enhanced prompt
        return self.original_generator.answer(context_prompt)

# --- Page Configuration ---
st.set_page_config(
    page_title="Document Intelligence Q&A",
    page_icon="πŸ“„",
    layout="wide"
)

# --- Session State Initialization ---
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []  # List of {role: 'user'/'assistant', content: str}
if 'parsed' not in st.session_state:
    st.session_state.parsed = None
if "selected_chunks" not in st.session_state:
    st.session_state.selected_chunks = []
if "conversation_context" not in st.session_state:
    st.session_state.conversation_context = []

# --- Custom CSS for styling ---
st.markdown(
    """
    <style>
    /* Global Styles */
    body {
        background-color: #fafafa;
        font-family: 'Helvetica Neue', sans-serif;
    }
    
    /* Header Styles */
    .main-header {
        margin-bottom: 2rem;
    }
    
    /* Card Styles */
    .card {
        background: white;
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
    }
    
    /* Button Styles */
    .stButton>button {
        background-color: #4361ee;
        color: white;
        border-radius: 4px;
        border: none;
        padding: 8px 16px;
        font-weight: 500;
    }
    
    .stButton>button:hover {
        background-color: #3a56d4;
    }
    
    /* Input Styles */
    .stTextInput>div>div>input {
        border-radius: 4px;
        border: 1px solid #e0e0e0;
    }
    
    /* Code Block Styles */
    pre {
        background-color: #f5f5f5;
        padding: 12px;
        border-radius: 4px;
        font-size: 14px;
    }
    
    /* Hide Streamlit footer */
    footer {
        display: none;
    }
    
    /* Sidebar Styles */
    .css-18e3th9 {
        padding-top: 1rem;
    }
    
    /* Expander styles */
    .streamlit-expanderHeader {
        font-size: 1rem;
        font-weight: 500;
    }
    
    /* Chat Interface Styles */
    .chat-container {
        display: flex;
        flex-direction: column;
        gap: 12px;
        margin-top: 20px;
        margin-bottom: 20px;
    }
    
    .chat-message {
        display: flex;
        margin-bottom: 10px;
    }
    
    .user-message {
        justify-content: flex-end;
    }
    
    .assistant-message {
        justify-content: flex-start;
    }
    
    .message-content {
        padding: 12px 16px;
        border-radius: 18px;
        max-width: 80%;
        overflow-wrap: break-word;
    }
    
    .user-message .message-content {
        background-color: #4361ee;
        color: white;
        border-bottom-right-radius: 4px;
    }
    
    .assistant-message .message-content {
        background-color: #f0f2f6;
        color: #1e1e1e;
        border-bottom-left-radius: 4px;
    }
    
    .message-content p {
        margin: 0;
        padding: 0;
    }
    
    /* Empty chat placeholder style */
    .empty-chat-placeholder {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        height: 300px;
        background-color: #f8f9fa;
        border-radius: 8px;
        margin-bottom: 20px;
        text-align: center;
        color: #6c757d;
    }
    
    .empty-chat-icon {
        font-size: 40px;
        margin-bottom: 16px;
        color: #adb5bd;
    }
    
    /* Message typing indicator */
    .typing-indicator {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        margin-top: 8px;
    }
    
    .typing-indicator span {
        height: 8px;
        width: 8px;
        background-color: #4361ee;
        border-radius: 50%;
        margin: 0 2px;
        display: inline-block;
        opacity: 0.7;
    }
    
    .typing-indicator span:nth-child(1) {
        animation: pulse 1s infinite;
    }
    
    .typing-indicator span:nth-child(2) {
        animation: pulse 1s infinite 0.2s;
    }
    
    .typing-indicator span:nth-child(3) {
        animation: pulse 1s infinite 0.4s;
    }
    
    @keyframes pulse {
        0% { transform: scale(1); opacity: 0.7; }
        50% { transform: scale(1.2); opacity: 1; }
        100% { transform: scale(1); opacity: 0.7; }
    }
    
    /* Spinner */
    .stSpinner > div > div {
        border-top-color: #4361ee !important;
    }
    
    /* Info box */
    .stAlert {
        border-radius: 8px;
    }
    </style>
    """, unsafe_allow_html=True
)

# --- Left Sidebar: Instructions & Upload ---
with st.sidebar:
    # App info section
    st.image("https://img.icons8.com/ios-filled/50/4A90E2/document.png", width=40)
    st.title("Document Intelligence")
    st.caption(f"Last updated: {datetime.now().strftime('%Y-%m-%d')}")
    
    with st.expander("How It Works", expanded=True):
        st.markdown(
            """
            1. **Upload PDF**: Select and parse your document
            2. **Ask Questions**: Type your query about the document
            3. **Get Answers**: AI analyzes and responds with insights
            4. **View Evidence**: See supporting chunks in the right sidebar
            """
        )
    
    st.markdown("---")
    
    # Upload section
    st.subheader("Upload Document")
    uploaded_file = st.file_uploader("Select a PDF", type=["pdf"], help="Upload a PDF file to analyze")
    
    if uploaded_file:
        try:
            filename = secure_filename(uploaded_file.name)
            if not re.match(r'^[\w\-. ]+$', filename):
                st.error("Invalid file name. Please rename your file.")
            else:
                col1, col2 = st.columns(2)
                with col1:
                    if st.button("Parse pdf", use_container_width=True, key="parse_button"):
                        output_dir = os.path.join("./parsed", filename)
                        os.makedirs(output_dir, exist_ok=True)
                        pdf_path = os.path.join(output_dir, filename)
                        
                        with open(pdf_path, "wb") as f:
                            f.write(uploaded_file.getbuffer())
                        
                        with st.spinner("Parsing document..."):
                            try:
                                gpp = GPP(GPPConfig())
                                parsed = gpp.run(pdf_path, output_dir)
                                st.session_state.parsed = parsed
                                st.session_state.chat_history = []  # Reset chat when new document is parsed
                                st.session_state.conversation_context = []  # Reset conversation context
                                st.session_state.selected_chunks = []  # Reset selected chunks
                                st.success("Document parsed successfully!")
                            except Exception as e:
                                st.error(f"Parsing failed: {str(e)}")
                                st.session_state.parsed = None
                with col2:
                    if st.button("Clear", use_container_width=True, key="clear_button"):
                        st.session_state.parsed = None
                        st.session_state.selected_chunks = []
                        st.session_state.chat_history = []
                        st.session_state.conversation_context = []
                        st.experimental_rerun()
        except Exception as e:
            st.error(f"Upload error: {str(e)}")
    
    # Display document preview if parsed
    if st.session_state.parsed:
        st.markdown("---")
        st.subheader("Document Preview")
        parsed = st.session_state.parsed
        
        # Layout PDF
        layout_pdf = parsed.get("layout_pdf")
        if layout_pdf and os.path.exists(layout_pdf):
            with st.expander("View Layout PDF", expanded=False):
                st.markdown(f"[Open in new tab]({layout_pdf})")
        
        # Content preview
        md_path = parsed.get("md_path")
        if md_path and os.path.exists(md_path):
            try:
                with open(md_path, 'r', encoding='utf-8') as md_file:
                    md_text = md_file.read()
                with st.expander("Content Preview", expanded=False):
                    st.markdown(f"<pre style='font-size:12px;max-height:300px;overflow-y:auto'>{md_text[:3000]}{'...' if len(md_text)>3000 else ''}</pre>", unsafe_allow_html=True)
            except Exception as e:
                st.warning(f"Could not preview content: {str(e)}")

# --- Main Content Area ---
# Create a two-column layout for main content
main_col, evidence_col = st.columns([3, 1])

with main_col:
    st.markdown("<div class='main-header'>", unsafe_allow_html=True)
    st.title("Document Q&A")
    st.markdown("</div>", unsafe_allow_html=True)

    if not st.session_state.parsed:
        st.info("πŸ‘ˆ Please upload and parse a document to begin asking questions.")
    else:
        # Q&A Section with chat-like interface
        st.markdown("<div class='card'>", unsafe_allow_html=True)
        question =     st.text_input(
            "Ask a question about your document:",
            key="question_input",
            placeholder="E.g., 'What are the key findings?' or 'Summarize the data'",
            on_change=None  # Ensure the input field gets cleared naturally after submission
        )
    
    col_btn1, col_btn2 = st.columns([4, 1])
    with col_btn1:
        submit_button = st.button("Get Answer", use_container_width=True)
    with col_btn2:
        clear_chat = st.button("Clear Chat", use_container_width=True)
    
    # Initialize chat history
    if "chat_history" not in st.session_state:
        st.session_state.chat_history = []
        
    # Clear chat when button is pressed
    if clear_chat:
        st.session_state.chat_history = []
        st.session_state.conversation_context = []
        st.session_state.selected_chunks = []
        st.experimental_rerun()
        
    if submit_button and question:
        with st.spinner("Analyzing document and generating answer..."):
            try:
                # Add user question to chat history
                st.session_state.chat_history.append({"role": "user", "content": question})
                
                # Generate answer using conversation context
                generator = ContextAwareAnswerGenerator(st.session_state.parsed['chunks'])
                answer, supporting_chunks = generator.answer(
                    question, conversation_context=st.session_state.chat_history
                )
                
                # Add assistant response to chat history
                st.session_state.chat_history.append({"role": "assistant", "content": answer})
                
                # Store supporting chunks in session state for the right sidebar
                st.session_state.selected_chunks = supporting_chunks
                
                # Clear the question input
                question = ""
                
            except Exception as e:
                st.error(f"Failed to generate answer: {str(e)}")
                st.session_state.selected_chunks = []
                
    # Display chat history
    st.markdown("<div class='chat-container'>", unsafe_allow_html=True)
    
    if not st.session_state.chat_history:
        # Show empty chat state with icon
        st.markdown("""
        <div class='empty-chat-placeholder'>
            <div class='empty-chat-icon'>πŸ’¬</div>
            <p>Ask questions about your document to start a conversation</p>
        </div>
        """, unsafe_allow_html=True)
    else:
        for message in st.session_state.chat_history:
            if message["role"] == "user":
                st.markdown(f"""
                <div class='chat-message user-message'>
                    <div class='message-content'>
                        <p>{message["content"]}</p>
                    </div>
                </div>
                """, unsafe_allow_html=True)
            else:
                st.markdown(f"""
                <div class='chat-message assistant-message'>
                    <div class='message-content'>
                        <p>{message["content"]}</p>
                    </div>
                </div>
                """, unsafe_allow_html=True)
    st.markdown("</div>", unsafe_allow_html=True)
    st.markdown("</div>", unsafe_allow_html=True)

# --- Supporting Evidence in the right column ---
with evidence_col:
    if st.session_state.parsed:
        st.markdown("### Supporting Evidence")
        
        if not st.session_state.selected_chunks:
            st.info("Evidence chunks will appear here after you ask a question.")
        else:
            for idx, chunk in enumerate(st.session_state.selected_chunks):
                with st.expander(f"Evidence #{idx+1}", expanded=True):
                    st.markdown(f"**Type:** {chunk['type'].capitalize()}")
                    st.markdown(chunk.get('narration', 'No narration available'))
                    
                    # Display table if available
                    if 'table_structure' in chunk:
                        st.write("**Table Data:**")
                        st.dataframe(chunk['table_structure'], use_container_width=True)
                    
                    # Display images if available
                    for blk in chunk.get('blocks', []):
                        if blk.get('type') == 'img_path' and 'images_dir' in st.session_state.parsed:
                            img_path = os.path.join(st.session_state.parsed['images_dir'], blk.get('img_path',''))
                            if os.path.exists(img_path):
                                st.image(img_path, use_column_width=True)

# -- Error handling wrapper -- 
def handle_error(func):
    try:
        func()
    except Exception as e:
        st.error(f"An unexpected error occurred: {str(e)}")
        st.info("Please refresh the page and try again.")

# Wrap the entire app in the error handler
handle_error(lambda: None)