File size: 22,877 Bytes
26fe3e3
 
 
 
ab78f68
26fe3e3
 
 
ab78f68
 
 
 
 
 
 
 
 
 
 
 
26fe3e3
 
 
 
ab78f68
 
 
26fe3e3
 
 
 
ab78f68
26fe3e3
 
 
 
 
 
 
 
 
 
ab78f68
 
 
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab78f68
 
 
 
 
 
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab78f68
26fe3e3
 
ab78f68
 
 
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab78f68
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab78f68
26fe3e3
 
 
 
ab78f68
 
 
26fe3e3
 
 
 
 
 
 
 
ab78f68
26fe3e3
 
ab78f68
 
 
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab78f68
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab78f68
26fe3e3
ab78f68
26fe3e3
 
 
 
ab78f68
 
 
26fe3e3
 
 
 
 
 
ab78f68
26fe3e3
 
ab78f68
 
 
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab78f68
26fe3e3
 
 
 
 
 
 
 
ab78f68
26fe3e3
 
 
 
 
ab78f68
26fe3e3
ab78f68
 
 
26fe3e3
ab78f68
 
 
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362926e
26fe3e3
362926e
26fe3e3
 
 
362926e
 
26fe3e3
 
 
362926e
26fe3e3
 
 
 
 
362926e
26fe3e3
 
 
362926e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362926e
26fe3e3
362926e
26fe3e3
 
 
 
362926e
 
26fe3e3
 
362926e
26fe3e3
362926e
 
 
 
 
 
 
 
 
 
 
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362926e
26fe3e3
362926e
26fe3e3
 
 
362926e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab78f68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab78f68
26fe3e3
 
 
 
 
ab78f68
26fe3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab78f68
26fe3e3
 
 
 
 
ab78f68
26fe3e3
 
 
 
 
 
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
import gradio as gr
from dotenv import load_dotenv
from research_manager import ResearchManager, ResearchManagerAgent
from agents import Runner, trace, gen_trace_id
import os

load_dotenv(override=True)

# Available models for user selection
AVAILABLE_MODELS = [
    "gpt-4o",
    "gpt-4o-mini", 
    "gpt-4-turbo",
    "gpt-4",
    "gpt-3.5-turbo",
    "o1-preview",
    "o1-mini"
]

async def handle_query_submission(query: str, current_state: dict, api_key: str, model: str):
    """Handle initial query submission - generate clarifying questions with progress"""
    if not query.strip():
        return "Please enter a research query.", gr.update(visible=False), gr.update(visible=False), current_state
    
    if not api_key.strip():
        return "Please provide your OpenAI API key.", gr.update(visible=False), gr.update(visible=False), current_state
    
    try:
        # Show progress
        progress_update = "πŸ”„ **Generating clarifying questions...**\n\nPlease wait while our AI analyzes your query and creates focused questions to improve the research quality."
        
        research_manager = ResearchManager(api_key=api_key, model=model)
        result = await research_manager.run_with_clarification(query)
        
        # Format questions for display
        questions_text = "\n\n".join([f"**{i+1}.** {q}" for i, q in enumerate(result["questions"])])
        display_text = f"**βœ… Clarifying Questions Generated:**\n\n{questions_text}\n\n**Please answer these questions to help focus the research:**"
        
        # Update state with query and questions
        new_state = {
            "query": query,
            "questions": result["questions"],
            "trace_id": result["trace_id"],
            "api_key": api_key,
            "model": model
        }
        
        return display_text, gr.update(visible=True), gr.update(visible=True), new_state
        
    except Exception as e:
        return f"❌ Error generating clarifying questions: {str(e)}", gr.update(visible=False), gr.update(visible=False), current_state

