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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c8af35
 
 
042fd4f
1518c87
042fd4f
 
4c8af35
042fd4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c8af35
 
042fd4f
 
4c8af35
 
 
 
 
 
042fd4f
4c8af35
 
042fd4f
 
4c8af35
 
 
 
 
 
172912a
042fd4f
 
 
 
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import os
import gradio as gr
import google.generativeai as genai
from tavily import TavilyClient
from sentence_transformers import SentenceTransformer, CrossEncoder
import markdown
from weasyprint import HTML, CSS as WeasyCSS
from datetime import datetime
import tempfile
import re

from research_agent.config import AgentConfig
from research_agent.agent import get_clarifying_questions, research_and_plan, write_report_stream

google_key = os.getenv("GOOGLE_API_KEY")
tavily_key = os.getenv("TAVILY_API_KEY")

if not google_key or not tavily_key:
    raise ValueError("API keys not found.")

# Enhanced CSS for a professional research interface
CSS = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --bg-tertiary: #334155;
    --text-primary: #f1f5f9;
    --text-secondary: #cbd5e1;
    --text-muted: #94a3b8;
    --border-color: #334155;
    --success-color: #10b981;
    --warning-color: #f59e0b;
    --error-color: #ef4444;
}
body, .gradio-container {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
    background-color: var(--bg-primary) !important;
    color: var(--text-primary) !important;
}
.gradio-container {
    max-width: 1200px !important;
    margin: 0 auto !important;
    padding: 2rem !important;
}
/* Header Styling */
.header-container {
    text-align: center;
    margin-bottom: 3rem;
    padding: 2rem;
    background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--bg-tertiary) 100%);
    border-radius: 16px;
    border: 1px solid var(--border-color);
}
h1 {
    font-size: 3rem;
    font-weight: 700;
    background: linear-gradient(135deg, #60a5fa 0%, #a78bfa 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 0.5rem;
}
.subtitle {
    color: var(--text-secondary);
    font-size: 1.25rem;
    font-weight: 400;
}
/* Status Bar */
.status-bar {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1rem 1.5rem;
    margin-bottom: 2rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.status-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--success-color);
    animation: pulse 2s infinite;
}
@keyframes pulse {
    0% { opacity: 1; }
    50% { opacity: 0.5; }
    100% { opacity: 1; }
}
/* Chat Interface */
#chatbot {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
    border-radius: 16px !important;
    overflow: hidden !important;
}
#chatbot .message {
    border: none !important;
    padding: 1.5rem !important;
}
#chatbot .user {
    background: var(--bg-tertiary) !important;
    border-left: 4px solid var(--primary-color) !important;
}
#chatbot .bot {
    background: var(--bg-secondary) !important;
}
/* Progress Indicators */
.progress-container {
    background: var(--bg-tertiary);
    border-radius: 8px;
    padding: 1rem;
    margin: 1rem 0;
}
.progress-bar {
    height: 4px;
    background: var(--border-color);
    border-radius: 2px;
    overflow: hidden;
    margin-top: 0.5rem;
}
.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary-color) 0%, #60a5fa 100%);
    transition: width 0.3s ease;
    animation: shimmer 2s infinite;
}
@keyframes shimmer {
    0% { opacity: 0.8; }
    50% { opacity: 1; }
    100% { opacity: 0.8; }
}
/* Input Area */
.input-container {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1.5rem;
    margin-top: 2rem;
}
#chat-input textarea {
    background: var(--bg-tertiary) !important;
    color: var(--text-primary) !important;
    border: 1px solid var(--border-color) !important;
    border-radius: 8px !important;
    padding: 1rem !important;
    font-size: 1rem !important;
    transition: all 0.2s ease !important;
}
#chat-input textarea:focus {
    border-color: var(--primary-color) !important;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1) !important;
}
/* Buttons */
.gr-button {
    background: var(--primary-color) !important;
    color: white !important;
    border: none !important;
    border-radius: 8px !important;
    padding: 0.75rem 1.5rem !important;
    font-weight: 600 !important;
    transition: all 0.2s ease !important;
    cursor: pointer !important;
}
.gr-button:hover {
    background: var(--primary-hover) !important;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3) !important;
}
.gr-button.secondary {
    background: var(--bg-tertiary) !important;
    color: var(--text-primary) !important;
}
.gr-button.secondary:hover {
    background: #475569 !important;
}
/* Report Display */
.report-section {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 2rem;
    margin: 1rem 0;
}
.report-section h2 {
    color: var(--text-primary);
    font-size: 1.75rem;
    font-weight: 600;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 2px solid var(--border-color);
}
.report-section h3 {
    color: var(--text-secondary);
    font-size: 1.25rem;
    font-weight: 500;
    margin: 1.5rem 0 0.75rem 0;
}
.source-list {
    background: var(--bg-tertiary);
    border-radius: 8px;
    padding: 1rem;
    margin-top: 1rem;
}
.source-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0;
    color: var(--text-secondary);
    text-decoration: none;
    transition: color 0.2s ease;
}
.source-item:hover {
    color: var(--primary-color);
}
/* Loading States */
.thinking-indicator {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    color: var(--text-secondary);
    font-style: italic;
}
.thinking-dots {
    display: flex;
    gap: 0.25rem;
}
.thinking-dots span {
    width: 6px;
    height: 6px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
}
.thinking-dots span:nth-child(1) { animation-delay: -0.32s; }
.thinking-dots span:nth-child(2) { animation-delay: -0.16s; }
@keyframes bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}
/* Export Options */
.export-container {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1.5rem;
    margin-top: 2rem;
}
.export-buttons {
    display: flex;
    gap: 1rem;
    margin-top: 1rem;
}
/* Responsive Design */
@media (max-width: 768px) {
    .gradio-container {
        padding: 1rem !important;
    }
    
    h1 {
        font-size: 2rem;
    }
    
    .export-buttons {
        flex-direction: column;
    }
}
"""

# Initialize models
config = AgentConfig()
writer_model, planner_model, embedding_model, reranker, tavily_client = None, None, None, None, None
IS_PROCESSING = False

def initialize_models():
    """Initializes all the models and clients using keys from environment variables."""
    global writer_model, planner_model, embedding_model, reranker, tavily_client, IS_PROCESSING
    try:
        genai.configure(api_key=google_key)
        tavily_client = TavilyClient(api_key=tavily_key)
        writer_model = genai.GenerativeModel(config.WRITER_MODEL)
        planner_model = genai.GenerativeModel(config.WRITER_MODEL)
        embedding_model = SentenceTransformer('all-MiniLM-L6-v2', device='cpu')
        reranker = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2', device='cpu')
    except Exception as e:
        print(f"FATAL: Failed to initialize models. Error: {str(e)}")
        raise gr.Error(f"Failed to initialize models. Please check the logs. Error: {str(e)}")
    IS_PROCESSING = False
    print("Models and clients initialized successfully.")

# Initialize models on startup
initialize_models()

# Helper functions for better UI
def format_progress_message(message):
    """Formats progress messages with visual indicators"""
    if "Step" in message:
        return f"πŸ”„ **{message}**"
    elif "Searching" in message:
        return f"πŸ” {message}"
    elif "Found" in message:
        return f"βœ… {message}"
    elif "Processing" in message:
        return f"βš™οΈ {message}"
    elif "Writing" in message or "Synthesizing" in message:
        return f"✍️ {message}"
    elif "Fact-checking" in message:
        return f"πŸ”Ž {message}"
    else:
        return message

def export_to_pdf(report_content, filename="research_report.pdf"):
    """Exports the report to PDF with proper formatting"""
    try:
        # Convert markdown to HTML
        html_content = markdown.markdown(report_content, extensions=['extra', 'codehilite'])
        
        # Add CSS for PDF
        pdf_css = """
        @page { size: A4; margin: 2cm; }
        body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
        h1 { color: #2563eb; border-bottom: 2px solid #2563eb; padding-bottom: 10px; }
        h2 { color: #1e40af; margin-top: 30px; }
        h3 { color: #3730a3; }
        pre { background: #f3f4f6; padding: 10px; border-radius: 4px; }
        code { background: #e5e7eb; padding: 2px 4px; border-radius: 2px; }
        blockquote { border-left: 4px solid #2563eb; padding-left: 16px; color: #6b7280; }
        """
        
        # Create PDF
        with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as tmp_file:
            HTML(string=f"<html><body>{html_content}</body></html>").write_pdf(
                tmp_file.name,
                stylesheets=[WeasyCSS(string=pdf_css)]
            )
            return tmp_file.name
    except Exception as e:
        print(f"Error creating PDF: {e}")
        return None

def chat_step_wrapper(user_input, history, current_agent_state, topic_state, progress_state):
    """Enhanced wrapper with progress tracking"""
    global IS_PROCESSING
    if IS_PROCESSING:
        print("Ignoring duplicate request while processing.")
        if False: yield
        return

    IS_PROCESSING = True
    try:
        for update in chat_step(user_input, history, current_agent_state, topic_state, progress_state):
            yield update
    except Exception as e:
        error_message = f"❌ **Error**: {str(e)}"
        history.append((None, error_message))
        yield history, "INITIAL", "", {}, gr.update(interactive=True, placeholder="Let's try again. What would you like to research?"), None, gr.update(visible=False)
    finally:
        IS_PROCESSING = False
        print("Processing finished. Lock released.")

def chat_step(user_input, history, current_agent_state, topic_state, progress_state):
    """Enhanced chat step with visual progress tracking"""
    history = history or []
    history.append((user_input, None))
    
    if current_agent_state == "INITIAL":
        yield history, "CLARIFYING", user_input, progress_state, gr.update(interactive=False, placeholder="Analyzing your topic..."), None, gr.update(visible=False)
        
        # Show thinking animation
        thinking_msg = """<div class="thinking-indicator">
            <span>Analyzing your research topic</span>
            <div class="thinking-dots">
                <span></span><span></span><span></span>
            </div>
        </div>"""
        history[-1] = (user_input, thinking_msg)
        yield history, "CLARIFYING", user_input, progress_state, gr.update(interactive=False), None, gr.update(visible=False)
        
        questions = get_clarifying_questions(planner_model, user_input)
        formatted_questions = f"""
### 🎯 Let's refine your research
To create the most comprehensive report on **{user_input}**, I'd like to understand your specific interests:
{questions}
Please provide your answers below to help me tailor the research to your needs.
"""
        history[-1] = (user_input, formatted_questions)
        yield history, "CLARIFYING", user_input, progress_state, gr.update(interactive=True, placeholder="Type your answers here..."), None, gr.update(visible=False)

    elif current_agent_state == "CLARIFYING":
        # Show initial processing message
        history[-1] = (user_input, "πŸ“‹ **Perfect! I have all the information I need.**\n\nStarting deep research process...")
        yield history, "GENERATING", topic_state, {"current_step": 1, "total_steps": 5}, gr.update(interactive=False, placeholder="Generating report..."), None, gr.update(visible=False)
        
        try:
            # Research and planning phase
            plan = research_and_plan(config, planner_model, tavily_client, topic_state, user_input)
            
            # Show research plan - FIXED: Safe access to section titles
            sections_preview = "\n".join([f"  {i+1}. {s.get('title', f'Section {i+1}')}" for i, s in enumerate(plan['sections'])])
            planning_update = f"""
### πŸ“Š Research Plan Created
**Topic**: {plan['detailed_topic']}
**Report Structure**:
{sections_preview}
Now conducting deep research and writing each section...
"""
            history[-1] = (user_input, planning_update)
            yield history, "GENERATING", topic_state, {"current_step": 2, "total_steps": 5}, gr.update(interactive=False), None, gr.update(visible=False)
            
            # Stream report generation
            report_generator = write_report_stream(config, writer_model, tavily_client, embedding_model, reranker, plan)
            
            full_report = ""
            for update in report_generator:
                # Format the update for better display
                if update.startswith("#"):
                    full_report = update
                    # Add progress indicators to the report display
                    display_report = full_report
                else:
                    # Show progress updates
                    progress_msg = format_progress_message(update)
                    display_report = f"{planning_update}\n\n---\n\n**Current Progress**: {progress_msg}\n\n---\n\n### πŸ“„ Report Preview:\n\n{full_report}"
                
                history[-1] = (user_input, display_report)
                yield history, "GENERATING", topic_state, progress_state, gr.update(interactive=False), None, gr.update(visible=False)
            
            # Final report display
            completion_message = f"""
### βœ… Research Complete!
Your comprehensive research report is ready. You can:
- πŸ“₯ Download as PDF using the button below
- πŸ“‹ Copy the text directly from the report
- πŸ”„ Start a new research topic
---
{full_report}
"""
            history[-1] = (user_input, completion_message)
            
            # Enable PDF download
            pdf_path = export_to_pdf(full_report)
            
            yield history, "INITIAL", "", {}, gr.update(interactive=True, placeholder="What would you like to research next?"), pdf_path, gr.update(visible=True)
            
        except Exception as e:
            error_msg = f"❌ **Error during research**: {str(e)}\n\nPlease try again with a different topic or check your API keys."
            history.append((None, error_msg))
            yield history, "INITIAL", "", {}, gr.update(interactive=True, placeholder="Let's try again. What would you like to research?"), None, gr.update(visible=False)

# Build the Gradio interface
with gr.Blocks(css=CSS, theme=gr.themes.Base()) as app:
    # Header
    gr.HTML("""
        <div class="header-container">
            <h1>DeepSearch Research Agent</h1>
            <p class="subtitle">AI-powered comprehensive research and analysis</p>
        </div>
    """)
    
    # Status bar
    gr.HTML("""
        <div class="status-bar">
            <div class="status-indicator">
                <span class="status-dot"></span>
                <span>System Online</span>
            </div>
            <div>
                <span style="color: var(--text-muted);">Powered by Gemini & Tavily</span>
            </div>
        </div>
    """)
    
    # State management
    agent_state = gr.State("INITIAL")
    initial_topic_state = gr.State("")
    progress_state = gr.State({})
    
    # Chat interface
    chatbot = gr.Chatbot(
        elem_id="chatbot",
        bubble_full_width=False,
        height=600,
        visible=True,
        value=[(None, "πŸ‘‹ **Welcome to DeepSearch!**\n\nI'm your AI research assistant. I can help you create comprehensive, well-researched reports on any topic.\n\n**How it works:**\n1. Tell me what you'd like to research\n2. I'll ask a few clarifying questions\n3. I'll conduct deep research and write a detailed report\n4. You'll get a downloadable PDF with all sources\n\n**What would you like to research today?**")],
        avatar_images=(None, "πŸ”¬")
    )
    
    # Input area
    with gr.Group(elem_classes="input-container"):
        with gr.Row():
            chat_input = gr.Textbox(
                placeholder="Enter your research topic (e.g., 'Impact of AI on healthcare', 'Climate change solutions', 'History of quantum computing')",
                interactive=True,
                visible=True,
                show_label=False,
                scale=8,
                elem_id="chat-input"
            )
            submit_button = gr.Button("πŸš€ Start Research", scale=2, variant="primary")
    
    # Export section
    with gr.Group(elem_classes="export-container", visible=False) as export_group:
        gr.Markdown("### πŸ“₯ Export Options")
        with gr.Row(elem_classes="export-buttons"):
            pdf_download = gr.File(label="Download PDF Report", visible=False)
            
    # Event handlers
    submit_event = submit_button.click(
        fn=chat_step_wrapper,
        inputs=[chat_input, chatbot, agent_state, initial_topic_state, progress_state],
        outputs=[chatbot, agent_state, initial_topic_state, progress_state, chat_input, pdf_download, export_group],
    ).then(
        fn=lambda: gr.update(value=""),
        inputs=None,
        outputs=[chat_input],
        queue=False
    )
    
    chat_input.submit(
        fn=chat_step_wrapper,
        inputs=[chat_input, chatbot, agent_state, initial_topic_state, progress_state],
        outputs=[chatbot, agent_state, initial_topic_state, progress_state, chat_input, pdf_download, export_group],
    ).then(
        fn=lambda: gr.update(value=""),
        inputs=None,
        outputs=[chat_input],
        queue=False
    )

# Launch the app
if __name__ == "__main__":
    app.queue()
    app.launch(debug=True, share=False)