Update spoken_module.py
Browse files- spoken_module.py +68 -80
spoken_module.py
CHANGED
|
@@ -1,17 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import uuid
|
|
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
from gtts import gTTS
|
| 5 |
from datetime import datetime
|
| 6 |
-
from
|
| 7 |
from app_utils import (
|
| 8 |
-
|
| 9 |
-
convert_to_wav, transcribe_audio,
|
| 10 |
parse_scores_from_feedback, generate_progress_summary,
|
| 11 |
build_score_comparison_data, render_score_chart,
|
| 12 |
build_trend_data, render_trend_chart
|
| 13 |
)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def generate_example_response(transcript, language):
|
| 16 |
prompt = f"""Rewrite this speech to make it more polished, fluent, and confident.
|
| 17 |
Keep the meaning and tone the same, but improve clarity and structure.
|
|
@@ -28,53 +58,41 @@ Transcript:
|
|
| 28 |
)
|
| 29 |
return response.choices[0].message.content
|
| 30 |
|
| 31 |
-
|
| 32 |
def spoken_dashboard(nickname):
|
| 33 |
with gr.Column() as spoken_panel:
|
| 34 |
gr.Markdown("""
|
| 35 |
<div id="header" style="text-align: center;">
|
| 36 |
<img src="file/images/chatter_owl.png" width="120">
|
| 37 |
<h2>π¦ Meet <strong>Chatter the Owl</strong></h2>
|
| 38 |
-
<p>Choose
|
| 39 |
</div>
|
| 40 |
""")
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
"
|
| 49 |
-
|
| 50 |
-
"General improvement"
|
| 51 |
-
],
|
| 52 |
-
value="General improvement"
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
focus_checkboxes = gr.CheckboxGroup(
|
| 56 |
-
label="π§ Areas to Focus On",
|
| 57 |
-
choices=["Clarity", "Structure", "Fluency", "Tone", "Content Relevance"],
|
| 58 |
-
value=["Clarity", "Structure", "Fluency", "Tone", "Content Relevance"]
|
| 59 |
-
)
|
| 60 |
|
| 61 |
with gr.Row():
|
| 62 |
audio_input = gr.Audio(type="filepath", label="π Speak or Upload Audio")
|
|
|
|
| 63 |
|
| 64 |
-
transcript_box = gr.Textbox(label="π
|
| 65 |
-
feedback_box = gr.Textbox(label="π‘
|
| 66 |
-
audio_output = gr.Audio(label="π Chatter Speaks", type="filepath")
|
| 67 |
hidden_transcript = gr.Textbox(visible=False)
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
try_again = gr.Button("π Try Again")
|
| 71 |
-
show_example = gr.Button("π― Show
|
| 72 |
-
|
| 73 |
-
example_box = gr.Textbox(label="π£ Suggested Improvement", visible=True, placeholder="Click to generate improved speech...")
|
| 74 |
-
history_table = gr.Dataframe(headers=["π Timestamp", "π Language", "π Transcript (Preview)", "π Feedback (Preview)"])
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
milestone_box = gr.Markdown(visible=False)
|
| 79 |
|
| 80 |
def tutor_feedback(audio_file, language, goal, focus_areas):
|
|
@@ -83,79 +101,49 @@ def spoken_dashboard(nickname):
|
|
| 83 |
|
| 84 |
wav_path = convert_to_wav(audio_file)
|
| 85 |
transcript = transcribe_audio(wav_path)
|
| 86 |
-
|
| 87 |
-
previous_transcript =
|
| 88 |
-
previous_feedback =
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
transcript=transcript,
|
| 93 |
-
language=language,
|
| 94 |
-
goal=goal,
|
| 95 |
-
focus_areas=focus_areas,
|
| 96 |
-
previous_transcript=previous_transcript
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
# Progress summary
|
| 100 |
if previous_feedback:
|
| 101 |
-
|
| 102 |
-
feedback_text += progress_summary
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
milestone_message = ""
|
| 107 |
if session_count in [3, 5, 10]:
|
| 108 |
-
|
| 109 |
-
feedback_text += f"\n\n{
|
| 110 |
|
| 111 |
-
# TTS
|
| 112 |
lang_code = LANG_CODES.get(language, "en")
|
| 113 |
tts = gTTS(feedback_text, lang=lang_code)
|
| 114 |
mp3_path = f"/tmp/{uuid.uuid4()}.mp3"
|
| 115 |
tts.save(mp3_path)
|
| 116 |
|
| 117 |
-
# Save new session
|
| 118 |
save_to_db(nickname, transcript, feedback_text, language)
|
| 119 |
sessions = fetch_user_sessions(nickname)
|
| 120 |
session_table = [[s.timestamp, s.language, s.transcript[:40], s.feedback[:40]] for s in sessions]
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
if
|
| 125 |
-
chart_data = build_score_comparison_data(feedback_text, previous_feedback)
|
| 126 |
-
score_chart = render_score_chart(chart_data)
|
| 127 |
-
|
| 128 |
-
if sessions:
|
| 129 |
-
dates, tone_scores = build_trend_data(sessions, category="Tone")
|
| 130 |
-
if tone_scores:
|
| 131 |
-
trend_chart = render_trend_chart(dates, tone_scores, "Tone")
|
| 132 |
|
| 133 |
-
return (
|
| 134 |
-
transcript, feedback_text, mp3_path,
|
| 135 |
-
transcript, session_table,
|
| 136 |
-
score_chart, trend_chart,
|
| 137 |
-
gr.update(visible=bool(milestone_message), value=milestone_message)
|
| 138 |
-
)
|
| 139 |
|
| 140 |
audio_input.change(
|
| 141 |
fn=tutor_feedback,
|
| 142 |
inputs=[audio_input, language_dropdown, goal_dropdown, focus_checkboxes],
|
| 143 |
-
outputs=[
|
| 144 |
-
transcript_box, feedback_box, audio_output, hidden_transcript,
|
| 145 |
-
history_table, comparison_plot, trend_plot, milestone_box
|
| 146 |
-
],
|
| 147 |
show_progress="minimal"
|
| 148 |
)
|
| 149 |
|
| 150 |
try_again.click(fn=lambda: ("", "", None, "", "", None, None, gr.update(visible=False)),
|
| 151 |
inputs=None,
|
| 152 |
-
outputs=[
|
| 153 |
-
transcript_box, feedback_box, audio_output, hidden_transcript,
|
| 154 |
-
example_box, comparison_plot, trend_plot, milestone_box
|
| 155 |
-
])
|
| 156 |
|
| 157 |
show_example.click(fn=generate_example_response,
|
| 158 |
inputs=[hidden_transcript, language_dropdown],
|
| 159 |
outputs=example_box)
|
| 160 |
|
| 161 |
-
return spoken_panel
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import uuid
|
| 3 |
+
import os
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
from gtts import gTTS
|
| 6 |
from datetime import datetime
|
| 7 |
+
from openai import OpenAI
|
| 8 |
from app_utils import (
|
| 9 |
+
LANG_CODES, save_to_db, fetch_user_sessions,
|
| 10 |
+
convert_to_wav, transcribe_audio,
|
| 11 |
parse_scores_from_feedback, generate_progress_summary,
|
| 12 |
build_score_comparison_data, render_score_chart,
|
| 13 |
build_trend_data, render_trend_chart
|
| 14 |
)
|
| 15 |
|
| 16 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 17 |
+
|
| 18 |
+
def generate_feedback(transcript, language, goal="general improvement", focus_areas=None, previous_transcript=None):
|
| 19 |
+
focus_str = ", ".join(focus_areas) if focus_areas else "Clarity, Structure, Fluency, Content Relevance, and Tone"
|
| 20 |
+
history_section = f"\n\nFor reference, their previous transcript was:\n{previous_transcript}" if previous_transcript else ""
|
| 21 |
+
|
| 22 |
+
prompt = f"""
|
| 23 |
+
You are a supportive communication coach helping a learner whose goal is: **{goal}**.
|
| 24 |
+
Evaluate the user's current speech based on the following areas:
|
| 25 |
+
{focus_str}
|
| 26 |
+
Give a score out of 10 and a short explanation for each area.
|
| 27 |
+
Then provide:
|
| 28 |
+
- A summary of strengths and improvement areas.
|
| 29 |
+
- One motivational line to end with.
|
| 30 |
+
Transcript:
|
| 31 |
+
{transcript}
|
| 32 |
+
{history_section}
|
| 33 |
+
""".strip()
|
| 34 |
+
|
| 35 |
+
response = client.chat.completions.create(
|
| 36 |
+
model="gpt-4",
|
| 37 |
+
messages=[
|
| 38 |
+
{"role": "system", "content": f"You are a warm and constructive communication coach responding in {language}."},
|
| 39 |
+
{"role": "user", "content": prompt}
|
| 40 |
+
],
|
| 41 |
+
temperature=0.7
|
| 42 |
+
)
|
| 43 |
+
return response.choices[0].message.content
|
| 44 |
+
|
| 45 |
def generate_example_response(transcript, language):
|
| 46 |
prompt = f"""Rewrite this speech to make it more polished, fluent, and confident.
|
| 47 |
Keep the meaning and tone the same, but improve clarity and structure.
|
|
|
|
| 58 |
)
|
| 59 |
return response.choices[0].message.content
|
| 60 |
|
|
|
|
| 61 |
def spoken_dashboard(nickname):
|
| 62 |
with gr.Column() as spoken_panel:
|
| 63 |
gr.Markdown("""
|
| 64 |
<div id="header" style="text-align: center;">
|
| 65 |
<img src="file/images/chatter_owl.png" width="120">
|
| 66 |
<h2>π¦ Meet <strong>Chatter the Owl</strong></h2>
|
| 67 |
+
<p>Choose your goal, speak, and get better every time.</p>
|
| 68 |
</div>
|
| 69 |
""")
|
| 70 |
|
| 71 |
+
with gr.Row():
|
| 72 |
+
language_dropdown = gr.Dropdown(label="π Language", choices=list(LANG_CODES.keys()), value="English")
|
| 73 |
+
goal_dropdown = gr.Dropdown(label="π― Goal", choices=[
|
| 74 |
+
"Interview preparation", "Public speaking", "Class presentation", "General improvement"
|
| 75 |
+
], value="General improvement")
|
| 76 |
+
focus_checkboxes = gr.CheckboxGroup(label="π§ Focus Areas", choices=[
|
| 77 |
+
"Clarity", "Structure", "Fluency", "Tone", "Content Relevance"
|
| 78 |
+
], value=["Clarity", "Structure", "Fluency", "Tone", "Content Relevance"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
with gr.Row():
|
| 81 |
audio_input = gr.Audio(type="filepath", label="π Speak or Upload Audio")
|
| 82 |
+
audio_output = gr.Audio(label="π Feedback Audio", type="filepath")
|
| 83 |
|
| 84 |
+
transcript_box = gr.Textbox(label="π Transcript", interactive=False)
|
| 85 |
+
feedback_box = gr.Textbox(label="π‘ Feedback", interactive=False)
|
|
|
|
| 86 |
hidden_transcript = gr.Textbox(visible=False)
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
try_again = gr.Button("π Try Again")
|
| 90 |
+
show_example = gr.Button("π― Show Example")
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
example_box = gr.Textbox(label="π£ Suggested Improvement", visible=True)
|
| 93 |
+
history_table = gr.Dataframe(headers=["π Timestamp", "π Language", "π Transcript", "π Feedback"])
|
| 94 |
+
comparison_plot = gr.Plot(label="π Score Comparison")
|
| 95 |
+
trend_plot = gr.Plot(label="π Tone Trend")
|
| 96 |
milestone_box = gr.Markdown(visible=False)
|
| 97 |
|
| 98 |
def tutor_feedback(audio_file, language, goal, focus_areas):
|
|
|
|
| 101 |
|
| 102 |
wav_path = convert_to_wav(audio_file)
|
| 103 |
transcript = transcribe_audio(wav_path)
|
| 104 |
+
sessions = fetch_user_sessions(nickname)
|
| 105 |
+
previous_transcript = sessions[-1].transcript if sessions else None
|
| 106 |
+
previous_feedback = sessions[-1].feedback if sessions else None
|
| 107 |
+
|
| 108 |
+
feedback_text = generate_feedback(transcript, language, goal, focus_areas, previous_transcript)
|
| 109 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
if previous_feedback:
|
| 111 |
+
feedback_text += generate_progress_summary(feedback_text, previous_feedback)
|
|
|
|
| 112 |
|
| 113 |
+
session_count = len(sessions) + 1
|
| 114 |
+
milestone_msg = ""
|
|
|
|
| 115 |
if session_count in [3, 5, 10]:
|
| 116 |
+
milestone_msg = f"π Youβve completed {session_count} sessions!"
|
| 117 |
+
feedback_text += f"\n\n{milestone_msg}"
|
| 118 |
|
|
|
|
| 119 |
lang_code = LANG_CODES.get(language, "en")
|
| 120 |
tts = gTTS(feedback_text, lang=lang_code)
|
| 121 |
mp3_path = f"/tmp/{uuid.uuid4()}.mp3"
|
| 122 |
tts.save(mp3_path)
|
| 123 |
|
|
|
|
| 124 |
save_to_db(nickname, transcript, feedback_text, language)
|
| 125 |
sessions = fetch_user_sessions(nickname)
|
| 126 |
session_table = [[s.timestamp, s.language, s.transcript[:40], s.feedback[:40]] for s in sessions]
|
| 127 |
|
| 128 |
+
chart_plot = render_score_chart(build_score_comparison_data(feedback_text, previous_feedback)) if previous_feedback else None
|
| 129 |
+
dates, tone_scores = build_trend_data(sessions, category="Tone")
|
| 130 |
+
trend_chart = render_trend_chart(dates, tone_scores, "Tone") if tone_scores else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
+
return transcript, feedback_text, mp3_path, transcript, session_table, chart_plot, trend_chart, gr.update(visible=bool(milestone_msg), value=milestone_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
audio_input.change(
|
| 135 |
fn=tutor_feedback,
|
| 136 |
inputs=[audio_input, language_dropdown, goal_dropdown, focus_checkboxes],
|
| 137 |
+
outputs=[transcript_box, feedback_box, audio_output, hidden_transcript, history_table, comparison_plot, trend_plot, milestone_box],
|
|
|
|
|
|
|
|
|
|
| 138 |
show_progress="minimal"
|
| 139 |
)
|
| 140 |
|
| 141 |
try_again.click(fn=lambda: ("", "", None, "", "", None, None, gr.update(visible=False)),
|
| 142 |
inputs=None,
|
| 143 |
+
outputs=[transcript_box, feedback_box, audio_output, hidden_transcript, example_box, comparison_plot, trend_plot, milestone_box])
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
show_example.click(fn=generate_example_response,
|
| 146 |
inputs=[hidden_transcript, language_dropdown],
|
| 147 |
outputs=example_box)
|
| 148 |
|
| 149 |
+
return spoken_panel
|