Update app_utils.py
Browse files- app_utils.py +70 -4
app_utils.py
CHANGED
|
@@ -1,9 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import json
|
| 3 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def parse_scores_from_feedback(feedback_text):
|
| 8 |
try:
|
| 9 |
json_match = re.search(r"\{.*?\}", feedback_text, re.DOTALL)
|
|
@@ -27,7 +91,7 @@ def generate_progress_summary(current_feedback, previous_feedback):
|
|
| 27 |
if diff > 0:
|
| 28 |
lines.append(f"β
**{cat}** improved by **+{diff}**")
|
| 29 |
elif diff < 0:
|
| 30 |
-
lines.append(f"β οΈ **{cat}** dropped by **{diff}**")
|
| 31 |
else:
|
| 32 |
lines.append(f"β **{cat}** stayed the same")
|
| 33 |
|
|
@@ -35,6 +99,7 @@ def generate_progress_summary(current_feedback, previous_feedback):
|
|
| 35 |
return "\n\n**π Progress Tracker**\n" + "\n".join(lines)
|
| 36 |
return ""
|
| 37 |
|
|
|
|
| 38 |
def build_score_comparison_data(current_feedback, previous_feedback):
|
| 39 |
current = parse_scores_from_feedback(current_feedback)
|
| 40 |
previous = parse_scores_from_feedback(previous_feedback)
|
|
@@ -58,7 +123,8 @@ def render_score_chart(data):
|
|
| 58 |
ax.set_title("π― Score Comparison")
|
| 59 |
return fig
|
| 60 |
|
| 61 |
-
|
|
|
|
| 62 |
points = []
|
| 63 |
timestamps = []
|
| 64 |
for s in sessions:
|
|
@@ -68,7 +134,7 @@ def build_trend_data(sessions, category="Tone"):
|
|
| 68 |
timestamps.append(s.timestamp.split()[0])
|
| 69 |
return timestamps, points
|
| 70 |
|
| 71 |
-
def render_trend_chart(timestamps, points, category="
|
| 72 |
fig, ax = plt.subplots()
|
| 73 |
ax.plot(timestamps, points, marker="o", linestyle="-", color="blue")
|
| 74 |
ax.set_title(f"π {category} Progress Over Time")
|
|
|
|
| 1 |
+
from sqlmodel import SQLModel, Field, create_engine, Session, select
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from typing import Optional
|
| 4 |
+
import os
|
| 5 |
+
import uuid
|
| 6 |
+
import subprocess
|
| 7 |
import re
|
| 8 |
import json
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
+
from faster_whisper import WhisperModel
|
| 11 |
+
from openai import OpenAI
|
| 12 |
|
| 13 |
+
# === Setup ===
|
| 14 |
+
db_path = "/tmp/chatter_sessions.db"
|
| 15 |
+
engine = create_engine(f"sqlite:///{db_path}")
|
| 16 |
+
SQLModel.metadata.create_all(engine)
|
| 17 |
|
| 18 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 19 |
+
client = OpenAI(api_key=openai_api_key)
|
| 20 |
+
|
| 21 |
+
# === Language Map ===
|
| 22 |
+
LANG_CODES = {
|
| 23 |
+
"English": "en", "Spanish": "es", "Hindi": "hi", "French": "fr", "German": "de",
|
| 24 |
+
"Arabic": "ar", "Chinese": "zh", "Portuguese": "pt", "Japanese": "ja", "Korean": "ko"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# === Session Table (shared for spoken + written) ===
|
| 28 |
+
class SessionEntry(SQLModel, table=True):
|
| 29 |
+
id: Optional[int] = Field(default=None, primary_key=True)
|
| 30 |
+
user: str
|
| 31 |
+
timestamp: str
|
| 32 |
+
transcript: str
|
| 33 |
+
feedback: str
|
| 34 |
+
language: str
|
| 35 |
+
|
| 36 |
+
# === Session Utilities ===
|
| 37 |
+
def save_to_db(user, transcript, feedback, language):
|
| 38 |
+
session = Session(engine)
|
| 39 |
+
entry = SessionEntry(
|
| 40 |
+
user=user,
|
| 41 |
+
timestamp=datetime.now().strftime("%Y-%m-%d %H:%M"),
|
| 42 |
+
transcript=transcript,
|
| 43 |
+
feedback=feedback,
|
| 44 |
+
language=language
|
| 45 |
+
)
|
| 46 |
+
session.add(entry)
|
| 47 |
+
session.commit()
|
| 48 |
+
session.close()
|
| 49 |
+
|
| 50 |
+
def fetch_user_sessions(user):
|
| 51 |
+
session = Session(engine)
|
| 52 |
+
statement = select(SessionEntry).where(SessionEntry.user == user)
|
| 53 |
+
results = session.exec(statement).all()
|
| 54 |
+
session.close()
|
| 55 |
+
return results
|
| 56 |
+
|
| 57 |
+
# === Whisper Audio Utilities (Spoken Only) ===
|
| 58 |
+
model = WhisperModel("base", compute_type="int8")
|
| 59 |
+
|
| 60 |
+
def convert_to_wav(input_file):
|
| 61 |
+
output_wav = f"/tmp/{uuid.uuid4()}.wav"
|
| 62 |
+
command = ["ffmpeg", "-y", "-i", input_file, "-ar", "16000", "-ac", "1", output_wav]
|
| 63 |
+
subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 64 |
+
return output_wav
|
| 65 |
+
|
| 66 |
+
def transcribe_audio(audio_path):
|
| 67 |
+
segments, _ = model.transcribe(audio_path)
|
| 68 |
+
return " ".join([segment.text for segment in segments])
|
| 69 |
+
|
| 70 |
+
# === Score Extraction & Comparison Utilities ===
|
| 71 |
def parse_scores_from_feedback(feedback_text):
|
| 72 |
try:
|
| 73 |
json_match = re.search(r"\{.*?\}", feedback_text, re.DOTALL)
|
|
|
|
| 91 |
if diff > 0:
|
| 92 |
lines.append(f"β
**{cat}** improved by **+{diff}**")
|
| 93 |
elif diff < 0:
|
| 94 |
+
lines.append(f"β οΈ **{cat}** dropped by **{abs(diff)}**")
|
| 95 |
else:
|
| 96 |
lines.append(f"β **{cat}** stayed the same")
|
| 97 |
|
|
|
|
| 99 |
return "\n\n**π Progress Tracker**\n" + "\n".join(lines)
|
| 100 |
return ""
|
| 101 |
|
| 102 |
+
# === Score Charting (Current vs. Previous) ===
|
| 103 |
def build_score_comparison_data(current_feedback, previous_feedback):
|
| 104 |
current = parse_scores_from_feedback(current_feedback)
|
| 105 |
previous = parse_scores_from_feedback(previous_feedback)
|
|
|
|
| 123 |
ax.set_title("π― Score Comparison")
|
| 124 |
return fig
|
| 125 |
|
| 126 |
+
# === Trend Charting (Category Over Time) ===
|
| 127 |
+
def build_trend_data(sessions, category="Clarity"):
|
| 128 |
points = []
|
| 129 |
timestamps = []
|
| 130 |
for s in sessions:
|
|
|
|
| 134 |
timestamps.append(s.timestamp.split()[0])
|
| 135 |
return timestamps, points
|
| 136 |
|
| 137 |
+
def render_trend_chart(timestamps, points, category="Clarity"):
|
| 138 |
fig, ax = plt.subplots()
|
| 139 |
ax.plot(timestamps, points, marker="o", linestyle="-", color="blue")
|
| 140 |
ax.set_title(f"π {category} Progress Over Time")
|