Download app_utils.py from ankban/CommTutor: direct link, hf CLI and curl.
- Browser
- Download file 3.21 kB
-
https://huggingface.co/spaces/ankban/CommTutor/resolve/8a9bac85be2eafdff407dc57b7d0ff8fd9cf19b2/app_utils.py
- Command line
-
hf download hf://spaces/ankban/CommTutor@8a9bac85be2eafdff407dc57b7d0ff8fd9cf19b2/app_utils.py
-
curl -L -o app_utils.py https://huggingface.co/spaces/ankban/CommTutor/resolve/8a9bac85be2eafdff407dc57b7d0ff8fd9cf19b2/app_utils.py
3.21 kB
| import os | |
| import uuid | |
| import subprocess | |
| from datetime import datetime | |
| from sqlmodel import SQLModel, Field, create_engine, Session, select | |
| from typing import Optional | |
| from faster_whisper import WhisperModel | |
| from openai import OpenAI | |
| # === Setup === | |
| db_path = "/tmp/chatter_sessions.db" | |
| client = OpenAI(api_key=os.getenv("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" | |
| } | |
| # === SQLModel setup === | |
| class SessionEntry(SQLModel, table=True): | |
| id: Optional[int] = Field(default=None, primary_key=True) | |
| user: str | |
| timestamp: str | |
| transcript: str | |
| feedback: str | |
| language: str | |
| engine = create_engine(f"sqlite:///{db_path}") | |
| SQLModel.metadata.create_all(engine) | |
| 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 = 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 === | |
| def generate_feedback(transcript, language): | |
| prompt = f"""You are a communication coach. Please respond in [language={language}]. | |
| Evaluate the user's speech on: | |
| 1. Clarity | |
| 2. Structure | |
| 3. Fluency | |
| 4. Content Relevance | |
| 5. Tone & Expression | |
| Each category: | |
| - Score out of 10 | |
| - Short explanation | |
| End with: | |
| - Overall feedback summary | |
| - One motivational line | |
| Transcript: | |
| {transcript} | |
| """ | |
| response = client.chat.completions.create( | |
| model="gpt-4", | |
| messages=[ | |
| {"role": "system", "content": f"You are a supportive communication coach responding in {language}."}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| temperature=0.7 | |
| ) | |
| return response.choices[0].message.content | |
| def generate_example_response(transcript, language): | |
| prompt = f"""You are a communication coach. 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 | |