Add session history locally
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from gtts import gTTS
|
|
| 7 |
from faster_whisper import WhisperModel
|
| 8 |
import subprocess
|
| 9 |
import shutil
|
|
|
|
| 10 |
|
| 11 |
# === Clean /tmp at startup ===
|
| 12 |
TMP_DIR = "/tmp"
|
|
@@ -24,8 +25,6 @@ for sub in os.listdir(TMP_DIR):
|
|
| 24 |
os.environ["HF_HOME"] = "/tmp/hf"
|
| 25 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf"
|
| 26 |
os.environ["XDG_CACHE_HOME"] = "/tmp/hf"
|
| 27 |
-
|
| 28 |
-
# Use writable cache for Hugging Face
|
| 29 |
os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
|
| 30 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 31 |
|
|
@@ -47,6 +46,9 @@ LANG_CODES = {
|
|
| 47 |
model = WhisperModel("base", compute_type="int8")
|
| 48 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
# === Audio Processing ===
|
| 51 |
def convert_to_wav(input_file):
|
| 52 |
output_wav = f"/tmp/{uuid.uuid4()}.wav"
|
|
@@ -62,22 +64,18 @@ def transcribe_audio(audio_path):
|
|
| 62 |
def generate_feedback(transcript, language):
|
| 63 |
prompt = f"""
|
| 64 |
You are a communication coach. Please respond in [language={language}].
|
| 65 |
-
|
| 66 |
Evaluate the user's speech on:
|
| 67 |
1. Clarity
|
| 68 |
2. Structure
|
| 69 |
3. Fluency
|
| 70 |
4. Content Relevance
|
| 71 |
5. Tone & Expression
|
| 72 |
-
|
| 73 |
Each category:
|
| 74 |
- Score out of 10
|
| 75 |
- Short explanation
|
| 76 |
-
|
| 77 |
End with:
|
| 78 |
- Overall feedback summary
|
| 79 |
- One motivational line
|
| 80 |
-
|
| 81 |
Transcript:
|
| 82 |
{transcript}
|
| 83 |
"""
|
|
@@ -95,10 +93,8 @@ Transcript:
|
|
| 95 |
def generate_example_response(transcript, language):
|
| 96 |
prompt = f"""
|
| 97 |
You are a communication coach.
|
| 98 |
-
|
| 99 |
Rewrite this speech to make it more polished, fluent, and confident.
|
| 100 |
Keep the meaning and tone the same, but improve clarity and structure.
|
| 101 |
-
|
| 102 |
Transcript:
|
| 103 |
{transcript}
|
| 104 |
"""
|
|
@@ -111,7 +107,6 @@ Transcript:
|
|
| 111 |
)
|
| 112 |
return response.choices[0].message.content
|
| 113 |
|
| 114 |
-
|
| 115 |
# === Main Feedback Function ===
|
| 116 |
def tutor_feedback(audio_file, language):
|
| 117 |
if not audio_file or not os.path.exists(audio_file):
|
|
@@ -126,6 +121,14 @@ def tutor_feedback(audio_file, language):
|
|
| 126 |
mp3_path = f"/tmp/{uuid.uuid4()}.mp3"
|
| 127 |
tts.save(mp3_path)
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
return transcript, feedback_text, mp3_path, transcript
|
| 130 |
|
| 131 |
# === Gradio Interface ===
|
|
@@ -160,6 +163,14 @@ with gr.Blocks(css="light_mode_chatter_owl.css") as app:
|
|
| 160 |
|
| 161 |
example_box = gr.Textbox(label="π£ Suggested Improvement", visible=True, placeholder="Click to generate improved speech...")
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
# Connect functions
|
| 164 |
audio_input.change(
|
| 165 |
fn=tutor_feedback,
|
|
@@ -179,6 +190,12 @@ with gr.Blocks(css="light_mode_chatter_owl.css") as app:
|
|
| 179 |
outputs=example_box
|
| 180 |
)
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
# === Launch App ===
|
| 183 |
if __name__ == "__main__":
|
| 184 |
print("β
App is launching...")
|
|
|
|
| 7 |
from faster_whisper import WhisperModel
|
| 8 |
import subprocess
|
| 9 |
import shutil
|
| 10 |
+
import datetime
|
| 11 |
|
| 12 |
# === Clean /tmp at startup ===
|
| 13 |
TMP_DIR = "/tmp"
|
|
|
|
| 25 |
os.environ["HF_HOME"] = "/tmp/hf"
|
| 26 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf"
|
| 27 |
os.environ["XDG_CACHE_HOME"] = "/tmp/hf"
|
|
|
|
|
|
|
| 28 |
os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
|
| 29 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 30 |
|
|
|
|
| 46 |
model = WhisperModel("base", compute_type="int8")
|
| 47 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 48 |
|
| 49 |
+
# === In-memory session history ===
|
| 50 |
+
session_history = []
|
| 51 |
+
|
| 52 |
# === Audio Processing ===
|
| 53 |
def convert_to_wav(input_file):
|
| 54 |
output_wav = f"/tmp/{uuid.uuid4()}.wav"
|
|
|
|
| 64 |
def generate_feedback(transcript, language):
|
| 65 |
prompt = f"""
|
| 66 |
You are a communication coach. Please respond in [language={language}].
|
|
|
|
| 67 |
Evaluate the user's speech on:
|
| 68 |
1. Clarity
|
| 69 |
2. Structure
|
| 70 |
3. Fluency
|
| 71 |
4. Content Relevance
|
| 72 |
5. Tone & Expression
|
|
|
|
| 73 |
Each category:
|
| 74 |
- Score out of 10
|
| 75 |
- Short explanation
|
|
|
|
| 76 |
End with:
|
| 77 |
- Overall feedback summary
|
| 78 |
- One motivational line
|
|
|
|
| 79 |
Transcript:
|
| 80 |
{transcript}
|
| 81 |
"""
|
|
|
|
| 93 |
def generate_example_response(transcript, language):
|
| 94 |
prompt = f"""
|
| 95 |
You are a communication coach.
|
|
|
|
| 96 |
Rewrite this speech to make it more polished, fluent, and confident.
|
| 97 |
Keep the meaning and tone the same, but improve clarity and structure.
|
|
|
|
| 98 |
Transcript:
|
| 99 |
{transcript}
|
| 100 |
"""
|
|
|
|
| 107 |
)
|
| 108 |
return response.choices[0].message.content
|
| 109 |
|
|
|
|
| 110 |
# === Main Feedback Function ===
|
| 111 |
def tutor_feedback(audio_file, language):
|
| 112 |
if not audio_file or not os.path.exists(audio_file):
|
|
|
|
| 121 |
mp3_path = f"/tmp/{uuid.uuid4()}.mp3"
|
| 122 |
tts.save(mp3_path)
|
| 123 |
|
| 124 |
+
# Store session history
|
| 125 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
|
| 126 |
+
session_history.append({
|
| 127 |
+
"timestamp": timestamp,
|
| 128 |
+
"transcript": transcript,
|
| 129 |
+
"feedback": feedback_text
|
| 130 |
+
})
|
| 131 |
+
|
| 132 |
return transcript, feedback_text, mp3_path, transcript
|
| 133 |
|
| 134 |
# === Gradio Interface ===
|
|
|
|
| 163 |
|
| 164 |
example_box = gr.Textbox(label="π£ Suggested Improvement", visible=True, placeholder="Click to generate improved speech...")
|
| 165 |
|
| 166 |
+
# Session History Display
|
| 167 |
+
def get_session_table():
|
| 168 |
+
rows = [[s['timestamp'], s['transcript'][:60] + '...', s['feedback'][:60] + '...'] for s in session_history]
|
| 169 |
+
return rows
|
| 170 |
+
|
| 171 |
+
history_display = gr.Dataframe(headers=["π Timestamp", "π Transcript", "π Feedback Preview"], interactive=False)
|
| 172 |
+
history_btn = gr.Button("π View My Past Sessions")
|
| 173 |
+
|
| 174 |
# Connect functions
|
| 175 |
audio_input.change(
|
| 176 |
fn=tutor_feedback,
|
|
|
|
| 190 |
outputs=example_box
|
| 191 |
)
|
| 192 |
|
| 193 |
+
history_btn.click(
|
| 194 |
+
fn=get_session_table,
|
| 195 |
+
inputs=None,
|
| 196 |
+
outputs=history_display
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
# === Launch App ===
|
| 200 |
if __name__ == "__main__":
|
| 201 |
print("β
App is launching...")
|