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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
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
"""
LifeAdmin AI - Main Gradio Application
Hackathon Project: Track 2 - MCP in Action
Hugging Face MCP Birthday Hackathon

An autonomous life management agent with real MCP tool servers,
RAG-enabled document search, and voice control.
"""

import gradio as gr
import asyncio
import os
from pathlib import Path
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

from ui.manual_dashboard import create_manual_dashboard
from ui.voice_agent_ui import create_voice_agent_ui
from agent.agent_core import LifeAdminAgent
from utils.llm_utils import setup_llm_fallback

# Initialize directories
Path("data/uploads").mkdir(parents=True, exist_ok=True)
Path("data/outputs").mkdir(parents=True, exist_ok=True)
Path("data/chroma_db").mkdir(parents=True, exist_ok=True)

# Initialize agent
print("πŸ€– Initializing LifeAdmin AI Agent...")
agent = LifeAdminAgent()
print("βœ“ Agent initialized successfully!")

# Custom CSS for beautiful UI
custom_css = """
/* Main container styling */
.gradio-container {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    max-width: 1400px !important;
}

/* Header styling */
.header-container {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 2rem;
    border-radius: 1rem;
    color: white;
    margin-bottom: 2rem;
    box-shadow: 0 10px 30px rgba(102, 126, 234, 0.3);
}

.header-container h1 {
    margin: 0;
    font-size: 2.5rem;
    font-weight: 700;
    text-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.header-container p {
    margin: 0.5rem 0 0 0;
    opacity: 0.95;
}

/* Tool card styling */
.tool-card {
    border: 2px solid #e2e8f0;
    border-radius: 0.75rem;
    padding: 1.25rem;
    transition: all 0.3s ease;
    background: white;
    margin: 0.5rem 0;
}

.tool-card:hover {
    border-color: #667eea;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
    transform: translateY(-2px);
}

/* Thought bubble styling */
.thought-bubble {
    background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%);
    border-left: 4px solid #667eea;
    padding: 1rem;
    margin: 0.75rem 0;
    border-radius: 0.5rem;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

/* Success banner */
.success-banner {
    background: linear-gradient(135deg, #c6f6d5 0%, #9ae6b4 100%);
    color: #22543d;
    padding: 1rem;
    border-radius: 0.5rem;
    border-left: 4px solid #38a169;
    font-weight: 500;
}

/* Error banner */
.error-banner {
    background: linear-gradient(135deg, #fed7d7 0%, #fc8181 100%);
    color: #742a2a;
    padding: 1rem;
    border-radius: 0.5rem;
    border-left: 4px solid #e53e3e;
    font-weight: 500;
}

/* Button styling */
.primary-button {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
    border: none !important;
    color: white !important;
    font-weight: 600 !important;
    padding: 0.75rem 1.5rem !important;
    border-radius: 0.5rem !important;
    transition: all 0.3s ease !important;
}

.primary-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4) !important;
}

/* Tab styling */
.tab-nav button {
    font-weight: 500 !important;
    padding: 0.75rem 1.5rem !important;
}

.tab-nav button.selected {
    border-bottom: 3px solid #667eea !important;
    color: #667eea !important;
}

/* File upload area */
.upload-area {
    border: 2px dashed #cbd5e0;
    border-radius: 0.75rem;
    padding: 2rem;
    text-align: center;
    transition: all 0.3s ease;
}

.upload-area:hover {
    border-color: #667eea;
    background: #f7fafc;
}

/* Status indicator */
.status-indicator {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-right: 0.5rem;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.status-active {
    background: #48bb78;
}

.status-processing {
    background: #ed8936;
}

.status-error {
    background: #f56565;
}

/* Footer styling */
.footer {
    text-align: center;
    margin-top: 3rem;
    padding: 1.5rem;
    border-top: 1px solid #e2e8f0;
    color: #718096;
}

/* Responsive adjustments */
@media (max-width: 768px) {
    .header-container h1 {
        font-size: 1.75rem;
    }
    
    .gradio-container {
        padding: 1rem;
    }
}

/* Custom scrollbar */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
    background: #667eea;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #764ba2;
}
"""

