Update spoken_module.py
Browse files- spoken_module.py +43 -45
spoken_module.py
CHANGED
|
@@ -3,7 +3,6 @@ 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,
|
|
@@ -21,12 +20,16 @@ def generate_feedback(transcript, language, goal="general improvement", focus_ar
|
|
| 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}
|
|
@@ -58,68 +61,71 @@ Transcript:
|
|
| 58 |
)
|
| 59 |
return response.choices[0].message.content
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def spoken_dashboard(nickname_input):
|
| 62 |
with gr.Column() as spoken_panel:
|
| 63 |
gr.Markdown("""
|
| 64 |
<div id="header" style="text-align: center;">
|
|
|
|
| 65 |
<h2>π¦ Meet <strong>Chatter the Owl</strong></h2>
|
| 66 |
-
<p>
|
| 67 |
</div>
|
| 68 |
""")
|
| 69 |
|
| 70 |
-
with gr.Row(
|
| 71 |
language_dropdown = gr.Dropdown(label="π Language", choices=list(LANG_CODES.keys()), value="English")
|
| 72 |
-
goal_dropdown = gr.Dropdown(label="π― Goal", choices=[
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
"Clarity", "Structure", "Fluency", "Tone", "Content Relevance"
|
| 77 |
-
], value=["Clarity", "Structure", "Fluency", "Tone", "Content Relevance"])
|
| 78 |
-
|
| 79 |
-
with gr.Row(elem_classes=["audio-row"]):
|
| 80 |
audio_input = gr.Audio(type="filepath", label="π Speak or Upload Audio")
|
| 81 |
-
audio_output = gr.Audio(label="π
|
| 82 |
|
| 83 |
transcript_box = gr.Textbox(label="π Transcript", interactive=False)
|
| 84 |
feedback_box = gr.Textbox(label="π‘ Feedback", interactive=False)
|
| 85 |
hidden_transcript = gr.Textbox(visible=False)
|
| 86 |
|
| 87 |
-
with gr.Row(
|
| 88 |
try_again = gr.Button("π Try Again")
|
| 89 |
-
show_example = gr.Button("π― Show Example")
|
| 90 |
|
| 91 |
example_box = gr.Textbox(label="π£ Suggested Improvement", visible=True)
|
| 92 |
history_table = gr.Dataframe(headers=["π Timestamp", "π Language", "π Transcript", "π¬ Feedback"])
|
| 93 |
score_chart = gr.Plot(label="π Score Comparison")
|
| 94 |
trend_chart = gr.Plot(label="π Tone Progress")
|
| 95 |
milestone_box = gr.Markdown(visible=False)
|
| 96 |
-
comparison_plot = gr.Plot(label="π Score Comparison")
|
| 97 |
-
trend_plot = gr.Plot(label="π Tone Trend")
|
| 98 |
-
milestone_box = gr.Markdown(visible=False)
|
| 99 |
|
| 100 |
def tutor_feedback(audio_file, language, goal, focus_areas, nickname):
|
| 101 |
if not audio_file:
|
| 102 |
-
return "", "No audio received.", None, "", [],
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
wav_path = convert_to_wav(audio_file)
|
| 105 |
transcript = transcribe_audio(wav_path)
|
| 106 |
-
|
| 107 |
-
previous_transcript =
|
| 108 |
-
previous_feedback =
|
| 109 |
|
| 110 |
feedback_text = generate_feedback(transcript, language, goal, focus_areas, previous_transcript)
|
| 111 |
|
| 112 |
if previous_feedback:
|
| 113 |
feedback_text += generate_progress_summary(feedback_text, previous_feedback)
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
if session_count in [3, 5, 10]:
|
| 118 |
-
|
| 119 |
-
feedback_text += f"\n\n{
|
| 120 |
|
| 121 |
-
|
| 122 |
-
tts = gTTS(feedback_text, lang=lang_code)
|
| 123 |
mp3_path = f"/tmp/{uuid.uuid4()}.mp3"
|
| 124 |
tts.save(mp3_path)
|
| 125 |
|
|
@@ -127,33 +133,25 @@ def spoken_dashboard(nickname_input):
|
|
| 127 |
sessions = fetch_user_sessions(nickname)
|
| 128 |
session_table = [[s.timestamp, s.language, s.transcript[:40], s.feedback[:40]] for s in sessions]
|
| 129 |
|
| 130 |
-
|
| 131 |
-
dates, tone_scores = build_trend_data(sessions,
|
| 132 |
-
|
| 133 |
|
| 134 |
-
return transcript, feedback_text, mp3_path, transcript, session_table,
|
| 135 |
|
| 136 |
audio_input.change(
|
| 137 |
fn=tutor_feedback,
|
| 138 |
-
inputs=[
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
nickname_input # β
This is the actual textbox, not a State object
|
| 142 |
-
],
|
| 143 |
-
outputs=[
|
| 144 |
-
transcript_box, feedback_box, audio_output,
|
| 145 |
-
hidden_transcript, history_table,
|
| 146 |
-
score_chart, trend_chart, milestone_box
|
| 147 |
-
]
|
| 148 |
)
|
| 149 |
|
| 150 |
-
|
| 151 |
-
try_again.click(fn=lambda: ("", "", None, "", "", None, None, gr.update(visible=False)),
|
| 152 |
inputs=None,
|
| 153 |
-
outputs=[transcript_box, feedback_box, audio_output, hidden_transcript, example_box,
|
| 154 |
|
| 155 |
show_example.click(fn=generate_example_response,
|
| 156 |
inputs=[hidden_transcript, language_dropdown],
|
| 157 |
outputs=example_box)
|
| 158 |
|
| 159 |
-
return spoken_panel
|
|
|
|
| 3 |
import os
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
from gtts import gTTS
|
|
|
|
| 6 |
from openai import OpenAI
|
| 7 |
from app_utils import (
|
| 8 |
LANG_CODES, save_to_db, fetch_user_sessions,
|
|
|
|
| 20 |
|
| 21 |
prompt = f"""
|
| 22 |
You are a supportive communication coach helping a learner whose goal is: **{goal}**.
|
| 23 |
+
|
| 24 |
Evaluate the user's current speech based on the following areas:
|
| 25 |
{focus_str}
|
| 26 |
+
|
| 27 |
Give a score out of 10 and a short explanation for each area.
|
| 28 |
+
|
| 29 |
Then provide:
|
| 30 |
- A summary of strengths and improvement areas.
|
| 31 |
- One motivational line to end with.
|
| 32 |
+
|
| 33 |
Transcript:
|
| 34 |
{transcript}
|
| 35 |
{history_section}
|
|
|
|
| 61 |
)
|
| 62 |
return response.choices[0].message.content
|
| 63 |
|
| 64 |
+
def render_empty_chart(title):
|
| 65 |
+
fig, ax = plt.subplots()
|
| 66 |
+
ax.set_title(title)
|
| 67 |
+
ax.text(0.5, 0.5, "No data yet", ha='center', va='center', fontsize=12)
|
| 68 |
+
ax.axis('off')
|
| 69 |
+
return fig
|
| 70 |
+
|
| 71 |
def spoken_dashboard(nickname_input):
|
| 72 |
with gr.Column() as spoken_panel:
|
| 73 |
gr.Markdown("""
|
| 74 |
<div id="header" style="text-align: center;">
|
| 75 |
+
<img src="images/chatternest_logo.png" width="100">
|
| 76 |
<h2>π¦ Meet <strong>Chatter the Owl</strong></h2>
|
| 77 |
+
<p>Speak and improve your communication with personalized feedback and progress tracking.</p>
|
| 78 |
</div>
|
| 79 |
""")
|
| 80 |
|
| 81 |
+
with gr.Row():
|
| 82 |
language_dropdown = gr.Dropdown(label="π Language", choices=list(LANG_CODES.keys()), value="English")
|
| 83 |
+
goal_dropdown = gr.Dropdown(label="π― Goal", choices=["Interview preparation", "Public speaking", "Class presentation", "General improvement"], value="General improvement")
|
| 84 |
+
focus_checkboxes = gr.CheckboxGroup(label="π§ Focus Areas", choices=["Clarity", "Structure", "Fluency", "Tone", "Content Relevance"], value=["Clarity", "Structure", "Fluency", "Tone", "Content Relevance"])
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
audio_input = gr.Audio(type="filepath", label="π Speak or Upload Audio")
|
| 88 |
+
audio_output = gr.Audio(label="π Chatter's Response", type="filepath")
|
| 89 |
|
| 90 |
transcript_box = gr.Textbox(label="π Transcript", interactive=False)
|
| 91 |
feedback_box = gr.Textbox(label="π‘ Feedback", interactive=False)
|
| 92 |
hidden_transcript = gr.Textbox(visible=False)
|
| 93 |
|
| 94 |
+
with gr.Row():
|
| 95 |
try_again = gr.Button("π Try Again")
|
| 96 |
+
show_example = gr.Button("π― Show Me an Example")
|
| 97 |
|
| 98 |
example_box = gr.Textbox(label="π£ Suggested Improvement", visible=True)
|
| 99 |
history_table = gr.Dataframe(headers=["π Timestamp", "π Language", "π Transcript", "π¬ Feedback"])
|
| 100 |
score_chart = gr.Plot(label="π Score Comparison")
|
| 101 |
trend_chart = gr.Plot(label="π Tone Progress")
|
| 102 |
milestone_box = gr.Markdown(visible=False)
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
def tutor_feedback(audio_file, language, goal, focus_areas, nickname):
|
| 105 |
if not audio_file:
|
| 106 |
+
return "", "No audio received.", None, "", [], render_empty_chart("π Score Comparison"), render_empty_chart("π Tone Progress"), gr.update(visible=False)
|
| 107 |
+
|
| 108 |
+
if hasattr(nickname, "value"):
|
| 109 |
+
nickname = nickname.value # Handle gr.Textbox or gr.State
|
| 110 |
|
| 111 |
wav_path = convert_to_wav(audio_file)
|
| 112 |
transcript = transcribe_audio(wav_path)
|
| 113 |
+
previous_sessions = fetch_user_sessions(nickname)
|
| 114 |
+
previous_transcript = previous_sessions[-1].transcript if previous_sessions else None
|
| 115 |
+
previous_feedback = previous_sessions[-1].feedback if previous_sessions else None
|
| 116 |
|
| 117 |
feedback_text = generate_feedback(transcript, language, goal, focus_areas, previous_transcript)
|
| 118 |
|
| 119 |
if previous_feedback:
|
| 120 |
feedback_text += generate_progress_summary(feedback_text, previous_feedback)
|
| 121 |
|
| 122 |
+
milestone = ""
|
| 123 |
+
session_count = len(previous_sessions) + 1
|
| 124 |
if session_count in [3, 5, 10]:
|
| 125 |
+
milestone = f"π Congrats on completing **{session_count} sessions**!"
|
| 126 |
+
feedback_text += f"\n\n{milestone}"
|
| 127 |
|
| 128 |
+
tts = gTTS(feedback_text, lang=LANG_CODES.get(language, "en"))
|
|
|
|
| 129 |
mp3_path = f"/tmp/{uuid.uuid4()}.mp3"
|
| 130 |
tts.save(mp3_path)
|
| 131 |
|
|
|
|
| 133 |
sessions = fetch_user_sessions(nickname)
|
| 134 |
session_table = [[s.timestamp, s.language, s.transcript[:40], s.feedback[:40]] for s in sessions]
|
| 135 |
|
| 136 |
+
score_plot = render_score_chart(build_score_comparison_data(feedback_text, previous_feedback)) if previous_feedback else render_empty_chart("π Score Comparison")
|
| 137 |
+
dates, tone_scores = build_trend_data(sessions, "Tone")
|
| 138 |
+
trend_plot = render_trend_chart(dates, tone_scores, "Tone") if tone_scores else render_empty_chart("π Tone Progress")
|
| 139 |
|
| 140 |
+
return transcript, feedback_text, mp3_path, transcript, session_table, score_plot, trend_plot, gr.update(visible=bool(milestone), value=milestone)
|
| 141 |
|
| 142 |
audio_input.change(
|
| 143 |
fn=tutor_feedback,
|
| 144 |
+
inputs=[audio_input, language_dropdown, goal_dropdown, focus_checkboxes, nickname_input],
|
| 145 |
+
outputs=[transcript_box, feedback_box, audio_output, hidden_transcript, history_table, score_chart, trend_chart, milestone_box],
|
| 146 |
+
show_progress="minimal"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
+
try_again.click(fn=lambda: ("", "", None, "", "", render_empty_chart("π Score Comparison"), render_empty_chart("π Tone Progress"), gr.update(visible=False)),
|
|
|
|
| 150 |
inputs=None,
|
| 151 |
+
outputs=[transcript_box, feedback_box, audio_output, hidden_transcript, example_box, score_chart, trend_chart, milestone_box])
|
| 152 |
|
| 153 |
show_example.click(fn=generate_example_response,
|
| 154 |
inputs=[hidden_transcript, language_dropdown],
|
| 155 |
outputs=example_box)
|
| 156 |
|
| 157 |
+
return spoken_panel
|