File size: 9,601 Bytes
20d720d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4551066
 
17cf6ab
20d720d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143faee
 
20d720d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0326351
20d720d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401c0a0
6849d58
 
20d720d
401c0a0
20d720d
 
6849d58
20d720d
 
6849d58
 
 
 
 
 
 
20d720d
 
 
 
 
 
 
 
e29472a
 
 
 
 
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
"""
Personal AI Coach with CrewAI and Mistral
Multilingual support with advanced conversational AI
"""

import gradio as gr
import asyncio
import os
import sys
from datetime import datetime
from typing import Dict, List, Tuple, Optional
import numpy as np

sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from crew_config import PersonalCoachCrew
from agents.tools.voice_tools import MultilingualVoiceProcessor
from utils.config import Config
from dotenv import load_dotenv
import certifi
load_dotenv()

class PersonalCoachApp:
    """Main application using CrewAI orchestration"""
    
    def __init__(self):
        print("Initializing Personal Coach AI with CrewAI...")
        self.config = Config()
        
        # Initialize CrewAI
        self.crew = PersonalCoachCrew(self.config)
        
        # Initialize voice processor
        self.voice_processor = MultilingualVoiceProcessor()
        
        # Session management
        self.conversation_history = []
        self.session_data = {
            "start_time": datetime.now(),
            "language": "en",
            "user_profile": {}
        }
        
        print("Personal Coach AI initialized successfully!")
    
    async def process_input(
        self, 
        text_input: str, 
        voice_input: Optional[np.ndarray], 
        language: str,
        history: List
    ) -> Tuple:
        """Process user input through CrewAI"""
        try:
            # Prepare input data
            if voice_input is not None:
                # Process voice input with language detection
                transcribed_text, detected_lang = await self.voice_processor.transcribe(
                    voice_input, 
                    language
                )
                text_input = transcribed_text
                self.session_data["language"] = detected_lang
            else:
                self.session_data["language"] = language
            
            if not text_input:
                return history, None, "", None
            
            # Prepare crew input
            crew_input = {
                "user_message": text_input,
                "language": self.session_data["language"],
                "conversation_history": history[-5:],  # Last 5 exchanges
                "user_profile": self.session_data.get("user_profile", {})
            }
            
            # Execute crew
            print(f"Processing input in {self.session_data['language']}...")
            print("PersonalCoachCrew methods:", dir(PersonalCoachCrew))
            print("Has process?", hasattr(PersonalCoachCrew, "process"))
            result = self.crew.process(inputs=crew_input)
            
            # Extract response
            response_text = result.get("final_response", "I'm here to help. Please tell me more.")
            
            # Generate audio response
            audio_response = await self.voice_processor.synthesize(
                response_text,
                self.session_data["language"],
                voice_type="meditation"
            )
            
            # Update history
            history.append([text_input, response_text])
            
            # Update user profile
            if "user_profile_update" in result:
                self.session_data["user_profile"].update(result["user_profile_update"])
            
            return history, audio_response, "", None
            
        except Exception as e:
            print(f"Error in process_input: {str(e)}")
            error_message = f"I apologize, but I encountered an error: {str(e)}"
            history.append(["Error", error_message])
            return history, None, "", None
    
    def clear_conversation(self):
        """Clear conversation and reset session"""
        self.conversation_history = []
        self.session_data["user_profile"] = {}
        return [], None