async def handle_research_with_answers(answers: str, current_state: dict, email_address: str, send_email: bool):
    """Handle research execution with clarification answers with progress updates"""
    if not current_state.get("query"):
        return "Please start by entering a research query first.", current_state
    
    if not answers.strip():
        return "Please provide answers to the clarifying questions.", current_state
    
    api_key = current_state.get("api_key", "")
    model = current_state.get("model", "gpt-4o-mini")
    
    if not api_key:
        return "API key missing. Please restart with your API key.", current_state
    
    try:
        # Show progress
        progress_message = f"""πŸ”„ **Research in Progress...**

**Original Query:** {current_state['query']}

**Status:** Processing your clarifications and starting comprehensive research...

⏳ This may take 1-2 minutes. We're:
1. Planning search strategy
2. Conducting multiple web searches  
3. Writing initial report
4. Evaluating quality
5. Optimizing if needed
6. Preparing final delivery"""
        
        # Use the enhanced manager with email settings
        from research_manager import create_custom_research_agent
        
        # Parse answers (one per line)
        answer_list = [line.strip() for line in answers.split('\n') if line.strip()]
        
        # Format the query with clarifications
        clarified_query = f"""Original query: {current_state['query']}

Clarifications provided:
{chr(10).join([f"{i+1}. {answer}" for i, answer in enumerate(answer_list)])}

Please use these clarifications to focus and refine the research approach."""
        
        # Create custom agent with email settings and API configuration
        custom_agent = create_custom_research_agent(
            email_address=email_address if send_email else None,
            send_email=send_email,
            api_key=api_key,
            model=model
        )
        
        # Run research with custom agent
        trace_id = gen_trace_id()
        with trace("Focused Research with Clarifications", trace_id=trace_id):
            result = await Runner.run(
                custom_agent,
                f"Research Query: {clarified_query}"
            )
        
        email_status = ""
        if send_email and email_address:
            email_status = f"\nπŸ“§ **Email sent to:** {email_address}"
        elif send_email and not email_address:
            email_status = f"\n⚠️ **Email not sent:** No email address provided"
        else:
            email_status = f"\nπŸ“„ **Report generated:** Email sending disabled"
        
        final_report = f"""**βœ… Research Complete!**

**πŸ”— Trace ID:** {trace_id}
**πŸ€– Model Used:** {model}

**Original Query:** {current_state['query']}

**πŸ“Š Enhanced Final Report:**

{result.final_output}

{email_status}

---
*Research completed using enhanced AI system with quality assurance and your clarifications.*"""
        
        return final_report, current_state
        
    except Exception as e:
        return f"❌ Error during research: {str(e)}", current_state

async def run_direct_research(query: str, api_key: str, model: str, email_address: str = "", send_email: bool = False):
    """Run research directly without clarification using the new agent-based system"""
    if not query.strip():
        return "Please enter a research query."
    
    if not api_key.strip():
        return "Please provide your OpenAI API key."
    
    try:
        trace_id = gen_trace_id()
        with trace("Enhanced Research Manager", trace_id=trace_id):
            print(f"πŸ”— Starting enhanced research with trace: {trace_id}")
            
            # Import the function here to avoid circular imports
            from research_manager import create_custom_research_agent
            
            # Create agent with email settings and API configuration
            custom_agent = create_custom_research_agent(
                email_address=email_address if send_email else None,
                send_email=send_email,
                api_key=api_key,
                model=model
            )
            
            # Use the custom agent
            result = await Runner.run(
                custom_agent,
                f"Research Query: {query}"
            )
            
            email_status = ""
            if send_email and email_address:
                email_status = f"\nπŸ“§ **Email sent to:** {email_address}"
            elif send_email and not email_address:
                email_status = f"\n⚠️ **Email not sent:** No email address provided"
            else:
                email_status = f"\nπŸ“„ **Report generated:** Email sending disabled"
            
            return f"""**βœ… Research Complete!**

**πŸ”— Trace ID:** {trace_id}
**πŸ€– Model Used:** {model}
**πŸ‘€ View Detailed Trace:** https://platform.openai.com/traces/trace?trace_id={trace_id}

**πŸ“Š Enhanced Research Report with Quality Assurance:**

{result.final_output}

{email_status}

---
*πŸ€– This research was conducted using our enhanced agent-based system with automatic quality evaluation and optimization. Check the trace link above to see the full workflow including planning, searching, writing, evaluation, and optimization steps.*"""
             
    except Exception as e:
        import traceback
        error_details = traceback.format_exc()
        print(f"Error details: {error_details}")
        return f"❌ Error during research: {str(e)}\n\nPlease check your API key and model selection, or try the Legacy Quick Research option if this persists."

