Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,14 +49,12 @@ MAIN_SYSTEM_PROMPT = {
|
|
| 49 |
|
| 50 |
WELCOME_PROMPT = {
|
| 51 |
"role": "system",
|
| 52 |
-
"content": """Create a
|
| 53 |
1. Introduces you as Sam, the English tutor
|
| 54 |
-
2. Asks for the user's name
|
| 55 |
-
3. Asks about their background (country, job/studies)
|
| 56 |
-
4. Inquires about their English learning goals
|
| 57 |
|
| 58 |
Return in JSON format with key 'greeting'.
|
| 59 |
-
Keep it friendly and
|
| 60 |
}
|
| 61 |
|
| 62 |
class EnglishTutor:
|
|
@@ -129,14 +127,17 @@ class EnglishTutor:
|
|
| 129 |
def _format_response(self, response_dict):
|
| 130 |
formatted = response_dict["response"]
|
| 131 |
|
| 132 |
-
if response_dict.get("corrections"):
|
| 133 |
-
formatted += f"\n\nβοΈ Corrections: {response_dict['corrections']}"
|
| 134 |
-
|
| 135 |
-
if response_dict.get("vocabulary"):
|
| 136 |
-
formatted += f"\n\nπ Vocabulary: {response_dict['vocabulary']}"
|
| 137 |
-
|
| 138 |
if response_dict.get("encouragement"):
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
return formatted
|
| 142 |
|
|
@@ -178,32 +179,66 @@ def initialize_chat():
|
|
| 178 |
try:
|
| 179 |
welcome = tutor.get_welcome_message()
|
| 180 |
welcome_audio = text_to_speech(welcome)
|
| 181 |
-
return [(None, welcome)], welcome_audio, ""
|
| 182 |
except Exception as e:
|
| 183 |
print(f"Error initializing chat: {str(e)}")
|
| 184 |
-
return [(None, "Hi! I'm Sam, your English tutor. What's your name?")], None, ""
|
| 185 |
|
| 186 |
-
def process_audio(audio, history, transcript):
|
| 187 |
try:
|
| 188 |
if audio is None:
|
| 189 |
-
return history, None, transcript
|
| 190 |
|
| 191 |
user_message = convert_audio_to_text(audio)
|
| 192 |
if not user_message:
|
| 193 |
-
return history, None, transcript
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
history = history or []
|
| 199 |
-
history.append((user_message,
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
return history, audio_response, new_transcript
|
| 204 |
except Exception as e:
|
| 205 |
print(f"Error in process_audio: {str(e)}")
|
| 206 |
-
return history, None, transcript
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
def clear_chat():
|
| 209 |
global tutor
|
|
@@ -212,55 +247,75 @@ def clear_chat():
|
|
| 212 |
|
| 213 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 214 |
gr.Markdown("# π English Learning Assistant")
|
| 215 |
-
gr.Markdown("
|
| 216 |
|
| 217 |
with gr.Row():
|
| 218 |
-
with gr.Column(scale=
|
| 219 |
chatbot = gr.Chatbot(
|
| 220 |
-
height=
|
| 221 |
bubble_full_width=False,
|
| 222 |
-
show_label=False
|
|
|
|
| 223 |
)
|
| 224 |
|
| 225 |
with gr.Row():
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
)
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
)
|
| 236 |
-
|
| 237 |
-
with gr.Column(scale=1):
|
| 238 |
-
gr.Markdown("### π Conversation Transcript")
|
| 239 |
-
transcript_display = gr.Textbox(
|
| 240 |
-
lines=15,
|
| 241 |
-
max_lines=15,
|
| 242 |
-
show_label=False,
|
| 243 |
-
interactive=False,
|
| 244 |
-
placeholder="Your conversation transcript will appear here..."
|
| 245 |
-
)
|
| 246 |
|
| 247 |
with gr.Row():
|
| 248 |
-
clear_btn = gr.Button("π
|
|
|
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
inputs=[audio_input, chatbot, transcript_display],
|
| 253 |
-
outputs=[chatbot, audio_output, transcript_display]
|
| 254 |
)
|
| 255 |
|
| 256 |
clear_btn.click(
|
| 257 |
clear_chat,
|
| 258 |
-
outputs=[chatbot, audio_output, transcript_display]
|
| 259 |
)
|
| 260 |
|
| 261 |
demo.load(
|
| 262 |
initialize_chat,
|
| 263 |
-
outputs=[chatbot, audio_output, transcript_display]
|
| 264 |
)
|
| 265 |
|
| 266 |
if __name__ == "__main__":
|
|
|
|
| 49 |
|
| 50 |
WELCOME_PROMPT = {
|
| 51 |
"role": "system",
|
| 52 |
+
"content": """Create a brief welcome message that:
|
| 53 |
1. Introduces you as Sam, the English tutor
|
| 54 |
+
2. Asks for the user's name only
|
|
|
|
|
|
|
| 55 |
|
| 56 |
Return in JSON format with key 'greeting'.
|
| 57 |
+
Keep it short, friendly and natural - maximum 2 sentences."""
|
| 58 |
}
|
| 59 |
|
| 60 |
class EnglishTutor:
|
|
|
|
| 127 |
def _format_response(self, response_dict):
|
| 128 |
formatted = response_dict["response"]
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
if response_dict.get("encouragement"):
|
| 131 |
+
encouragement = response_dict["encouragement"]
|
| 132 |
+
# Make encouragement more impressive
|
| 133 |
+
if "doing" in encouragement.lower() and "great" in encouragement.lower():
|
| 134 |
+
formatted += f"\n\nπ {encouragement}"
|
| 135 |
+
elif "wonderful" in encouragement.lower() or "excellent" in encouragement.lower():
|
| 136 |
+
formatted += f"\n\n⨠{encouragement}"
|
| 137 |
+
elif "good" in encouragement.lower() or "well" in encouragement.lower():
|
| 138 |
+
formatted += f"\n\nπ― {encouragement}"
|
| 139 |
+
else:
|
| 140 |
+
formatted += f"\n\nπ« {encouragement}"
|
| 141 |
|
| 142 |
return formatted
|
| 143 |
|
|
|
|
| 179 |
try:
|
| 180 |
welcome = tutor.get_welcome_message()
|
| 181 |
welcome_audio = text_to_speech(welcome)
|
| 182 |
+
return [(None, welcome)], welcome_audio, f"π€ Sam: {welcome}", ""
|
| 183 |
except Exception as e:
|
| 184 |
print(f"Error initializing chat: {str(e)}")
|
| 185 |
+
return [(None, "Hi! I'm Sam, your English tutor. What's your name?")], None, "π€ Sam: Hi! I'm Sam, your English tutor. What's your name?", ""
|
| 186 |
|
| 187 |
+
def process_audio(audio, history, transcript, corrections):
|
| 188 |
try:
|
| 189 |
if audio is None:
|
| 190 |
+
return history, None, transcript, corrections
|
| 191 |
|
| 192 |
user_message = convert_audio_to_text(audio)
|
| 193 |
if not user_message:
|
| 194 |
+
return history, None, transcript, corrections
|
| 195 |
|
| 196 |
+
bot_response_json = tutor.get_bot_response(user_message)
|
| 197 |
+
|
| 198 |
+
# Extract the JSON response for corrections
|
| 199 |
+
try:
|
| 200 |
+
response_dict = json.loads(bot_response_json) if isinstance(bot_response_json, str) else bot_response_json
|
| 201 |
+
except:
|
| 202 |
+
response_dict = {"response": bot_response_json}
|
| 203 |
+
|
| 204 |
+
# Format response for TTS (without corrections and vocabulary)
|
| 205 |
+
tts_response = response_dict.get("response", bot_response_json)
|
| 206 |
+
if response_dict.get("encouragement"):
|
| 207 |
+
tts_response += f" {response_dict['encouragement']}"
|
| 208 |
+
|
| 209 |
+
# Generate audio from clean response
|
| 210 |
+
audio_response = text_to_speech(tts_response)
|
| 211 |
+
|
| 212 |
+
# Format full response for chat display
|
| 213 |
+
formatted_response = tutor._format_response(response_dict) if hasattr(tutor, '_format_response') else bot_response_json
|
| 214 |
|
| 215 |
history = history or []
|
| 216 |
+
history.append((user_message, formatted_response))
|
| 217 |
+
|
| 218 |
+
new_transcript = transcript + f"\n\nπ€ You: {user_message}\nπ€ Sam: {tts_response}"
|
| 219 |
|
| 220 |
+
# Update corrections display
|
| 221 |
+
new_corrections = corrections
|
| 222 |
+
if response_dict.get("corrections") or response_dict.get("vocabulary"):
|
| 223 |
+
correction_text = ""
|
| 224 |
+
if response_dict.get("corrections"):
|
| 225 |
+
correction_text += f"βοΈ Grammar: {response_dict['corrections']}\n\n"
|
| 226 |
+
if response_dict.get("vocabulary"):
|
| 227 |
+
vocab = response_dict['vocabulary']
|
| 228 |
+
if isinstance(vocab, dict):
|
| 229 |
+
vocab_text = "\n".join([f"β’ '{k}' β '{v}'" for k, v in vocab.items()])
|
| 230 |
+
else:
|
| 231 |
+
vocab_text = str(vocab)
|
| 232 |
+
correction_text += f"π Vocabulary:\n{vocab_text}"
|
| 233 |
+
new_corrections = corrections + f"\n\n{correction_text}" if corrections else correction_text
|
| 234 |
|
| 235 |
+
return history, audio_response, new_transcript, new_corrections
|
| 236 |
except Exception as e:
|
| 237 |
print(f"Error in process_audio: {str(e)}")
|
| 238 |
+
return history, None, transcript, corrections
|
| 239 |
+
|
| 240 |
+
def submit_recording(audio, history, transcript, corrections):
|
| 241 |
+
return process_audio(audio, history, transcript, corrections)
|
| 242 |
|
| 243 |
def clear_chat():
|
| 244 |
global tutor
|
|
|
|
| 247 |
|
| 248 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 249 |
gr.Markdown("# π English Learning Assistant")
|
| 250 |
+
gr.Markdown("π€ **Record your voice and click submit** - I'll help you improve your English conversation skills!")
|
| 251 |
|
| 252 |
with gr.Row():
|
| 253 |
+
with gr.Column(scale=3):
|
| 254 |
chatbot = gr.Chatbot(
|
| 255 |
+
height=500,
|
| 256 |
bubble_full_width=False,
|
| 257 |
+
show_label=False,
|
| 258 |
+
avatar_images=("π€", "π€")
|
| 259 |
)
|
| 260 |
|
| 261 |
with gr.Row():
|
| 262 |
+
with gr.Column(scale=2):
|
| 263 |
+
audio_input = gr.Audio(
|
| 264 |
+
label="ποΈ Record your voice",
|
| 265 |
+
type="filepath",
|
| 266 |
+
show_label=True
|
| 267 |
+
)
|
| 268 |
+
with gr.Column(scale=1):
|
| 269 |
+
submit_btn = gr.Button("π€ Submit Recording", variant="primary", size="lg")
|
| 270 |
+
with gr.Column(scale=2):
|
| 271 |
+
audio_output = gr.Audio(
|
| 272 |
+
label="π Sam's response",
|
| 273 |
+
type="filepath",
|
| 274 |
+
show_label=True,
|
| 275 |
+
autoplay=True
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
with gr.Column(scale=2):
|
| 279 |
+
with gr.Box():
|
| 280 |
+
gr.Markdown("### π Live Transcript")
|
| 281 |
+
transcript_display = gr.Textbox(
|
| 282 |
+
lines=10,
|
| 283 |
+
max_lines=10,
|
| 284 |
+
show_label=False,
|
| 285 |
+
interactive=False,
|
| 286 |
+
placeholder="Your conversation will appear here...",
|
| 287 |
+
autoscroll=True
|
| 288 |
)
|
| 289 |
+
|
| 290 |
+
with gr.Box():
|
| 291 |
+
gr.Markdown("### π Learning Corner")
|
| 292 |
+
corrections_display = gr.Textbox(
|
| 293 |
+
lines=8,
|
| 294 |
+
max_lines=8,
|
| 295 |
+
show_label=False,
|
| 296 |
+
interactive=False,
|
| 297 |
+
placeholder="Grammar corrections and vocabulary suggestions will appear here...",
|
| 298 |
+
autoscroll=True
|
| 299 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
with gr.Row():
|
| 302 |
+
clear_btn = gr.Button("π Start New Conversation", variant="secondary", size="lg")
|
| 303 |
+
gr.Markdown("π‘ **Tip**: Check the Learning Corner for personalized feedback on your English!")
|
| 304 |
|
| 305 |
+
submit_btn.click(
|
| 306 |
+
submit_recording,
|
| 307 |
+
inputs=[audio_input, chatbot, transcript_display, corrections_display],
|
| 308 |
+
outputs=[chatbot, audio_output, transcript_display, corrections_display]
|
| 309 |
)
|
| 310 |
|
| 311 |
clear_btn.click(
|
| 312 |
clear_chat,
|
| 313 |
+
outputs=[chatbot, audio_output, transcript_display, corrections_display]
|
| 314 |
)
|
| 315 |
|
| 316 |
demo.load(
|
| 317 |
initialize_chat,
|
| 318 |
+
outputs=[chatbot, audio_output, transcript_display, corrections_display]
|
| 319 |
)
|
| 320 |
|
| 321 |
if __name__ == "__main__":
|