ankban commited on
Commit
817c4af
·
verified ·
1 Parent(s): 6cb4362

Upload app_utils.py

Browse files
Files changed (1) hide show
  1. app_utils.py +107 -0
app_utils.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import uuid
4
+ import subprocess
5
+ from datetime import datetime
6
+ from sqlmodel import SQLModel, Field, create_engine, Session, select
7
+ from typing import Optional
8
+ from faster_whisper import WhisperModel
9
+ from openai import OpenAI
10
+
11
+ # === Setup ===
12
+ db_path = "/tmp/chatter_sessions.db"
13
+ openai.api_key = os.getenv("OPENAI_API_KEY")
14
+ client = OpenAI(api_key=openai.api_key)
15
+
16
+ LANG_CODES = {
17
+ "English": "en", "Spanish": "es", "Hindi": "hi", "French": "fr", "German": "de",
18
+ "Arabic": "ar", "Chinese": "zh", "Portuguese": "pt", "Japanese": "ja", "Korean": "ko"
19
+ }
20
+
21
+ # === SQLModel setup ===
22
+ class SessionEntry(SQLModel, table=True):
23
+ id: Optional[int] = Field(default=None, primary_key=True)
24
+ user: str
25
+ timestamp: str
26
+ transcript: str
27
+ feedback: str
28
+ language: str
29
+
30
+ engine = create_engine(f"sqlite:///{db_path}")
31
+ SQLModel.metadata.create_all(engine)
32
+
33
+ def save_to_db(user, transcript, feedback, language):
34
+ session = Session(engine)
35
+ entry = SessionEntry(
36
+ user=user,
37
+ timestamp=datetime.now().strftime("%Y-%m-%d %H:%M"),
38
+ transcript=transcript,
39
+ feedback=feedback,
40
+ language=language
41
+ )
42
+ session.add(entry)
43
+ session.commit()
44
+ session.close()
45
+
46
+ def fetch_user_sessions(user):
47
+ session = Session(engine)
48
+ statement = select(SessionEntry).where(SessionEntry.user == user)
49
+ results = session.exec(statement).all()
50
+ session.close()
51
+ return results
52
+
53
+ # === Whisper ===
54
+ model = WhisperModel("base", compute_type="int8")
55
+
56
+ def convert_to_wav(input_file):
57
+ output_wav = f"/tmp/{uuid.uuid4()}.wav"
58
+ command = ["ffmpeg", "-y", "-i", input_file, "-ar", "16000", "-ac", "1", output_wav]
59
+ subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
60
+ return output_wav
61
+
62
+ def transcribe_audio(audio_path):
63
+ segments, _ = model.transcribe(audio_path)
64
+ return " ".join([segment.text for segment in segments])
65
+
66
+ # === GPT ===
67
+ def generate_feedback(transcript, language):
68
+ prompt = f"""You are a communication coach. Please respond in [language={language}].
69
+ Evaluate the user's speech on:
70
+ 1. Clarity
71
+ 2. Structure
72
+ 3. Fluency
73
+ 4. Content Relevance
74
+ 5. Tone & Expression
75
+ Each category:
76
+ - Score out of 10
77
+ - Short explanation
78
+ End with:
79
+ - Overall feedback summary
80
+ - One motivational line
81
+ Transcript:
82
+ {transcript}
83
+ """
84
+ response = client.chat.completions.create(
85
+ model="gpt-4",
86
+ messages=[
87
+ {"role": "system", "content": f"You are a supportive communication coach responding in {language}."},
88
+ {"role": "user", "content": prompt}
89
+ ],
90
+ temperature=0.7
91
+ )
92
+ return response.choices[0].message.content
93
+
94
+ def generate_example_response(transcript, language):
95
+ prompt = f"""You are a communication coach. Rewrite this speech to make it more polished, fluent, and confident.
96
+ Keep the meaning and tone the same, but improve clarity and structure.
97
+ Transcript:
98
+ {transcript}
99
+ """
100
+ response = client.chat.completions.create(
101
+ model="gpt-4",
102
+ messages=[
103
+ {"role": "system", "content": f"Reply in {language}. Provide only the improved version of the speech."},
104
+ {"role": "user", "content": prompt}
105
+ ]
106
+ )
107
+ return response.choices[0].message.content