async def run_legacy_research(query: str, api_key: str, model: str, email_address: str, send_email: bool):
    """Run research using the original ResearchManager class with email options"""
    if not query.strip():
        return "Please enter a research query."
    
    if not api_key.strip():
        return "Please provide your OpenAI API key."
    
    try:
        # Use the enhanced system but call it "legacy" for the user
        trace_id = gen_trace_id()
        with trace("Quick Research", trace_id=trace_id):
            from research_manager import create_custom_research_agent
            
            # Create agent with email settings and API configuration
            custom_agent = create_custom_research_agent(
                email_address=email_address if send_email else None,
                send_email=send_email,
                api_key=api_key,
                model=model
            )
            
            result = await Runner.run(
                custom_agent,
                f"Research Query: {query}"
            )
            
            email_status = ""
            if send_email and email_address:
                email_status = f"\nπŸ“§ **Email sent to:** {email_address}"
            elif send_email and not email_address:
                email_status = f"\n⚠️ **Email not sent:** No email address provided"
            else:
                email_status = f"\nπŸ“„ **Report generated:** Email sending disabled"
            
            return f"""**βœ… Quick Research Complete!**

**πŸ”— Trace ID:** {trace_id}
**πŸ€– Model Used:** {model}

**πŸ“Š Research Report:**

{result.final_output}

{email_status}

---
*Research completed using streamlined research system.*"""
        
    except Exception as e:
        import traceback
        error_details = traceback.format_exc()
        print(f"Error details: {error_details}")
        return f"❌ Error during research: {str(e)}\n\nPlease check your API key and model selection."

async def run_enhanced_research_with_progress(query: str, api_key: str, model: str, email_address: str = "", send_email: bool = False):
    """Run enhanced research with progress tracking"""
    return await run_direct_research(query, api_key, model, email_address, send_email)

async def run_clarified_research_with_progress(answers: str, current_state: dict, email_address: str, send_email: bool):
    """Run research with clarification answers and progress tracking"""
    return await handle_research_with_answers(answers, current_state, email_address, send_email)

