ankban commited on
Commit
981c6f0
Β·
verified Β·
1 Parent(s): 780eed3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -30
app.py CHANGED
@@ -1,18 +1,20 @@
1
  import gradio as gr
2
  import os
3
  import uuid
4
- from gtts import gTTS
5
  import whisper
 
 
6
  import openai
7
- import wave
8
 
9
- # Load models once
10
- # Force whisper to use a writable cache dir
11
  os.environ["XDG_CACHE_HOME"] = "/tmp/.cache"
 
 
 
12
  model = whisper.load_model("base")
13
  openai.api_key = os.getenv("OPENAI_API_KEY")
14
 
15
- # LLM prompt template
16
  PROMPT_TEMPLATE = """
17
  You're a communication coach helping users improve their spoken English.
18
 
@@ -33,9 +35,16 @@ Transcript:
33
  Provide concise feedback in 3-4 bullet points followed by a one-line motivational sentence.
34
  """
35
 
 
 
 
 
 
 
 
 
36
  def generate_feedback_with_llm(transcript):
37
  prompt = PROMPT_TEMPLATE.format(transcript=transcript.strip())
38
-
39
  response = openai.ChatCompletion.create(
40
  model="gpt-3.5-turbo",
41
  temperature=0.7,
@@ -44,35 +53,34 @@ def generate_feedback_with_llm(transcript):
44
  {"role": "user", "content": prompt}
45
  ]
46
  )
47
-
48
  return response.choices[0].message["content"]
49
 
50
- def is_valid_audio(filepath):
51
- try:
52
- with wave.open(filepath, 'rb') as wf:
53
- return wf.getnframes() > 0
54
- except Exception:
55
- return False
56
-
57
  def tutor_feedback(audio_file):
58
- if not audio_file or not os.path.exists(audio_file) or not is_valid_audio(audio_file):
59
- return "No valid audio received.", "Please record again.", None
60
-
61
- # Step 1: Transcribe
62
- result = model.transcribe(audio_file)
 
 
 
 
 
 
63
  transcript = result["text"]
64
 
65
- # Step 2: Analyze via LLM
66
  feedback_text = generate_feedback_with_llm(transcript)
67
 
68
- # Step 3: TTS
69
  tts = gTTS(feedback_text)
70
- output_path = f"/tmp/{uuid.uuid4()}.mp3"
71
- tts.save(output_path)
72
 
73
- return transcript, feedback_text, output_path
74
 
75
- # Gradio UI
76
  iface = gr.Interface(
77
  fn=tutor_feedback,
78
  inputs=gr.Audio(type="filepath", label="🎀 Upload or record your response"),
@@ -81,10 +89,11 @@ iface = gr.Interface(
81
  gr.Textbox(label="πŸ“’ Tutor Feedback"),
82
  gr.Audio(label="πŸ”Š Spoken Feedback", type="filepath")
83
  ],
84
- title="πŸ—£ Communications Tutor with LLM Feedback",
85
- description="Speak your answer. The AI tutor will transcribe it, analyze it using GPT, and give spoken feedback on how to improve.",
86
- allow_flagging="never" # πŸ‘ˆ This disables flagging entirely
87
  )
88
 
89
- print(">>> App is ready. Launching Gradio...")
90
- iface.launch(server_name="0.0.0.0", server_port=7860, debug=True)
 
 
1
  import gradio as gr
2
  import os
3
  import uuid
 
4
  import whisper
5
+ from gtts import gTTS
6
+ import subprocess
7
  import openai
 
8
 
9
+ # Use writable cache for Hugging Face
 
10
  os.environ["XDG_CACHE_HOME"] = "/tmp/.cache"
11
+ os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
12
+
13
+ # Load Whisper and set OpenAI key
14
  model = whisper.load_model("base")
15
  openai.api_key = os.getenv("OPENAI_API_KEY")
16
 
17
+ # Prompt template
18
  PROMPT_TEMPLATE = """
19
  You're a communication coach helping users improve their spoken English.
20
 
 
35
  Provide concise feedback in 3-4 bullet points followed by a one-line motivational sentence.
36
  """
37
 
38
+ # Convert uploaded audio to 16kHz mono WAV
39
+ def convert_to_wav(input_file):
40
+ output_wav = f"/tmp/{uuid.uuid4()}.wav"
41
+ command = ["ffmpeg", "-y", "-i", input_file, "-ar", "16000", "-ac", "1", output_wav]
42
+ subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
43
+ return output_wav
44
+
45
+ # Call GPT to analyze transcript
46
  def generate_feedback_with_llm(transcript):
47
  prompt = PROMPT_TEMPLATE.format(transcript=transcript.strip())
 
48
  response = openai.ChatCompletion.create(
49
  model="gpt-3.5-turbo",
50
  temperature=0.7,
 
53
  {"role": "user", "content": prompt}
54
  ]
55
  )
 
56
  return response.choices[0].message["content"]
57
 
58
+ # Main function
 
 
 
 
 
 
59
  def tutor_feedback(audio_file):
60
+ if not audio_file or not os.path.exists(audio_file):
61
+ return "No audio received.", "Please upload or record again.", None
62
+
63
+ print(f">>> Audio received: {audio_file}")
64
+ print(f">>> File size: {os.path.getsize(audio_file)} bytes")
65
+
66
+ # Convert to whisper-friendly WAV
67
+ wav_path = convert_to_wav(audio_file)
68
+
69
+ # Transcribe
70
+ result = model.transcribe(wav_path)
71
  transcript = result["text"]
72
 
73
+ # Feedback via GPT
74
  feedback_text = generate_feedback_with_llm(transcript)
75
 
76
+ # TTS
77
  tts = gTTS(feedback_text)
78
+ mp3_path = f"/tmp/{uuid.uuid4()}.mp3"
79
+ tts.save(mp3_path)
80
 
81
+ return transcript, feedback_text, mp3_path
82
 
83
+ # Gradio interface
84
  iface = gr.Interface(
85
  fn=tutor_feedback,
86
  inputs=gr.Audio(type="filepath", label="🎀 Upload or record your response"),
 
89
  gr.Textbox(label="πŸ“’ Tutor Feedback"),
90
  gr.Audio(label="πŸ”Š Spoken Feedback", type="filepath")
91
  ],
92
+ title="πŸ—£ Communications Tutor with Whisper + GPT",
93
+ description="Speak or upload a response. The tutor will transcribe it, analyze with GPT, and respond with spoken feedback.",
94
+ allow_flagging="never"
95
  )
96
 
97
+ if __name__ == "__main__":
98
+ print("βœ… App is launching...")
99
+ iface.launch(server_name="0.0.0.0", server_port=7860, debug=True)