Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import uuid
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import whisper
|
| 6 |
+
import openai
|
| 7 |
+
|
| 8 |
+
# Load models once
|
| 9 |
+
model = whisper.load_model("base")
|
| 10 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
|
| 12 |
+
# LLM prompt template
|
| 13 |
+
PROMPT_TEMPLATE = """
|
| 14 |
+
You're a communication coach helping users improve their spoken English.
|
| 15 |
+
|
| 16 |
+
Evaluate the following speech transcript and provide helpful, constructive feedback.
|
| 17 |
+
|
| 18 |
+
Focus on:
|
| 19 |
+
- Clarity
|
| 20 |
+
- Fluency
|
| 21 |
+
- Grammar
|
| 22 |
+
- Filler words
|
| 23 |
+
- Tone and pacing
|
| 24 |
+
- Sentence structure
|
| 25 |
+
- Overall communication effectiveness
|
| 26 |
+
|
| 27 |
+
Transcript:
|
| 28 |
+
\"\"\"{transcript}\"\"\"
|
| 29 |
+
|
| 30 |
+
Provide concise feedback in 3-4 bullet points followed by a one-line motivational sentence.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
def generate_feedback_with_llm(transcript):
|
| 34 |
+
prompt = PROMPT_TEMPLATE.format(transcript=transcript.strip())
|
| 35 |
+
|
| 36 |
+
response = openai.ChatCompletion.create(
|
| 37 |
+
model="gpt-3.5-turbo",
|
| 38 |
+
temperature=0.7,
|
| 39 |
+
messages=[
|
| 40 |
+
{"role": "system", "content": "You are a helpful communication tutor."},
|
| 41 |
+
{"role": "user", "content": prompt}
|
| 42 |
+
]
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
return response.choices[0].message["content"]
|
| 46 |
+
|
| 47 |
+
def tutor_feedback(audio_file):
|
| 48 |
+
# Step 1: Transcribe
|
| 49 |
+
result = model.transcribe(audio_file)
|
| 50 |
+
transcript = result["text"]
|
| 51 |
+
|
| 52 |
+
# Step 2: Analyze via LLM
|
| 53 |
+
feedback_text = generate_feedback_with_llm(transcript)
|
| 54 |
+
|
| 55 |
+
# Step 3: TTS
|
| 56 |
+
tts = gTTS(feedback_text)
|
| 57 |
+
output_path = f"/tmp/{uuid.uuid4()}.mp3"
|
| 58 |
+
tts.save(output_path)
|
| 59 |
+
|
| 60 |
+
return transcript, feedback_text, output_path
|
| 61 |
+
|
| 62 |
+
# Gradio UI
|
| 63 |
+
iface = gr.Interface(
|
| 64 |
+
fn=tutor_feedback,
|
| 65 |
+
inputs=gr.Audio(source="microphone", type="filepath", label="π€ Speak your response"),
|
| 66 |
+
outputs=[
|
| 67 |
+
gr.Textbox(label="π Transcript"),
|
| 68 |
+
gr.Textbox(label="π’ Tutor Feedback"),
|
| 69 |
+
gr.Audio(label="π Spoken Feedback", type="filepath")
|
| 70 |
+
],
|
| 71 |
+
title="π£ Communications Tutor with LLM Feedback",
|
| 72 |
+
description="Speak your answer. The AI tutor will transcribe it, analyze it using GPT, and give spoken feedback on how to improve."
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
iface.launch()
|