# Custom CSS for better readability and contrast
custom_css = """
/* Main container improvements */
.gradio-container {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
}

/* Ensure good contrast for all text inputs */
.gradio-container input[type="text"],
.gradio-container textarea {
    background-color: #4b5563 !important;
    border: 2px solid #6b7280 !important;
    border-radius: 8px !important;
    padding: 12px !important;
    font-size: 14px !important;
    color: #f9fafb !important;
    font-weight: 400 !important;
    line-height: 1.5 !important;
    transition: border-color 0.2s ease !important;
}

.gradio-container input[type="text"]:focus,
.gradio-container textarea:focus {
    border-color: #60a5fa !important;
    box-shadow: 0 0 0 3px rgba(96, 165, 250, 0.2) !important;
    outline: none !important;
}

/* Placeholder styling for all inputs */
.gradio-container input[type="text"]::placeholder,
.gradio-container textarea::placeholder {
    color: #9ca3af !important;
    opacity: 0.8 !important;
    font-style: italic !important;
}

/* Simple button styling with good contrast */
.gradio-container button {
    border-radius: 8px !important;
    font-weight: 500 !important;
    font-size: 14px !important;
    padding: 8px 16px !important;
    border: 2px solid transparent !important;
    transition: all 0.2s ease !important;
}

/* Primary buttons */
button[variant="primary"] {
    background-color: #3b82f6 !important;
    color: white !important;
    border-color: #3b82f6 !important;
}

button[variant="primary"]:hover {
    background-color: #2563eb !important;
    border-color: #2563eb !important;
}

/* Secondary buttons */
button[variant="secondary"] {
    background-color: #f8fafc !important;
    color: #374151 !important;
    border-color: #d1d5db !important;
}

button[variant="secondary"]:hover {
    background-color: #f1f5f9 !important;
    border-color: #9ca3af !important;
}

/* Theme-adaptive section styling */
.clarification-section {
    border: 2px solid var(--border-color-primary, #e5e7eb) !important;
    border-radius: 12px !important;
    padding: 20px !important;
    margin: 16px 0 !important;
    background-color: var(--background-fill-secondary, #f8fafc) !important;
    color: var(--body-text-color, #374151) !important;
}

.clarification-section * {
    color: inherit !important;
}

.clarification-section h1, 
.clarification-section h2, 
.clarification-section h3 {
    color: inherit !important;
    font-weight: 600 !important;
}

/* Dark theme specific styles for clarification section */
.gradio-container.dark .clarification-section {
    background-color: #374151 !important;
    border-color: #4b5563 !important;
    color: #ffffff !important;
}

.gradio-container.dark .clarification-section * {
    color: #ffffff !important;
}

.gradio-container.dark .clarification-section h1,
.gradio-container.dark .clarification-section h2,
.gradio-container.dark .clarification-section h3 {
    color: #ffffff !important;
}

/* Clean answer box */
.answer-textbox {
    background-color: #4b5563 !important;
    border: 2px solid #6b7280 !important;
    border-radius: 8px !important;
    padding: 12px !important;
    color: #d1d5db !important;
    line-height: 1.5 !important;
}

.answer-textbox:focus {
    border-color: #60a5fa !important;
    box-shadow: 0 0 0 3px rgba(96, 165, 250, 0.2) !important;
}

/* Target the actual textarea element inside answer-textbox */
.answer-textbox textarea {
    background-color: #4b5563 !important;
    color: #f9fafb !important;
    border: 2px solid #6b7280 !important;
    border-radius: 8px !important;
    padding: 12px !important;
    font-size: 14px !important;
    font-weight: 400 !important;
    line-height: 1.5 !important;
}

.answer-textbox textarea:focus {
    border-color: #60a5fa !important;
    box-shadow: 0 0 0 3px rgba(96, 165, 250, 0.2) !important;
}

/* Make sure placeholder text is visible on dark background */
.answer-textbox textarea::placeholder {
    color: #9ca3af !important;
    opacity: 0.8 !important;
    font-style: italic !important;
}

/* Make all textareas have proper white text */
.gradio-container textarea {
    color: #f9fafb !important;
}

.answer-textbox::placeholder {
    color: #9ca3af !important;
    opacity: 0.9 !important;
}

/* Theme-adaptive results display */
.results-display {
    border: 2px solid var(--border-color-primary, #e5e7eb) !important;
    border-radius: 8px !important;
    padding: 16px !important;
    margin: 12px 0 !important;
    line-height: 1.6 !important;
    background-color: var(--background-fill-secondary, #f8fafc) !important;
    color: var(--body-text-color, #374151) !important;
}

/* Make sure markdown in results display adapts to theme */
.results-display * {
    color: inherit !important;
}

/* Dark theme specific styles */
.gradio-container.dark .results-display {
    background-color: #374151 !important;
    border-color: #4b5563 !important;
    color: #ffffff !important;
}

.gradio-container.dark .results-display * {
    color: #ffffff !important;
}

/* Style links in results display for visibility */
.results-display a {
    color: #60a5fa !important;
    text-decoration: underline !important;
}

.results-display a:hover {
    color: #93c5fd !important;
}

/* Accordion improvements */
.gradio-accordion {
    border: 1px solid #e5e7eb !important;
    border-radius: 8px !important;
    margin: 8px 0 !important;
}

/* Status indicators with good contrast */
.status-success {
    color: #059669 !important;
    font-weight: 500 !important;
}

.status-info {
    color: #0369a1 !important;
    font-weight: 500 !important;
}

.status-warning {
    color: #d97706 !important;
    font-weight: 500 !important;
}

/* Theme-adaptive headers */
h1, h2, h3 {
    color: var(--body-text-color) !important;
    font-weight: 600 !important;
}

/* Fallback for when CSS variables aren't available */
@media (prefers-color-scheme: dark) {
    h1, h2, h3 {
        color: #ffffff !important;
    }
}

@media (prefers-color-scheme: light) {
    h1, h2, h3 {
        color: #1f2937 !important;
    }
}

/* Specific overrides for Gradio themes */
.gradio-container.dark h1,
.gradio-container.dark h2,
.gradio-container.dark h3 {
    color: #ffffff !important;
}

.gradio-container.light h1,
.gradio-container.light h2,
.gradio-container.light h3 {
    color: #1f2937 !important;
}

/* Remove unnecessary gradients and shadows for simplicity */
* {
    box-shadow: none !important;
}

/* Keep only essential shadows for depth */
.gradio-container button,
.gradio-container input,
.gradio-container textarea {
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1) !important;
}

.gradio-container button:hover {
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15) !important;
}
"""

