ankban commited on
Commit
afbad91
·
verified ·
1 Parent(s): e4d0ea1

Update app_utils.py

Browse files
Files changed (1) hide show
  1. app_utils.py +41 -3
app_utils.py CHANGED
@@ -3,6 +3,8 @@ from datetime import datetime
3
  from typing import Optional
4
  import os
5
  import uuid
 
 
6
  import subprocess
7
  from faster_whisper import WhisperModel
8
  from openai import OpenAI
@@ -71,12 +73,12 @@ def generate_feedback(transcript, language, goal="general improvement", focus_ar
71
  prompt = f"""
72
  You are a supportive communication coach helping a learner whose goal is: **{goal}**.
73
 
74
- Evaluate the user's current speech based on the following areas:
75
  {focus_str}
76
 
77
- Give a score out of 10 and a short explanation for each area.
78
 
79
- Then provide:
80
  - A summary of strengths and improvement areas.
81
  - One motivational line to end with.
82
 
@@ -95,6 +97,42 @@ Transcript:
95
  )
96
  return response.choices[0].message.content
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  # === GPT: Improved Response ===
99
  def generate_example_response(transcript, language):
100
  prompt = f"""Rewrite this speech to make it more polished, fluent, and confident.
 
3
  from typing import Optional
4
  import os
5
  import uuid
6
+ import re
7
+ import json
8
  import subprocess
9
  from faster_whisper import WhisperModel
10
  from openai import OpenAI
 
73
  prompt = f"""
74
  You are a supportive communication coach helping a learner whose goal is: **{goal}**.
75
 
76
+ First, return a JSON object of the scores (0–10) for each of the following categories:
77
  {focus_str}
78
 
79
+ Then, write a detailed but friendly explanation for each.
80
 
81
+ Finally, provide:
82
  - A summary of strengths and improvement areas.
83
  - One motivational line to end with.
84
 
 
97
  )
98
  return response.choices[0].message.content
99
 
100
+
101
+ def parse_scores_from_feedback(feedback_text):
102
+ try:
103
+ # Match first valid-looking JSON block
104
+ json_match = re.search(r"\{.*?\}", feedback_text, re.DOTALL)
105
+ if json_match:
106
+ score_block = json.loads(json_match.group(0))
107
+ return {k.strip(): int(v) for k, v in score_block.items() if str(v).isdigit()}
108
+ except Exception as e:
109
+ print("Score parsing failed:", e)
110
+ return {}
111
+
112
+
113
+ def generate_progress_summary(current_feedback, previous_feedback):
114
+ current_scores = parse_scores_from_feedback(current_feedback)
115
+ previous_scores = parse_scores_from_feedback(previous_feedback)
116
+
117
+ if not current_scores or not previous_scores:
118
+ return "" # can't compare
119
+
120
+ summary_lines = []
121
+ for category in current_scores:
122
+ if category in previous_scores:
123
+ diff = current_scores[category] - previous_scores[category]
124
+ if diff > 0:
125
+ summary_lines.append(f"✅ **{category}** improved by **+{diff}**")
126
+ elif diff < 0:
127
+ summary_lines.append(f"⚠️ **{category}** decreased by **{diff}**")
128
+ else:
129
+ summary_lines.append(f"➖ **{category}** stayed the same")
130
+
131
+ if summary_lines:
132
+ return "\n\n**📈 Progress Tracker**\n" + "\n".join(summary_lines)
133
+ return ""
134
+
135
+
136
  # === GPT: Improved Response ===
137
  def generate_example_response(transcript, language):
138
  prompt = f"""Rewrite this speech to make it more polished, fluent, and confident.