Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -25,34 +25,15 @@ print("Whisper model loaded.")
|
|
| 25 |
|
| 26 |
|
| 27 |
# --- 1. DEFINICI脫N DE PROMPTS PARA LA IA ---
|
| 28 |
-
|
| 29 |
-
CONVERSATION_SYSTEM_PROMPT = """
|
| 30 |
-
|
| 31 |
-
A student will speak to you. Your task is to keep a natural, simple conversation going.
|
| 32 |
-
1. Briefly analyze the user's previous response to estimate their CEFR level (A1, A2, B1, etc.).
|
| 33 |
-
2. Formulate a simple, open-ended follow-up question that is appropriate for THAT estimated level.
|
| 34 |
-
3. Your entire response must be a single, short paragraph in natural, conversational English. DO NOT use JSON.
|
| 35 |
-
"""
|
| 36 |
-
|
| 37 |
-
FINAL_EVALUATION_SYSTEM_PROMPT = """
|
| 38 |
-
You are an expert English language examiner providing a final report. Analyze the entire conversation history provided.
|
| 39 |
-
Your task is to return a single, valid JSON object with the following structure. Do not include any text outside this JSON object.
|
| 40 |
-
JSON Output Structure:
|
| 41 |
-
{
|
| 42 |
-
"cefr_level": "string (e.g., A2, B1)",
|
| 43 |
-
"feedback_en": { "strengths": "string", "areas_for_improvement": "string", "word_by_word_feedback": [{"word": "string", "feedback": "string"}] },
|
| 44 |
-
"feedback_es": { "fortalezas": "string", "areas_a_mejorar": "string", "feedback_por_palabra": [{"palabra": "string", "feedback": "string"}] }
|
| 45 |
-
}
|
| 46 |
-
"""
|
| 47 |
-
|
| 48 |
SENTENCE_EVALUATION_SYSTEM_PROMPT = """
|
| 49 |
-
You are an expert English language examiner
|
| 50 |
-
Input You Will Receive: A JSON object with `reference_transcript` and a list of `spoken_words` with timestamps and energy.
|
| 51 |
-
Your entire response MUST be a single, valid JSON object with the following structure. Do not include any text outside this JSON object.
|
| 52 |
JSON Output Structure:
|
| 53 |
{
|
| 54 |
"overall_score_100": integer,
|
| 55 |
-
"cefr_level": "string
|
| 56 |
"holistic_feedback": { "strengths": "string", "areas_for_improvement": "string" },
|
| 57 |
"word_by_word_analysis": [ { "reference_word": "string", "spoken_word": "string", "word_score_100": integer, "correct_ipa": "string", "feedback_en": "string", "feedback_es": "string" } ]
|
| 58 |
}
|
|
@@ -77,84 +58,68 @@ def extract_word_level_features(audio_path):
|
|
| 77 |
print(f"Error during feature extraction: {e}"); return []
|
| 78 |
|
| 79 |
def chat_interaction(audio_input, history_state):
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
sr, y = audio_input; temp_audio_path = "temp_audio_chat.wav"; sf.write(temp_audio_path, y, sr)
|
| 83 |
-
user_text = client.audio.transcriptions.create(model="whisper-1", file=open(temp_audio_path, "rb")).text
|
| 84 |
-
if not history_state: history_state = []
|
| 85 |
-
history_state.append({"role": "user", "content": user_text})
|
| 86 |
-
chat_display = [(history_state[i]['content'], history_state[i+1]['content']) for i in range(0, len(history_state)-1, 2)]
|
| 87 |
-
chat_display.append((user_text, None))
|
| 88 |
-
|
| 89 |
-
if len(history_state) < 9:
|
| 90 |
-
messages_to_send = [{"role": "system", "content": CONVERSATION_SYSTEM_PROMPT}] + history_state
|
| 91 |
-
response = client.chat.completions.create(model="gpt-4o", messages=messages_to_send, temperature=0.7)
|
| 92 |
-
ai_response = response.choices[0].message.content
|
| 93 |
-
history_state.append({"role": "assistant", "content": ai_response})
|
| 94 |
-
chat_display[-1] = (chat_display[-1][0], ai_response)
|
| 95 |
-
return chat_display, history_state, gr.Markdown(visible=False), gr.Markdown(visible=False)
|
| 96 |
-
else:
|
| 97 |
-
print("Generating final evaluation..."); messages_to_send = [{"role": "system", "content": FINAL_EVALUATION_SYSTEM_PROMPT}] + history_state
|
| 98 |
-
response = client.chat.completions.create(model="gpt-4o", response_format={"type": "json_object"}, messages=messages_to_send)
|
| 99 |
-
try:
|
| 100 |
-
result = json.loads(response.choices[0].message.content)
|
| 101 |
-
fb_en = result.get('feedback_en', {}); md_en = f"## Final Report (CEFR Level: {result.get('cefr_level', 'N/A')})\n### Strengths\n{fb_en.get('strengths', '')}\n### Areas for Improvement\n{fb_en.get('areas_for_improvement', '')}\n### Word-by-Word Feedback\n"
|
| 102 |
-
for item in fb_en.get('word_by_word_feedback', []): md_en += f"- **{item['word']}**: {item['feedback']}\n"
|
| 103 |
-
fb_es = result.get('feedback_es', {}); md_es = f"## Reporte Final (Nivel MCERL: {result.get('cefr_level', 'N/A')})\n### Fortalezas\n{fb_es.get('fortalezas', '')}\n### 脕reas a Mejorar\n{fb_es.get('areas_a_mejorar', '')}\n### Retroalimentaci贸n por Palabra\n"
|
| 104 |
-
for item in fb_es.get('feedback_por_palabra', []): md_es += f"- **{item['palabra']}**: {item['feedback']}\n"
|
| 105 |
-
chat_display[-1] = (chat_display[-1][0], "Thank you for the conversation! Here is your final report.")
|
| 106 |
-
return chat_display, history_state, gr.Markdown(value=md_en, visible=True), gr.Markdown(value=md_es, visible=True)
|
| 107 |
-
except (json.JSONDecodeError, KeyError) as e:
|
| 108 |
-
print(f"Error parsing final report: {e}"); return chat_display, history_state, gr.Markdown(value="Error generating report.", visible=True), gr.Markdown(visible=False)
|
| 109 |
|
|
|
|
| 110 |
def run_sentence_evaluation(audio_input, reference_transcript):
|
| 111 |
if not api_key_found: raise gr.Error("OpenAI API key not found.")
|
| 112 |
if audio_input is None or not reference_transcript:
|
| 113 |
return 0, "N/A", "Please provide both an audio file and the reference text.", ""
|
|
|
|
| 114 |
sr, y = audio_input; temp_audio_path = "temp_audio_sentence.wav"; sf.write(temp_audio_path, y, sr)
|
| 115 |
word_features = extract_word_level_features(temp_audio_path)
|
| 116 |
if not word_features:
|
| 117 |
return 0, "N/A", "Could not process the audio.", ""
|
| 118 |
prompt_data = {"reference_transcript": reference_transcript, "spoken_words": word_features}
|
|
|
|
| 119 |
print("Sending detailed data to GPT-4o for sentence analysis...")
|
| 120 |
response = client.chat.completions.create(model="gpt-4o", response_format={"type": "json_object"}, messages=[{"role": "system", "content": SENTENCE_EVALUATION_SYSTEM_PROMPT}, {"role": "user", "content": json.dumps(prompt_data)}])
|
|
|
|
| 121 |
try:
|
| 122 |
result = json.loads(response.choices[0].message.content)
|
| 123 |
holistic_feedback_md = f"### Strengths\n{result['holistic_feedback']['strengths']}\n\n### Areas for Improvement\n{result['holistic_feedback']['areas_for_improvement']}"
|
| 124 |
word_analysis_list = result['word_by_word_analysis']
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
os.makedirs("reference_audio", exist_ok=True)
|
|
|
|
| 127 |
for index, item in enumerate(word_analysis_list):
|
| 128 |
-
word_to_speak = item['reference_word']
|
|
|
|
|
|
|
|
|
|
| 129 |
try:
|
| 130 |
-
tts = gTTS(text=word_to_speak, lang='en'); tts.save(audio_path)
|
|
|
|
|
|
|
| 131 |
except Exception as e:
|
| 132 |
print(f"Error al generar TTS para '{word_to_speak}': {e}"); audio_player = "Error"
|
|
|
|
| 133 |
md_table += (f"| **{item['reference_word']}** | {item['spoken_word']} | {item['word_score_100']} | {item['feedback_en']} | {item['feedback_es']} | {audio_player} |\n")
|
|
|
|
| 134 |
return (result.get("overall_score_100", 0), result.get("cefr_level", "N/A"), holistic_feedback_md, md_table)
|
| 135 |
except (json.JSONDecodeError, KeyError) as e:
|
| 136 |
print(f"Error processing API response: {e}"); error_msg = "The API response was not in the expected format."
|
| 137 |
return 0, "Error", error_msg, ""
|
| 138 |
|
| 139 |
-
|
|
|
|
| 140 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 141 |
gr.Markdown("# 馃嚞馃嚙 AI English Speaking Practice & Assessment")
|
| 142 |
with gr.Tabs():
|
| 143 |
-
#
|
| 144 |
with gr.TabItem("Pr谩ctica Conversacional (Chat AI)"):
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
chatbot = gr.Chatbot(value=[(None, "Hi there! I'm Alex. How are you doing today?")], label="Conversation with your AI Tutor", height=500)
|
| 148 |
-
audio_in_chat = gr.Audio(sources=["microphone"], type="numpy", label="Record your response")
|
| 149 |
-
with gr.Column(scale=1):
|
| 150 |
-
gr.Markdown("### Final Report"); feedback_en_out = gr.Markdown(label="English Feedback", visible=False); feedback_es_out = gr.Markdown(label="Retroalimentaci贸n en Espa帽ol", visible=False)
|
| 151 |
-
history = gr.State([])
|
| 152 |
-
audio_in_chat.stop_recording(fn=chat_interaction, inputs=[audio_in_chat, history], outputs=[chatbot, history, feedback_en_out, feedback_es_out])
|
| 153 |
|
| 154 |
-
#
|
| 155 |
with gr.TabItem("Evaluaci贸n por Frase"):
|
| 156 |
-
TONGUE_TWISTERS = ["Peter Piper picked a peck of pickled peppers.", "She sells seashells by the seashore.", "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
|
| 157 |
-
gr.Markdown("Choose a tongue twister or write your own sentence.
|
| 158 |
tongue_twister_selector = gr.Dropdown(choices=TONGUE_TWISTERS, label="Or Choose a Tongue Twister to Practice")
|
| 159 |
with gr.Row():
|
| 160 |
with gr.Column(scale=1):
|
|
@@ -163,16 +128,20 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 163 |
submit_btn_sentence = gr.Button("Get Assessment", variant="primary")
|
| 164 |
with gr.Column(scale=2):
|
| 165 |
gr.Markdown("### Assessment Summary")
|
| 166 |
-
with gr.Row():
|
| 167 |
score_out_sentence = gr.Number(label="Overall Score (0-100)", interactive=False)
|
| 168 |
level_out_sentence = gr.Textbox(label="Estimated CEFR Level", interactive=False)
|
| 169 |
holistic_feedback_out_sentence = gr.Markdown(label="Examiner's Feedback")
|
|
|
|
| 170 |
gr.Markdown("--- \n ### Detailed Word-by-Word Analysis")
|
|
|
|
|
|
|
| 171 |
word_analysis_out_sentence = gr.Markdown(label="Phonetic Breakdown")
|
|
|
|
| 172 |
def update_text(choice): return gr.Textbox(value=choice)
|
| 173 |
tongue_twister_selector.change(fn=update_text, inputs=tongue_twister_selector, outputs=text_in_sentence)
|
| 174 |
submit_btn_sentence.click(fn=run_sentence_evaluation, inputs=[audio_in_sentence, text_in_sentence], outputs=[score_out_sentence, level_out_sentence, holistic_feedback_out_sentence, word_analysis_out_sentence])
|
| 175 |
|
| 176 |
if __name__ == "__main__":
|
| 177 |
-
if not api_key_found: print("\nFATAL: OpenAI API key not found.
|
| 178 |
else: demo.launch(debug=True)
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
# --- 1. DEFINICI脫N DE PROMPTS PARA LA IA ---
|
| 28 |
+
# (Tus prompts completos van aqu铆...)
|
| 29 |
+
CONVERSATION_SYSTEM_PROMPT = """..."""
|
| 30 |
+
FINAL_EVALUATION_SYSTEM_PROMPT = """..."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
SENTENCE_EVALUATION_SYSTEM_PROMPT = """
|
| 32 |
+
You are an expert English language examiner...
|
|
|
|
|
|
|
| 33 |
JSON Output Structure:
|
| 34 |
{
|
| 35 |
"overall_score_100": integer,
|
| 36 |
+
"cefr_level": "string",
|
| 37 |
"holistic_feedback": { "strengths": "string", "areas_for_improvement": "string" },
|
| 38 |
"word_by_word_analysis": [ { "reference_word": "string", "spoken_word": "string", "word_score_100": integer, "correct_ipa": "string", "feedback_en": "string", "feedback_es": "string" } ]
|
| 39 |
}
|
|
|
|
| 58 |
print(f"Error during feature extraction: {e}"); return []
|
| 59 |
|
| 60 |
def chat_interaction(audio_input, history_state):
|
| 61 |
+
# (Tu funci贸n de chat sin cambios va aqu铆...)
|
| 62 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# --- CAMBIO: La funci贸n de evaluaci贸n de frase ahora devuelve MARKDOWN ---
|
| 65 |
def run_sentence_evaluation(audio_input, reference_transcript):
|
| 66 |
if not api_key_found: raise gr.Error("OpenAI API key not found.")
|
| 67 |
if audio_input is None or not reference_transcript:
|
| 68 |
return 0, "N/A", "Please provide both an audio file and the reference text.", ""
|
| 69 |
+
|
| 70 |
sr, y = audio_input; temp_audio_path = "temp_audio_sentence.wav"; sf.write(temp_audio_path, y, sr)
|
| 71 |
word_features = extract_word_level_features(temp_audio_path)
|
| 72 |
if not word_features:
|
| 73 |
return 0, "N/A", "Could not process the audio.", ""
|
| 74 |
prompt_data = {"reference_transcript": reference_transcript, "spoken_words": word_features}
|
| 75 |
+
|
| 76 |
print("Sending detailed data to GPT-4o for sentence analysis...")
|
| 77 |
response = client.chat.completions.create(model="gpt-4o", response_format={"type": "json_object"}, messages=[{"role": "system", "content": SENTENCE_EVALUATION_SYSTEM_PROMPT}, {"role": "user", "content": json.dumps(prompt_data)}])
|
| 78 |
+
|
| 79 |
try:
|
| 80 |
result = json.loads(response.choices[0].message.content)
|
| 81 |
holistic_feedback_md = f"### Strengths\n{result['holistic_feedback']['strengths']}\n\n### Areas for Improvement\n{result['holistic_feedback']['areas_for_improvement']}"
|
| 82 |
word_analysis_list = result['word_by_word_analysis']
|
| 83 |
+
|
| 84 |
+
# --- NUEVA L脫GICA: Construir una tabla en Markdown ---
|
| 85 |
+
md_table = "| Reference Word | Spoken Word | Score | Feedback (EN) | Feedback (ES) | Reference Audio |\n"
|
| 86 |
+
md_table += "| :--- | :--- | :---: | :--- | :--- | :---: |\n"
|
| 87 |
+
|
| 88 |
os.makedirs("reference_audio", exist_ok=True)
|
| 89 |
+
|
| 90 |
for index, item in enumerate(word_analysis_list):
|
| 91 |
+
word_to_speak = item['reference_word']
|
| 92 |
+
safe_filename = re.sub(r'\W+', '', word_to_speak.lower())
|
| 93 |
+
audio_path = f"reference_audio/{index}_{safe_filename}.mp3"
|
| 94 |
+
|
| 95 |
try:
|
| 96 |
+
tts = gTTS(text=word_to_speak, lang='en'); tts.save(audio_path)
|
| 97 |
+
# Embeber el audio usando una etiqueta HTML <audio>
|
| 98 |
+
audio_player = f'<audio src="file/{audio_path}" controls></audio>'
|
| 99 |
except Exception as e:
|
| 100 |
print(f"Error al generar TTS para '{word_to_speak}': {e}"); audio_player = "Error"
|
| 101 |
+
|
| 102 |
md_table += (f"| **{item['reference_word']}** | {item['spoken_word']} | {item['word_score_100']} | {item['feedback_en']} | {item['feedback_es']} | {audio_player} |\n")
|
| 103 |
+
|
| 104 |
return (result.get("overall_score_100", 0), result.get("cefr_level", "N/A"), holistic_feedback_md, md_table)
|
| 105 |
except (json.JSONDecodeError, KeyError) as e:
|
| 106 |
print(f"Error processing API response: {e}"); error_msg = "The API response was not in the expected format."
|
| 107 |
return 0, "Error", error_msg, ""
|
| 108 |
|
| 109 |
+
|
| 110 |
+
# --- 3. INTERFAZ DE GRADIO CON PESTA脩AS (Con salida Markdown) ---
|
| 111 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 112 |
gr.Markdown("# 馃嚞馃嚙 AI English Speaking Practice & Assessment")
|
| 113 |
with gr.Tabs():
|
| 114 |
+
# PESTA脩A 1: CHAT AI (sin cambios)
|
| 115 |
with gr.TabItem("Pr谩ctica Conversacional (Chat AI)"):
|
| 116 |
+
# ... (Aqu铆 va toda la definici贸n de la interfaz de tu chatbot, sin cambios)
|
| 117 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
# PESTA脩A 2: EVALUACI脫N POR FRASE
|
| 120 |
with gr.TabItem("Evaluaci贸n por Frase"):
|
| 121 |
+
TONGUE_TWISTERS = ["Peter Piper picked a peck of pickled peppers.", "She sells seashells by the seashore.", "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"]
|
| 122 |
+
gr.Markdown("Choose a tongue twister or write your own sentence...")
|
| 123 |
tongue_twister_selector = gr.Dropdown(choices=TONGUE_TWISTERS, label="Or Choose a Tongue Twister to Practice")
|
| 124 |
with gr.Row():
|
| 125 |
with gr.Column(scale=1):
|
|
|
|
| 128 |
submit_btn_sentence = gr.Button("Get Assessment", variant="primary")
|
| 129 |
with gr.Column(scale=2):
|
| 130 |
gr.Markdown("### Assessment Summary")
|
| 131 |
+
with gr.Row():
|
| 132 |
score_out_sentence = gr.Number(label="Overall Score (0-100)", interactive=False)
|
| 133 |
level_out_sentence = gr.Textbox(label="Estimated CEFR Level", interactive=False)
|
| 134 |
holistic_feedback_out_sentence = gr.Markdown(label="Examiner's Feedback")
|
| 135 |
+
|
| 136 |
gr.Markdown("--- \n ### Detailed Word-by-Word Analysis")
|
| 137 |
+
|
| 138 |
+
# --- AJUSTE CLAVE: La salida ahora es un 煤nico componente Markdown ---
|
| 139 |
word_analysis_out_sentence = gr.Markdown(label="Phonetic Breakdown")
|
| 140 |
+
|
| 141 |
def update_text(choice): return gr.Textbox(value=choice)
|
| 142 |
tongue_twister_selector.change(fn=update_text, inputs=tongue_twister_selector, outputs=text_in_sentence)
|
| 143 |
submit_btn_sentence.click(fn=run_sentence_evaluation, inputs=[audio_in_sentence, text_in_sentence], outputs=[score_out_sentence, level_out_sentence, holistic_feedback_out_sentence, word_analysis_out_sentence])
|
| 144 |
|
| 145 |
if __name__ == "__main__":
|
| 146 |
+
if not api_key_found: print("\nFATAL: OpenAI API key not found.")
|
| 147 |
else: demo.launch(debug=True)
|