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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
"""

NotebookLM-style response generator with professional formatting.

"""

from typing import List, Dict
import config
import re


class SimpleGenerator:
    """Lightweight generator with NotebookLM-quality formatting."""
    
    def __init__(self):
        self.ready = True
    
    def _clean_and_format_text(self, text: str) -> str:
        """Clean and format text with proper spacing like NotebookLM."""
        # Fix spacing after punctuation
        text = re.sub(r'([.!?])([A-Z])', r'\1 \2', text)
        # Remove multiple spaces
        text = re.sub(r'\s+', ' ', text)
        # Add proper line breaks after sentences
        text = re.sub(r'([.!?])\s+', r'\1\n\n', text)
        return text.strip()
    
    def _extract_key_terms(self, text: str) -> List[str]:
        """Extract key terms that should be bolded."""
        # Look for capitalized terms, technical terms
        terms = []
        
        # Find terms in quotes
        quoted = re.findall(r'"([^"]+)"', text)
        terms.extend(quoted)
        
        # Find repeated important words (appear 2+ times)
        words = re.findall(r'\b[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*\b', text)
        word_count = {}
        for word in words:
            word_count[word] = word_count.get(word, 0) + 1
        
        # Add words that appear multiple times
        terms.extend([w for w, count in word_count.items() if count >= 2])
        
        return list(set(terms))
    
    def _apply_bold_formatting(self, text: str) -> str:
        """Apply bold formatting to key terms like NotebookLM."""
        key_terms = self._extract_key_terms(text)
        
        # Bold key terms
        for term in key_terms:
            if len(term) > 3:  # Skip very short terms
                text = re.sub(rf'\b({re.escape(term)})\b', r'**\1**', text, count=1)
        
        # Bold specific patterns
        # Numbers with context
        text = re.sub(r'\b(\d+)\s+(observations?|years?|months?|quarters?)', r'**\1 \2**', text)
        
        return text
    
    def _create_structured_response(self, context: str, query: str) -> str:
        """Create a NotebookLM-style structured response."""
        # Split into paragraphs
        paragraphs = [p.strip() for p in context.split('\n\n') if len(p.strip()) > 50]
        
        # Remove duplicates
        unique_paras = []
        seen = set()
        for para in paragraphs:
            para_key = para.lower()[:150]
            if para_key not in seen:
                unique_paras.append(para)
                seen.add(para_key)
                if len(unique_paras) >= 5:
                    break
        
        if not unique_paras:
            return context[:1000]
        
        # Build NotebookLM-style response
        response = ""
        
        # Main explanation (first paragraph - cleaned and formatted)
        main_para = self._clean_and_format_text(unique_paras[0])
        main_para = self._apply_bold_formatting(main_para)
        response += main_para + "\n\n"
        
        # Add structured details if more content available
        if len(unique_paras) > 1:
            response += "### Key Points:\n\n"
            
            for i, para in enumerate(unique_paras[1:4], 1):
                # Extract first 2-3 sentences
                sentences = [s.strip() for s in para.split('.') if len(s.strip()) > 20]
                if sentences:
                    detail = self._clean_and_format_text('. '.join(sentences[:2]) + '.')
                    detail = self._apply_bold_formatting(detail)
                    response += f"{i}. {detail}\n\n"
        
        return response.strip()
    
    def generate_response(

        self,

        prompt: str,

        context: str = "",

        use_case: str = "explanation",

        metadatas: List[Dict] = None,

        **kwargs

    ) -> str:
        """

        Generate a NotebookLM-quality response with strict citations.

        

        Args:

            prompt: User query

            context: Retrieved context from documents

            use_case: Type of response (explanation, summary, qa,notes)

            metadatas: Metadata for each context chunk (for citations)

            

        Returns:

            Professional formatted response with inline citations

        """
        if not context:
            return (
                "I don't have enough information from your uploaded documents to answer this question. "
                "Please upload relevant study materials first, or try rephrasing your question."
            )
        
        # Use specialized prompts based on use case
        if use_case == "summary":
            response = self._create_summary_with_citations(context, prompt, metadatas)
        elif use_case == "notes":
            response = self._create_notes_with_citations(context, prompt, metadatas)
        elif use_case == "qa":
            response = self._create_qa_with_citations(context, prompt, metadatas)
        else:  # Default to explanation
            response = self._create_structured_response_with_citations(context, prompt, metadatas)
        
        return response
    
    def _create_structured_response_with_citations(

        self, 

        context: str, 

        query: str,

        metadatas: List[Dict] = None

    ) -> str:
        """Create NotebookLM-style response with inline citations."""
        # Split into paragraphs
        paragraphs = [p.strip() for p in context.split('\n\n') if len(p.strip()) > 50]
        
        # Remove duplicates
        unique_paras = []
        seen = set()
        for para in paragraphs:
            para_key = para.lower()[:150]
            if para_key not in seen:
                unique_paras.append(para)
                seen.add(para_key)
                if len(unique_paras) >= 5:
                    break
        
        if not unique_paras:
            return context[:1000]
        
        # Build response with citations
        response = ""
        
        # Main explanation (first paragraph - cleaned and formatted)
        main_para = self._clean_and_format_text(unique_paras[0])
        main_para = self._apply_bold_formatting(main_para)
        
        # Add citation to end of main paragraph
        cite_text = self._get_citation(0, metadatas) if metadatas else ""
        response += main_para + cite_text + "\n\n"
        
        # Add structured details if more content available
        if len(unique_paras) > 1:
            response += "### Key Points:\n\n"
            
            for i, para in enumerate(unique_paras[1:4], 1):
                # Extract first 2-3 sentences
                sentences = [s.strip() for s in para.split('.') if len(s.strip()) > 20]
                if sentences:
                    detail = self._clean_and_format_text('. '.join(sentences[:2]) + '.')
                    detail = self._apply_bold_formatting(detail)
                    
                    # Add citation
                    cite_text = self._get_citation(i, metadatas) if metadatas and i < len(metadatas) else ""
                    response += f"{i}. {detail}{cite_text}\n\n"
        
        return response.strip()
    
    def _get_citation(self, index: int, metadatas: List[Dict] = None) -> str:
        """Generate inline citation from metadata."""
        if not metadatas or index >= len(metadatas):
            return ""
        
        meta = metadatas[index]
        filename = meta.get('filename', 'Unknown')
        
        # Remove file extension for cleaner citation
        clean_name = filename.replace('.pdf', '').replace('.docx', '').replace('.txt', '')
        
        return f" **[{clean_name}]**"
    
    def _create_summary_with_citations(

        self, 

        context: str, 

        query: str,

        metadatas: List[Dict] = None

    ) -> str:
        """Create a summary with citations."""
        sentences = []
        seen = set()
        for s in context.split('.'):
            s_clean = s.strip()
            if len(s_clean) > 40 and s_clean.lower() not in seen:
                sentences.append(s_clean)
                seen.add(s_clean.lower())
                if len(sentences) >= 6:
                    break
        
        if not sentences:
            return context[:800]
        
        response = "## Summary\n\n"
        for i, point in enumerate(sentences, 1):
            cite = self._get_citation(i-1, metadatas) if metadatas else ""
            response += f"{i}. {point}.{cite}\n\n"
        
        return response.strip()
    
    def _create_qa_with_citations(

        self, 

        context: str, 

        query: str,

        metadatas: List[Dict] = None

    ) -> str:
        """Answer with strict source grounding."""
        paragraphs = [p.strip() for p in context.split('\n\n') if len(p.strip()) > 50]
        
        if not paragraphs:
            sentences = [s.strip() + '.' for s in context.split('.') if len(s.strip()) > 30]
            response = ' '.join(sentences[:6])
            cite = self._get_citation(0, metadatas) if metadatas else ""
            return response + cite
        
        # Remove duplicates
        unique_paras = []
        seen = set()
        for para in paragraphs:
            para_key = para.lower()[:150]
            if para_key not in seen:
                unique_paras.append(para)
                seen.add(para_key)
                if len(unique_paras) >= 3:
                    break
        
        # Fix spacing and add citations
        response = unique_paras[0] if unique_paras else context[:800]
        response = re.sub(r'([.!?])([A-Z])', r'\1 \2', response)
        cite = self._get_citation(0, metadatas) if metadatas else ""
        response += cite
        
        # Add supporting details if available
        if len(unique_paras) > 1:
            second_para = re.sub(r'([.!?])([A-Z])', r'\1 \2', unique_paras[1])
            cite2 = self._get_citation(1, metadatas) if metadatas and len(metadatas) > 1 else ""
            response += "\n\n" + second_para + cite2
        
        return response.strip()
    
    def _create_notes_with_citations(

        self, 

        context: str, 

        query: str,

        metadatas: List[Dict] = None

    ) -> str:
        """Create study notes with source attribution."""
        sections = [s.strip() for s in context.split('\n\n') if len(s.strip()) > 40]
        
        # Remove duplicates
        unique_sections = []
        seen = set()
        for section in sections:
            section_key = section.lower()[:100]
            if section_key not in seen:
                unique_sections.append(section)
                seen.add(section_key)
                if len(unique_sections) >= 6:
                    break
        
        if not unique_sections:
            return context[:1000]
        
        response = "## Study Notes\n\n"
        
        for i, section in enumerate(unique_sections, 1):
            sentences = [s.strip() for s in section.split('.') if len(s.strip()) > 20]
            
            if sentences:
                heading = sentences[0]
                cite = self._get_citation(i-1, metadatas) if metadatas else ""
                response += f"### {i}. {heading}{cite}\n\n"
                
                for sent in sentences[1:3]:
                    response += f"- {sent}\n"
                response += "\n"
        
        return response.strip()
    
    def _create_summary(self, context: str, query: str) -> str:
        """Create a clean summary from retrieved context."""
        # Extract key sentences - remove duplicates
        sentences = []
        seen = set()
        for s in context.split('.'):
            s_clean = s.strip()
            # Remove duplicates and filter short/low-quality sentences
            if len(s_clean) > 40 and s_clean.lower() not in seen:
                sentences.append(s_clean)
                seen.add(s_clean.lower())
                if len(sentences) >= 6:
                    break
        
        if not sentences:
            return context[:800]
        
        response = "## Summary\n\n"
        for i, point in enumerate(sentences, 1):
            response += f"{i}. {point}.\n\n"
        
        return response.strip()
    
    def _create_explanation(self, context: str, query: str) -> str:
        """Create a well-formatted explanation from retrieved context."""
        # Remove duplicate paragraphs
        paragraphs = []
        seen = set()
        for para in context.split('\n\n'):
            para_clean = para.strip()
            # Keep unique, substantial paragraphs
            if len(para_clean) > 50:
                para_lower = para_clean.lower()[:200]  # Check first 200 chars for duplicates
                if para_lower not in seen:
                    paragraphs.append(para_clean)
                    seen.add(para_lower)
        
        if not paragraphs:
            # Fallback: split by sentence
            sentences = [s.strip() + '.' for s in context.split('.') if len(s.strip()) > 30]
            return ' '.join(sentences[:8])
        
        # Build clean, formatted response with proper spacing
        response = ""
        
        # Add first paragraph as main explanation (ensure spacing between sentences)
        first_para = paragraphs[0]
        # Add space after punctuation if missing
        import re
        first_para = re.sub(r'([.!?])([A-Z])', r'\1 \2', first_para)
        response += first_para
        
        # Add additional details if available
        if len(paragraphs) > 1:
            response += "\n\n### Key Points:\n\n"
            for i, para in enumerate(paragraphs[1:4], 1):  # Max 3 additional points
                # Extract first sentence as bullet
                sentences = [s.strip() for s in para.split('.') if len(s.strip()) > 20]
                if sentences:
                    response += f"• {sentences[0]}.\n"
                    if len(sentences) > 1 and len(sentences[1]) > 20:
                        response += f"  {sentences[1]}.\n"
                    response += "\n"
        
        return response.strip()
    
    def _create_qa(self, context: str, query: str) -> str:
        """Answer a question with clean formatting."""
        # Find most relevant paragraphs
        paragraphs = [p.strip() for p in context.split('\n\n') if len(p.strip()) > 50]
        
        if not paragraphs:
            sentences = [s.strip() + '.' for s in context.split('.') if len(s.strip()) > 30]
            return ' '.join(sentences[:6])
        
        # Remove duplicates
        unique_paras = []
        seen = set()
        for para in paragraphs:
            para_key = para.lower()[:150]
            if para_key not in seen:
                unique_paras.append(para)
                seen.add(para_key)
                if len(unique_paras) >= 3:
                    break
        
        # Fix spacing in response
        import re
        response = unique_paras[0] if unique_paras else context[:800]
        response = re.sub(r'([.!?])([A-Z])', r'\1 \2', response)
        
        # Add supporting details if available
        if len(unique_paras) > 1:
            second_para = re.sub(r'([.!?])([A-Z])', r'\1 \2', unique_paras[1])
            response += "\n\n" + second_para
        
        return response.strip()
    
    def _create_notes(self, context: str, query: str) -> str:
        """Create well-structured study notes."""
        # Split and clean sections
        sections = [s.strip() for s in context.split('\n\n') if len(s.strip()) > 40]
        
        # Remove duplicates
        unique_sections = []
        seen = set()
        for section in sections:
            section_key = section.lower()[:100]
            if section_key not in seen:
                unique_sections.append(section)
                seen.add(section_key)
                if len(unique_sections) >= 6:
                    break
        
        if not unique_sections:
            return context[:1000]
        
        response = "## Study Notes\n\n"
        
        for i, section in enumerate(unique_sections, 1):
            # Extract key information
            sentences = [s.strip() for s in section.split('.') if len(s.strip()) > 20]
            
            if sentences:
                # Use first sentence as heading
                heading = sentences[0]
                response += f"### {i}. {heading}\n\n"
                
                # Add bullet points for remaining content
                for sent in sentences[1:3]:  # Max 2 additional sentences
                    response += f"- {sent}\n"
                response += "\n"
        
        return response.strip()