def create_interface():
    """Create Gradio interface with multilingual support"""
    app = PersonalCoachApp()
    
    with gr.Blocks(theme=gr.themes.Soft(), title="Personal AI Coach") as interface:
        gr.Markdown("""
        # 🧘 Personal AI Coach - Multilingual CrewAI System
        
        Powered by Mistral AI and CrewAI's multi-agent framework. Supports multiple languages!
        
        **Features:**
        - 🌍 Multilingual voice and text support
        - 🤖 4 specialized AI agents working together
        - 🧠 Advanced Mistral AI for deep understanding
        - 📚 Wisdom from 13 spiritual and self-help texts
        - 🎙️ Natural voice interactions in your language
        """)
        
        with gr.Row():
            # Main chat interface
            with gr.Column(scale=3):
                chatbot = gr.Chatbot(
                    height=500,
                    bubble_full_width=False
                    #avatar_images=(None, "🧘")
                )
                
                with gr.Row():
                    language = gr.Dropdown(
                        choices=[
                            ("English", "en"),
                            ("Spanish", "es"),
                            ("French", "fr"),
                            ("German", "de"),
                            ("Italian", "it"),
                            ("Portuguese", "pt"),
                            ("Hindi", "hi"),
                            ("Chinese", "zh"),
                            ("Japanese", "ja"),
                            ("Korean", "ko"),
                            ("Arabic", "ar"),
                            ("Russian", "ru")
                        ],
                        value="en",
                        label="Language",
                        scale=1
                    )
                    
                    text_input = gr.Textbox(
                        placeholder="Type your message or click the microphone...",
                        show_label=False,
                        scale=3
                    )
                    
                    voice_input = gr.Audio(
                        type="numpy",
                        label="Speak here",
                        scale=1
                    )
                
                with gr.Row():
                    send_btn = gr.Button("Send 📤", variant="primary")
                    clear_btn = gr.Button("Clear 🗑️")
                
                audio_output = gr.Audio(
                    label="🔊 Coach Response",
                    autoplay=True
                )
            
            # Sidebar
            with gr.Column(scale=1):
                gr.Markdown("""
                ### 🤖 CrewAI Agent Team
                
                **Agent 1: Empathetic Listener**
                - Multilingual voice processing
                - Emotional understanding
                - Context analysis
                
                **Agent 2: Wisdom Keeper**
                - RAG with Mistral AI
                - Spiritual text knowledge
                - Personalized guidance
                
                **Agent 3: Guardian**
                - Response validation
                - Safety checks
                - Tone refinement
                
                **Agent 4: Conversation Guide**
                - Natural dialogue flow
                - Voice synthesis
                - Feedback integration
                
                ### 🌍 Supported Languages
                Voice input/output in 12+ languages
                
                ### 📚 Knowledge Sources
                - Bhagavad Gita
                - Power of Now
                - Atomic Habits
                - Meditations
                - And 9 more texts...
                """)
        
        # Examples in multiple languages
        with gr.Accordion("💡 Example Prompts", open=False):
            gr.Examples(
                examples=[
                    ["I'm feeling overwhelmed with work pressure", "en"],
                    ["Je me sens perdu dans ma vie", "fr"],
                    ["Estoy luchando con la ansiedad", "es"],
                    ["Ich möchte bessere Gewohnheiten aufbauen", "de"],
                    ["मुझे अपने जीवन का उद्देश्य नहीं मिल रहा", "hi"],
                    ["我想学习冥想", "zh"]
                ],
                inputs=[text_input, language]
            )
        
        # # Event handlers
        async def handle_submission(text, voice, lang, history):
            return await app.process_input(text, voice, lang, history)
        
        # # Connect events
        for trigger in [text_input.submit, send_btn.click]:
            trigger(
                fn=lambda *args: asyncio.run(handle_submission(*args)),
                inputs=[text_input, voice_input, language, chatbot],
                outputs=[chatbot, audio_output, text_input, voice_input]
            )
    #     for trigger in [text_input.submit, send_btn.click]:
    #         trigger(
    #             fn=app.process_input,    # <-- Use async method directly!
    #             inputs=[text_input, voice_input, language, chatbot],
    #             outputs=[chatbot, audio_output, text_input, voice_input]
    # )
        
        clear_btn.click(
            fn=app.clear_conversation,
            outputs=[chatbot, audio_output]
        )
    
    return interface

if __name__ == "__main__":
    print("Starting Personal Coach AI with CrewAI...")
    interface = create_interface()
    interface.launch()
#app = create_interface()