def create_app():
    """Create and configure the Gradio application"""
    
    with gr.Blocks(
        theme=gr.themes.Soft(
            primary_hue="purple",
            secondary_hue="blue",
            neutral_hue="slate",
            font=[gr.themes.GoogleFont("Inter"), "system-ui", "sans-serif"],
        ),
        css=custom_css,
        title="LifeAdmin AI - Your Autonomous Life Management Agent",
        analytics_enabled=False
    ) as app:
        
        # Header
        with gr.Row():
            gr.HTML("""
                <div class="header-container">
                    <h1>πŸ€– LifeAdmin AI</h1>
                    <p style="font-size: 1.3rem; margin-top: 0.5rem;">
                        Your Autonomous Life Management Agent
                    </p>
                    <p style="font-size: 0.95rem; margin-top: 0.75rem; opacity: 0.9;">
                        Powered by 7 Real MCP Tool Servers β€’ Built for Hugging Face Hackathon
                    </p>
                    <div style="margin-top: 1rem; display: flex; gap: 1.5rem; font-size: 0.9rem;">
                        <div style="display: flex; align-items: center; gap: 0.5rem;">
                            <span>⚑</span>
                            <span>Autonomous Execution</span>
                        </div>
                        <div style="display: flex; align-items: center; gap: 0.5rem;">
                            <span>🎀</span>
                            <span>Voice Control</span>
                        </div>
                        <div style="display: flex; align-items: center; gap: 0.5rem;">
                            <span>πŸ”</span>
                            <span>RAG-Powered Search</span>
                        </div>
                        <div style="display: flex; align-items: center; gap: 0.5rem;">
                            <span>πŸ’Ύ</span>
                            <span>Memory System</span>
                        </div>
                    </div>
                </div>
            """)
        
        # Main tabs
        with gr.Tabs() as tabs:
            # Manual Dashboard Mode
            with gr.Tab("πŸ“Š Manual Dashboard", id="manual"):
                gr.Markdown("""
                ### 🎯 Manual Mode
                Upload files and use individual tools with full control over parameters and outputs.
                Perfect for precise document processing and batch operations.
                """)
                create_manual_dashboard(agent)
            
            # Voice Agent Mode
            with gr.Tab("🎀 Voice Agent", id="voice"):
                gr.Markdown("""
                ### πŸ€– Voice Agent Mode
                Speak your commands and let the AI agent autonomously plan and execute tasks.
                The agent will use multiple tools, manage context, and provide voice responses.
                """)
                create_voice_agent_ui(agent)
            
            # About & Documentation
            with gr.Tab("ℹ️ About", id="about"):
                gr.Markdown("""
                # About LifeAdmin AI
                
                ## 🎯 What is LifeAdmin AI?
                
                LifeAdmin AI is an autonomous life management agent that helps you handle everyday administrative tasks through natural language and voice commands. Built for the **Hugging Face MCP Birthday Hackathon (Track 2: MCP in Action)**.
                
                ### Key Capabilities
                
                - πŸ“„ **Document Processing**: OCR, summarization, metadata extraction
                - πŸ“… **Calendar Management**: Automatic event creation from extracted dates
                - βœ‰οΈ **Email Drafting**: Context-aware professional email generation
                - πŸ“‹ **Form Filling**: Auto-fill templates with extracted data
                - πŸ—‚οΈ **File Organization**: Intelligent file classification and management
                - πŸ” **Document Search**: RAG-powered semantic search across all documents
                
                ---
                
                ## πŸ—οΈ Architecture
                
                ### System Components
                
                ```
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚         Gradio 6 UI Layer          β”‚
                β”‚   Manual Dashboard | Voice Agent   β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚       Autonomous Agent Core         β”‚
                β”‚  Planning β†’ Execution β†’ Reflection  β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚        MCP Tool Servers (7)         β”‚
                β”‚  OCR | PDF | Forms | Calendar       β”‚
                β”‚  Email | Files | RAG Search         β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚     Supporting Infrastructure       β”‚
                β”‚  RAG Engine | Memory | LLM Chain    β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                ```
                
                ### MCP Tool Servers
                
                1. **OCR Server** - Extract text from images and scanned documents
                2. **PDF Server** - Summarize and analyze PDF documents
                3. **Form Filler** - Auto-fill DOCX/XLSX templates
                4. **Calendar Server** - Generate ICS calendar events
                5. **Email Server** - Draft context-aware emails
                6. **File Manager** - Organize and classify files
                7. **RAG Server** - Semantic document search
                
                ---
                
                ## πŸš€ How to Use
                
                ### Manual Mode
                
                1. **Upload Documents**: Drag and drop PDFs, images, or documents
                2. **Select Tool**: Choose from available tools (OCR, Summarize, etc.)
                3. **Configure Parameters**: Set language, summary length, tone, etc.
                4. **Execute**: Click the execute button to run the tool
                5. **Download Results**: Save generated files (ICS, emails, forms)
                
                ### Voice Mode
                
                1. **Click Microphone**: Start voice recording
                2. **Speak Command**: Give a natural language instruction
                3. **Agent Plans**: Watch the agent create an execution plan
                4. **Auto-Execute**: Agent runs necessary tools autonomously
                5. **Listen to Response**: Hear the results via text-to-speech
                
                ---
                
                ## πŸ’‘ Example Commands
                
                ### For Voice Agent
                
                - *"Extract all deadlines from my PDFs and create calendar events"*
                - *"Summarize this invoice and draft a dispute email"*
                - *"Organize my receipts folder by date"*
                - *"Find all documents mentioning Project X and summarize them"*
                - *"Fill out this form using data from my uploaded documents"*
                - *"Create a calendar event for tomorrow at 2 PM titled Team Meeting"*
                
                ### For Manual Mode
                
                - Upload an invoice β†’ Use OCR β†’ Extract metadata β†’ Create calendar event
                - Upload multiple PDFs β†’ Summarize each β†’ Draft summary email
                - Upload form template β†’ Provide data β†’ Generate filled form
                
                ---
                
                ## πŸ› οΈ Technical Stack
                
                ### Core Technologies
                
                - **Framework**: Gradio 6 (latest features, metadata traces)
                - **Agent**: Custom autonomous agent with planning & reflection
                - **MCP**: Real JSONRPC-based tool servers
                - **RAG**: ChromaDB + Sentence Transformers
                - **Memory**: SQLite persistent storage
                - **Voice**: ElevenLabs TTS + Groq Whisper STT
                
                ### LLM Providers (Fallback Chain)
                
                1. OpenAI (GPT-4) - Best quality
                2. Groq (Llama-3.3-70B) - Fast, recommended
                3. Hyperbolic (Llama-3.3-70B) - Alternative
                4. Hugging Face (Mixtral-8x7B) - Free fallback
                
                ### Document Processing
                
                - **OCR**: EasyOCR, Tesseract
                - **PDF**: PyPDF2, pdf2image
                - **Forms**: python-docx, openpyxl
                - **Calendar**: icalendar
                
                ---
                
                ## πŸŽ“ Agent Workflow
                
                ### Three-Phase Autonomous Loop
                
                **Phase 1: Planning**
                - Analyze user request
                - Query RAG for relevant context
                - Check memory for user preferences
                - Create step-by-step execution plan
                - Select appropriate MCP tools
                
                **Phase 2: Execution**
                - Execute tasks sequentially
                - Call MCP tools via JSONRPC
                - Handle errors with retries
                - Store intermediate results
                - Update progress in UI
                
                **Phase 3: Reflection**
                - Review execution results
                - Synthesize final answer
                - Generate voice response (if voice mode)
                - Update long-term memory
                - Provide downloadable outputs
                
                ---
                
                ## πŸ† Hackathon Submission
                
                **Event**: Hugging Face MCP Birthday Hackathon  
                **Track**: Track 2 - MCP in Action  
                **Features**:
                - βœ… Real MCP tool servers (not mocked)
                - βœ… Autonomous agent with planning
                - βœ… RAG integration for context
                - βœ… Voice control interface
                - βœ… Production-ready deployment
                - βœ… Comprehensive documentation
                
                ---
                
                ## πŸ“š Resources
                
                - [GitHub Repository](#) - Complete source code
                - [Documentation](#) - Detailed setup guide
                - [Demo Video](#) - 5-minute walkthrough
                - [MCP Specification](https://modelcontextprotocol.io)
                
                ---
                
                ## πŸ” Privacy & Security
                
                - **Local Processing**: All data processed locally by default
                - **No Telemetry**: No usage tracking or data collection
                - **Secure Storage**: Files stored locally in encrypted directories
                - **API Keys**: Stored securely in environment variables
                
                ---
                
                ## πŸ™ Acknowledgments
                
                Built with amazing open-source tools:
                - Gradio by Hugging Face
                - Model Context Protocol by Anthropic
                - ChromaDB for vector search
                - ElevenLabs for voice synthesis
                - Groq for fast inference
                
                Special thanks to the Hugging Face and Anthropic teams for making this hackathon possible!
                
                ---
                
                **Made with ❀️ for the Hugging Face MCP Birthday Hackathon**
                """)
        
        # Footer
        gr.HTML("""
            <div class="footer">
                <p style="font-size: 1.1rem; font-weight: 600; margin-bottom: 0.5rem;">
                    πŸ† Hugging Face MCP Birthday Hackathon Submission
                </p>
                <p style="margin-bottom: 0.25rem;">
                    Track 2: MCP in Action | Built with Gradio 6 & Real MCP Tool Servers
                </p>
                <p style="font-size: 0.9rem; color: #a0aec0;">
                    Open Source β€’ Production Ready β€’ Fully Autonomous
                </p>
            </div>
        """)
    
    return app


