Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,10 @@ import requests
|
|
| 2 |
import json
|
| 3 |
import random
|
| 4 |
from gradio_client import Client
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
# Load environment variables
|
| 9 |
load_dotenv()
|
|
@@ -74,6 +76,8 @@ class EnglishTutor:
|
|
| 74 |
"interests": None,
|
| 75 |
"goals": None
|
| 76 |
}
|
|
|
|
|
|
|
| 77 |
|
| 78 |
def get_welcome_message(self):
|
| 79 |
"""توليد رسالة ترحيب فريدة"""
|
|
@@ -83,12 +87,11 @@ class EnglishTutor:
|
|
| 83 |
json={
|
| 84 |
"model": "deepseek-chat",
|
| 85 |
"messages": [WELCOME_SYSTEM_PROMPT],
|
| 86 |
-
"temperature": random.uniform(0.9, 1),
|
| 87 |
"response_format": {"type": "json_object"}
|
| 88 |
}
|
| 89 |
)
|
| 90 |
welcome_json = json.loads(response.json()["choices"][0]["message"]["content"])
|
| 91 |
-
self.chat_history = [MAIN_SYSTEM_PROMPT] # بدء المحادثة بالبرومبت الرئيسي
|
| 92 |
return welcome_json["greeting"]
|
| 93 |
|
| 94 |
def get_bot_response(self, user_message):
|
|
@@ -123,7 +126,7 @@ class EnglishTutor:
|
|
| 123 |
if text.startswith('"') and text.endswith('"'):
|
| 124 |
text = text[1:-1]
|
| 125 |
|
| 126 |
-
tts_prompt = text
|
| 127 |
tts_emotion = "Warm, encouraging, and clear with a friendly and supportive tone."
|
| 128 |
|
| 129 |
return TTS_CLIENT.predict(
|
|
@@ -136,33 +139,73 @@ class EnglishTutor:
|
|
| 136 |
api_name="/text_to_speech_app"
|
| 137 |
)
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
print("\n🤖 Sam:", welcome)
|
| 146 |
-
welcome_audio = tutor.text_to_speech(welcome)
|
| 147 |
-
print("Audio response saved to:", welcome_audio)
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
import random
|
| 4 |
from gradio_client import Client
|
| 5 |
+
import gradio as gr
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import os
|
| 8 |
+
import tempfile
|
| 9 |
|
| 10 |
# Load environment variables
|
| 11 |
load_dotenv()
|
|
|
|
| 76 |
"interests": None,
|
| 77 |
"goals": None
|
| 78 |
}
|
| 79 |
+
# Initialize with welcome message
|
| 80 |
+
self.chat_history = [MAIN_SYSTEM_PROMPT]
|
| 81 |
|
| 82 |
def get_welcome_message(self):
|
| 83 |
"""توليد رسالة ترحيب فريدة"""
|
|
|
|
| 87 |
json={
|
| 88 |
"model": "deepseek-chat",
|
| 89 |
"messages": [WELCOME_SYSTEM_PROMPT],
|
| 90 |
+
"temperature": random.uniform(0.9, 1),
|
| 91 |
"response_format": {"type": "json_object"}
|
| 92 |
}
|
| 93 |
)
|
| 94 |
welcome_json = json.loads(response.json()["choices"][0]["message"]["content"])
|
|
|
|
| 95 |
return welcome_json["greeting"]
|
| 96 |
|
| 97 |
def get_bot_response(self, user_message):
|
|
|
|
| 126 |
if text.startswith('"') and text.endswith('"'):
|
| 127 |
text = text[1:-1]
|
| 128 |
|
| 129 |
+
tts_prompt = text
|
| 130 |
tts_emotion = "Warm, encouraging, and clear with a friendly and supportive tone."
|
| 131 |
|
| 132 |
return TTS_CLIENT.predict(
|
|
|
|
| 139 |
api_name="/text_to_speech_app"
|
| 140 |
)
|
| 141 |
|
| 142 |
+
# Create a single instance of EnglishTutor
|
| 143 |
+
tutor = EnglishTutor()
|
| 144 |
+
|
| 145 |
+
def format_response(response_dict):
|
| 146 |
+
"""Format the response dictionary into a nice HTML string"""
|
| 147 |
+
html = f"<div style='font-size: 16px;'>"
|
| 148 |
+
html += f"<p>{response_dict['response']}</p>"
|
| 149 |
|
| 150 |
+
if response_dict['corrections']:
|
| 151 |
+
html += f"<p><b>✍️ Corrections:</b> {response_dict['corrections']}</p>"
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
+
if response_dict['vocabulary']:
|
| 154 |
+
html += f"<p><b>📚 Vocabulary:</b> {response_dict['vocabulary']}</p>"
|
| 155 |
+
|
| 156 |
+
if response_dict['encouragement']:
|
| 157 |
+
html += f"<p><b>🌟 Encouragement:</b> {response_dict['encouragement']}</p>"
|
| 158 |
+
|
| 159 |
+
html += "</div>"
|
| 160 |
+
return html
|
| 161 |
+
|
| 162 |
+
def chat(message, history):
|
| 163 |
+
"""Handle chat interactions"""
|
| 164 |
+
if not message:
|
| 165 |
+
# Generate welcome message for empty input
|
| 166 |
+
welcome = tutor.get_welcome_message()
|
| 167 |
+
audio_path = tutor.text_to_speech(welcome)[0]
|
| 168 |
+
return welcome, audio_path
|
| 169 |
+
|
| 170 |
+
# Get bot response
|
| 171 |
+
response = tutor.get_bot_response(message)
|
| 172 |
+
|
| 173 |
+
# Generate audio for the main response
|
| 174 |
+
audio_path = tutor.text_to_speech(response["response"])[0]
|
| 175 |
+
|
| 176 |
+
# Format the complete response
|
| 177 |
+
formatted_response = format_response(response)
|
| 178 |
+
|
| 179 |
+
return formatted_response, audio_path
|
| 180 |
+
|
| 181 |
+
# Create Gradio interface
|
| 182 |
+
with gr.Blocks(css="footer {display: none}") as demo:
|
| 183 |
+
gr.Markdown("# 🤖 Sam - Your English Tutor")
|
| 184 |
+
gr.Markdown("Welcome to your personalized English learning session! Type your message below to start chatting.")
|
| 185 |
+
|
| 186 |
+
chatbot = gr.Chatbot(
|
| 187 |
+
show_label=False,
|
| 188 |
+
height=400,
|
| 189 |
+
bubble_full_width=False,
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
with gr.Row():
|
| 193 |
+
txt = gr.Textbox(
|
| 194 |
+
show_label=False,
|
| 195 |
+
placeholder="Type your message here...",
|
| 196 |
+
container=False,
|
| 197 |
+
)
|
| 198 |
+
audio_output = gr.Audio(
|
| 199 |
+
label="Sam's Voice",
|
| 200 |
+
show_label=True,
|
| 201 |
+
type="filepath"
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
txt.submit(chat, [txt, chatbot], [chatbot, audio_output])
|
| 205 |
+
txt.submit(lambda: "", [], [txt]) # Clear textbox after submit
|
| 206 |
+
|
| 207 |
+
# Launch the interface
|
| 208 |
+
if __name__ == "__main__":
|
| 209 |
+
# Generate welcome message at startup
|
| 210 |
+
welcome = tutor.get_welcome_message()
|
| 211 |
+
demo.launch()
|