CommTutor / app_utils.py
ankban's picture
Update app_utils.py
d981ac8 verified
Raw
History Blame
3.7 kB
from sqlmodel import SQLModel, Field, create_engine, Session, select
from datetime import datetime
from typing import Optional
import os
import uuid
import subprocess
from faster_whisper import WhisperModel
from openai import OpenAI
# === Setup ===
db_path = "/tmp/chatter_sessions.db"
engine = create_engine(f"sqlite:///{db_path}")
SQLModel.metadata.create_all(engine)
openai_api_key = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=openai_api_key)
LANG_CODES = {
"English": "en", "Spanish": "es", "Hindi": "hi", "French": "fr", "German": "de",
"Arabic": "ar", "Chinese": "zh", "Portuguese": "pt", "Japanese": "ja", "Korean": "ko"
}
# === Spoken Session Table ===
class SessionEntry(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
user: str
timestamp: str
transcript: str
feedback: str
language: str
# === Spoken Session Utilities ===
def save_to_db(user, transcript, feedback, language):
session = Session(engine)
entry = SessionEntry(
user=user,
timestamp=datetime.now().strftime("%Y-%m-%d %H:%M"),
transcript=transcript,
feedback=feedback,
language=language
)
session.add(entry)
session.commit()
session.close()
def fetch_user_sessions(user):
session = Session(engine)
statement = select(SessionEntry).where(SessionEntry.user == user)
results = session.exec(statement).all()
session.close()
return results
# === Whisper Model ===
model = WhisperModel("base", compute_type="int8")
def convert_to_wav(input_file):
output_wav = f"/tmp/{uuid.uuid4()}.wav"
command = ["ffmpeg", "-y", "-i", input_file, "-ar", "16000", "-ac", "1", output_wav]
subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return output_wav
def transcribe_audio(audio_path):
segments, _ = model.transcribe(audio_path)
return " ".join([segment.text for segment in segments])
# === GPT: Personalized Feedback ===
def generate_feedback(transcript, language, goal="general improvement", focus_areas=None, previous_transcript=None):
focus_str = ", ".join(focus_areas) if focus_areas else "Clarity, Structure, Fluency, Content Relevance, and Tone"
history_section = f"\n\nFor reference, their previous transcript was:\n{previous_transcript}" if previous_transcript else ""
prompt = f"""
You are a supportive communication coach helping a learner whose goal is: **{goal}**.
Evaluate the user's current speech based on the following areas:
{focus_str}
Give a score out of 10 and a short explanation for each area.
Then provide:
- A summary of strengths and improvement areas.
- One motivational line to end with.
Transcript:
{transcript}
{history_section}
""".strip()
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"You are a warm and constructive communication coach responding in {language}."},
{"role": "user", "content": prompt}
],
temperature=0.7
)
return response.choices[0].message.content
# === GPT: Improved Response ===
def generate_example_response(transcript, language):
prompt = f"""Rewrite this speech to make it more polished, fluent, and confident.
Keep the meaning and tone the same, but improve clarity and structure.
Transcript:
{transcript}
"""
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"Reply in {language}. Provide only the improved version of the speech."},
{"role": "user", "content": prompt}
]
)
return response.choices[0].message.content