File size: 3,702 Bytes
001a676
 
 
817c4af
 
 
 
33063e3
817c4af
 
 
001a676
 
817c4af
33063e3
 
 
817c4af
 
 
 
 
001a676
817c4af
 
 
 
 
 
 
 
001a676
817c4af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
001a676
817c4af
 
 
 
 
 
 
 
 
 
 
 
d981ac8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
817c4af
 
d981ac8
 
 
817c4af
 
 
d981ac8
817c4af
 
 
 
 
 
d981ac8
817c4af
d981ac8
817c4af
d981ac8
817c4af
 
 
 
 
 
 
 
 
 
d981ac8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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