with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), css=custom_css) as ui:
    gr.Markdown("# πŸ” Deep Research Assistant")
    gr.Markdown("**Ask a research question and get comprehensive, AI-powered analysis with quality assurance.**")
    
    # State to track the conversation
    state = gr.State({})
    
    # Main Research Configuration Block
    with gr.Column():
        # API Configuration Section
        gr.Markdown("### πŸ”‘ API Configuration")
        with gr.Row():
            with gr.Column(scale=2):
                api_key_textbox = gr.Textbox(
                    label="OpenAI API Key", 
                    placeholder="sk-...",
                    type="password",
                    lines=1,
                    info="Your OpenAI API key (required to avoid rate limits)"
                )
            with gr.Column(scale=1):
                model_textbox = gr.Dropdown(
                    label="Model Selection",
                    choices=AVAILABLE_MODELS,
                    value="gpt-4o-mini",
                    info="Choose your preferred OpenAI model"
                )
        
        gr.Markdown("### πŸ” Research Query")
        query_textbox = gr.Textbox(
            label="Research Query", 
            placeholder="What would you like to research? (e.g., 'Latest developments in renewable energy')",
            lines=2,
            elem_classes=["main-input"]
        )
        
        # Email Configuration (part of main block)
        with gr.Accordion("πŸ“§ Email Configuration (Optional)", open=False):
            gr.Markdown("**Configure email delivery for your research reports**")
            
            with gr.Row():
                with gr.Column(scale=3):
                    email_textbox = gr.Textbox(
                        label="Email Address", 
                        placeholder="your.email@example.com",
                        lines=1
                    )
                with gr.Column(scale=1):
                    send_email_checkbox = gr.Checkbox(
                        label="Send Email", 
                        value=False,
                        info="Check to receive the report via email"
                    )
            
            gr.Markdown("*This email setting will be used for any research option you choose below.*")
        
        # Start Research Button (below the main configuration)
        submit_button = gr.Button("πŸš€ Start Research", variant="primary", size="lg")
    
    # Output area for questions and results
    output_area = gr.Markdown(
        label="Research Progress", 
        elem_classes=["results-display"],
        value="πŸ‘‹ Enter your research query above and configure email settings if desired, then click Start Research!"
    )
    
    # Clarification answers section (initially hidden)
    with gr.Column(visible=False, elem_classes=["clarification-section"]) as clarification_row:
        gr.Markdown("### πŸ’­ Help us focus your research")
        gr.Markdown("Please answer these questions to get more targeted results:")
        
        answers_textbox = gr.Textbox(
            label="Your Answers", 
            placeholder="Answer each question on a separate line...\n\nExample:\n1. I'm interested in solar and wind technologies\n2. I need technical details and market analysis\n3. This is for a business presentation",
            lines=6,
            elem_classes=["answer-textbox"],
            show_label=True
        )
        
        research_button = gr.Button(
            "πŸ” Run Focused Research", 
            variant="primary", 
            visible=False, 
            size="lg"
        )
    
    # Research options
    with gr.Accordion("πŸ€– Enhanced Research (Recommended)", open=False):
        gr.Markdown("""
        **New AI-powered research system featuring:**
        
        βœ… **Quality Evaluation** - Each report is automatically assessed  
        βœ… **Smart Optimization** - Reports are improved if needed  
        βœ… **Comprehensive Analysis** - Multiple search strategies  
        
        *Delivers higher quality research through AI quality assurance.*
        """)
        enhanced_button = gr.Button("πŸ€– Enhanced Research", variant="primary")
    
    with gr.Accordion("⚑ Quick Research (Legacy)", open=False):
        gr.Markdown("*Faster research using the original system - good for quick queries.*")
        direct_button = gr.Button("⚑ Quick Research", variant="secondary")
    
    # Event handlers
    submit_button.click(
        fn=handle_query_submission,
        inputs=[query_textbox, state, api_key_textbox, model_textbox],
        outputs=[output_area, clarification_row, research_button, state]
    )
    
    query_textbox.submit(
        fn=handle_query_submission,
        inputs=[query_textbox, state, api_key_textbox, model_textbox],
        outputs=[output_area, clarification_row, research_button, state]
    )
    
    research_button.click(
        fn=run_clarified_research_with_progress,
        inputs=[answers_textbox, state, email_textbox, send_email_checkbox],
        outputs=[output_area]
    )
    
    answers_textbox.submit(
        fn=run_clarified_research_with_progress,
        inputs=[answers_textbox, state, email_textbox, send_email_checkbox],
        outputs=[output_area]
    )
    
    enhanced_button.click(
        fn=run_enhanced_research_with_progress,
        inputs=[query_textbox, api_key_textbox, model_textbox, email_textbox, send_email_checkbox],
        outputs=[output_area]
    )
    
    direct_button.click(
        fn=run_legacy_research,
        inputs=[query_textbox, api_key_textbox, model_textbox, email_textbox, send_email_checkbox],
        outputs=[output_area]
    )

if __name__ == "__main__":
    ui.launch(inbrowser=True)