if __name__ == "__main__":
    print("=" * 60)
    print("πŸš€ Starting LifeAdmin AI")
    print("=" * 60)
    
    # Setup LLM fallback chain
    print("\nβš™οΈ  Setting up LLM providers...")
    try:
        setup_llm_fallback()
        print("βœ“ LLM providers configured successfully!")
    except Exception as e:
        print(f"⚠️  Warning: {e}")
        print("   Please set at least one API key (GROQ_API_KEY, OPENAI_API_KEY, etc.)")
    
    # Check for voice mode requirements
    print("\n🎀 Checking voice mode requirements...")
    if os.getenv('ELEVENLABS_API_KEY'):
        print("βœ“ ElevenLabs API key found (TTS enabled)")
    else:
        print("⚠️  ElevenLabs API key not found (voice output disabled)")
    
    if os.getenv('GROQ_API_KEY'):
        print("βœ“ Groq API key found (STT enabled)")
    else:
        print("⚠️  Groq API key not found (voice input disabled)")
    
    # Create and launch app
    print("\n🎨 Creating Gradio interface...")
    app = create_app()
    
    print("\nβœ… LifeAdmin AI is ready!")
    print("=" * 60)
    print("\nπŸ“± Access the app at: http://localhost:7860")
    print("\nπŸ’‘ Tips:")
    print("   - Use Manual Mode for precise control")
    print("   - Use Voice Mode for autonomous execution")
    print("   - Upload files to enable document-based commands")
    print("\n" + "=" * 60 + "\n")
    
    # Launch the application
    app.launch(
        server_name="0.0.0.0",
        server_port=int(os.getenv("GRADIO_SERVER_PORT", 7860)),
        share=False,
        show_error=True,
        show_api=False
    )