Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,69 +21,6 @@ if not all([API_KEY, HF_TOKEN, TTS_PASSWORD]):
|
|
| 21 |
TTS_CLIENT = Client("KindSynapse/Youssef-Ahmed-Private-Text-To-Speech-Unlimited", hf_token=HF_TOKEN)
|
| 22 |
recognizer = sr.Recognizer()
|
| 23 |
|
| 24 |
-
# 30 Pre-written welcome messages for instant loading
|
| 25 |
-
WELCOME_MESSAGES = [
|
| 26 |
-
"Hi there! I'm Sam, your enthusiastic English tutor ready to embark on this learning journey with you. Could you tell me your name and where you're from? I'm thrilled to help you achieve your language goals!",
|
| 27 |
-
|
| 28 |
-
"Hello! I'm Sam, your dedicated English tutor, and I'm absolutely excited to meet you! What's your name, and which country are you calling home? Let's make your English learning adventure amazing!",
|
| 29 |
-
|
| 30 |
-
"Welcome! I'm Sam, your personal English coach, and I can't wait to help you improve your language skills. Please share your name and where you're from - I'm here to make learning fun and effective!",
|
| 31 |
-
|
| 32 |
-
"Hey there! I'm Sam, your friendly English tutor who's passionate about helping students succeed. What should I call you, and where in the world are you joining me from? Let's start this exciting journey together!",
|
| 33 |
-
|
| 34 |
-
"Greetings! I'm Sam, your English learning companion, and I'm delighted you've chosen to practice with me. Could you introduce yourself with your name and country? I'm ready to tailor our lessons just for you!",
|
| 35 |
-
|
| 36 |
-
"Hi! I'm Sam, your energetic English tutor, and I'm so happy you're here to improve your language skills. What's your name, and where are you from? Let's make every conversation count!",
|
| 37 |
-
|
| 38 |
-
"Hello and welcome! I'm Sam, your supportive English tutor who believes in making learning enjoyable. Please tell me your name and your home country - I'm excited to help you reach your English goals!",
|
| 39 |
-
|
| 40 |
-
"Hey! I'm Sam, your conversational English tutor, and I'm thrilled to be part of your learning journey. What's your name, and which part of the world are you from? Let's chat and learn together!",
|
| 41 |
-
|
| 42 |
-
"Welcome aboard! I'm Sam, your English tutor who's here to make language learning feel natural and fun. Could you share your name and where you're from? I'm ready to help you shine in English!",
|
| 43 |
-
|
| 44 |
-
"Hi there! I'm Sam, your dedicated English instructor, and I'm genuinely excited to work with you. What's your name, and where do you call home? Let's create some amazing learning moments together!",
|
| 45 |
-
|
| 46 |
-
"Hello! I'm Sam, your English tutor with a passion for helping students grow. Please introduce yourself - your name and country would be perfect! I'm here to make your English journey both effective and enjoyable!",
|
| 47 |
-
|
| 48 |
-
"Hey! I'm Sam, your English learning partner, and I'm so glad you've decided to practice with me. What's your name, and where are you from? Let's make every conversation a stepping stone to success!",
|
| 49 |
-
|
| 50 |
-
"Welcome! I'm Sam, your friendly English tutor who loves seeing students progress. Could you tell me your name and which country you're from? I'm excited to customize our learning experience just for you!",
|
| 51 |
-
|
| 52 |
-
"Hi! I'm Sam, your English conversation coach, and I'm absolutely thrilled to meet you today. What should I call you, and where in the world are you? Let's start building your confidence in English!",
|
| 53 |
-
|
| 54 |
-
"Hello there! I'm Sam, your English tutor who's passionate about interactive learning. Please share your name and home country with me - I'm ready to make our conversations both fun and educational!",
|
| 55 |
-
|
| 56 |
-
"Hey! I'm Sam, your English learning guide, and I'm so excited you've chosen to practice with me. What's your name, and where are you joining me from? Let's make every word count in your learning journey!",
|
| 57 |
-
|
| 58 |
-
"Welcome! I'm Sam, your enthusiastic English tutor who believes in the power of conversation. Could you introduce yourself with your name and country? I'm here to help you express yourself confidently in English!",
|
| 59 |
-
|
| 60 |
-
"Hi there! I'm Sam, your English tutor and conversation partner, and I'm delighted to start this journey with you. What's your name, and where do you call home? Let's make learning English an adventure!",
|
| 61 |
-
|
| 62 |
-
"Hello! I'm Sam, your supportive English instructor who's committed to your success. Please tell me your name and which country you're from - I'm excited to help you unlock your English potential!",
|
| 63 |
-
|
| 64 |
-
"Hey! I'm Sam, your English tutor who's here to make language learning feel effortless. What's your name, and where are you from? Let's turn every conversation into a learning opportunity!",
|
| 65 |
-
|
| 66 |
-
"Welcome! I'm Sam, your dedicated English coach, and I'm genuinely thrilled to work with you. Could you share your name and home country? I'm ready to help you communicate with confidence!",
|
| 67 |
-
|
| 68 |
-
"Hi! I'm Sam, your English learning companion who's passionate about student success. What should I call you, and where in the world are you? Let's make your English dreams a reality!",
|
| 69 |
-
|
| 70 |
-
"Hello there! I'm Sam, your friendly English tutor who loves interactive conversations. Please introduce yourself with your name and country - I'm here to make learning both effective and enjoyable!",
|
| 71 |
-
|
| 72 |
-
"Hey! I'm Sam, your English conversation expert, and I'm so happy you're here to improve your skills. What's your name, and where are you from? Let's create some memorable learning moments together!",
|
| 73 |
-
|
| 74 |
-
"Welcome! I'm Sam, your English tutor who believes in personalized learning. Could you tell me your name and which country you're from? I'm excited to tailor our conversations to your needs!",
|
| 75 |
-
|
| 76 |
-
"Hi there! I'm Sam, your English instructor and biggest supporter, and I'm thrilled to meet you. What's your name, and where do you call home? Let's make every practice session count!",
|
| 77 |
-
|
| 78 |
-
"Hello! I'm Sam, your English tutor who's committed to making learning fun and effective. Please share your name and home country with me - I'm ready to help you achieve your language goals!",
|
| 79 |
-
|
| 80 |
-
"Hey! I'm Sam, your English learning partner who's excited to be part of your journey. What should I call you, and where are you joining me from? Let's make English feel natural and enjoyable!",
|
| 81 |
-
|
| 82 |
-
"Welcome! I'm Sam, your enthusiastic English coach who's here to boost your confidence. Could you introduce yourself with your name and country? I'm ready to help you speak English with pride!",
|
| 83 |
-
|
| 84 |
-
"Hi! I'm Sam, your English tutor who's passionate about conversational learning. What's your name, and where in the world are you? Let's start this amazing English adventure together!"
|
| 85 |
-
]
|
| 86 |
-
|
| 87 |
MAIN_SYSTEM_PROMPT = {
|
| 88 |
"role": "system",
|
| 89 |
"content": """You are Sam, an intelligent and proactive English tutor. You drive the conversation and actively engage students. Your responses must be in JSON format with these keys:
|
|
@@ -130,6 +67,22 @@ MAIN_SYSTEM_PROMPT = {
|
|
| 130 |
Response length: Keep responses conversational (2-3 sentences max for response field)."""
|
| 131 |
}
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
class EnglishTutor:
|
| 134 |
def __init__(self):
|
| 135 |
self.chat_history = [MAIN_SYSTEM_PROMPT]
|
|
@@ -143,8 +96,22 @@ class EnglishTutor:
|
|
| 143 |
}
|
| 144 |
|
| 145 |
def get_welcome_message(self):
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
def get_bot_response(self, user_message):
|
| 150 |
try:
|
|
@@ -257,7 +224,6 @@ tutor = EnglishTutor()
|
|
| 257 |
|
| 258 |
def initialize_chat():
|
| 259 |
try:
|
| 260 |
-
# Get instant welcome message (no API call needed)
|
| 261 |
welcome = tutor.get_welcome_message()
|
| 262 |
clean_welcome = tutor.clean_text_for_tts(welcome)
|
| 263 |
welcome_audio = text_to_speech(clean_welcome)
|
|
@@ -265,7 +231,6 @@ def initialize_chat():
|
|
| 265 |
return history, welcome_audio, f"🤖 Sam: {welcome}", ""
|
| 266 |
except Exception as e:
|
| 267 |
print(f"Error initializing chat: {str(e)}")
|
| 268 |
-
# Fallback to a simple message if TTS fails
|
| 269 |
welcome_msg = "Hi! I'm Sam, your English tutor. What's your name and where are you from?"
|
| 270 |
history = [{"role": "assistant", "content": welcome_msg}]
|
| 271 |
return history, None, f"🤖 Sam: {welcome_msg}", ""
|
|
|
|
| 21 |
TTS_CLIENT = Client("KindSynapse/Youssef-Ahmed-Private-Text-To-Speech-Unlimited", hf_token=HF_TOKEN)
|
| 22 |
recognizer = sr.Recognizer()
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
MAIN_SYSTEM_PROMPT = {
|
| 25 |
"role": "system",
|
| 26 |
"content": """You are Sam, an intelligent and proactive English tutor. You drive the conversation and actively engage students. Your responses must be in JSON format with these keys:
|
|
|
|
| 67 |
Response length: Keep responses conversational (2-3 sentences max for response field)."""
|
| 68 |
}
|
| 69 |
|
| 70 |
+
WELCOME_PROMPT = {
|
| 71 |
+
"role": "system",
|
| 72 |
+
"content": """Create a heartfelt welcome message that:
|
| 73 |
+
1. Introduces you as Sam, an enthusiastic and friendly English tutor who’s excited to guide them
|
| 74 |
+
2. Kindly asks for their name and where they’re from in a natural conversational way
|
| 75 |
+
3. Expresses genuine excitement about helping them grow
|
| 76 |
+
|
| 77 |
+
Return the message in JSON format with the key 'greeting'.
|
| 78 |
+
Make it feel personal, warm, and inviting — like a tutor who truly cares. Keep it within 2 sentences.
|
| 79 |
+
|
| 80 |
+
Example:
|
| 81 |
+
{"greeting": "Hi there! I'm Sam, your friendly English tutor — so glad you're here! What's your name and where are you from?"}
|
| 82 |
+
"""
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
class EnglishTutor:
|
| 87 |
def __init__(self):
|
| 88 |
self.chat_history = [MAIN_SYSTEM_PROMPT]
|
|
|
|
| 96 |
}
|
| 97 |
|
| 98 |
def get_welcome_message(self):
|
| 99 |
+
try:
|
| 100 |
+
response = requests.post(
|
| 101 |
+
"https://api.deepseek.com/v1/chat/completions",
|
| 102 |
+
headers={"Authorization": f"Bearer {API_KEY}"},
|
| 103 |
+
json={
|
| 104 |
+
"model": "deepseek-chat",
|
| 105 |
+
"messages": [WELCOME_PROMPT],
|
| 106 |
+
"temperature": random.uniform(0.9, 1.0),
|
| 107 |
+
"response_format": {"type": "json_object"}
|
| 108 |
+
}
|
| 109 |
+
)
|
| 110 |
+
welcome_json = json.loads(response.json()["choices"][0]["message"]["content"])
|
| 111 |
+
return welcome_json["greeting"]
|
| 112 |
+
except Exception as e:
|
| 113 |
+
print(f"Error in welcome message: {str(e)}")
|
| 114 |
+
return "Hi! I'm Sam, your English tutor. What's your name and where are you from?"
|
| 115 |
|
| 116 |
def get_bot_response(self, user_message):
|
| 117 |
try:
|
|
|
|
| 224 |
|
| 225 |
def initialize_chat():
|
| 226 |
try:
|
|
|
|
| 227 |
welcome = tutor.get_welcome_message()
|
| 228 |
clean_welcome = tutor.clean_text_for_tts(welcome)
|
| 229 |
welcome_audio = text_to_speech(clean_welcome)
|
|
|
|
| 231 |
return history, welcome_audio, f"🤖 Sam: {welcome}", ""
|
| 232 |
except Exception as e:
|
| 233 |
print(f"Error initializing chat: {str(e)}")
|
|
|
|
| 234 |
welcome_msg = "Hi! I'm Sam, your English tutor. What's your name and where are you from?"
|
| 235 |
history = [{"role": "assistant", "content": welcome_msg}]
|
| 236 |
return history, None, f"🤖 Sam: {welcome_msg}", ""
|