AI-Edify commited on
Commit
e7ce94f
·
verified ·
1 Parent(s): fc05dbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -15
app.py CHANGED
@@ -1,43 +1,54 @@
1
  import gradio as gr
2
  import whisper
3
- import difflib # To compare expected vs actual pronunciation
4
 
5
- # Load the Whisper model
6
  model = whisper.load_model("base")
7
 
8
  def pronunciation_feedback(transcription, reference_text):
9
  """
10
- Function to give basic feedback on pronunciation based on differences
11
- between the transcribed text and the reference text.
12
  """
 
13
  diff = difflib.ndiff(reference_text.split(), transcription.split())
14
- errors = [word for word in diff if word.startswith('- ')] # Find words missing or mispronounced
 
 
 
15
  if errors:
16
- feedback = "You mispronounced the following words: " + ', '.join([error[2:] for error in errors])
17
  else:
18
  feedback = "Great job! Your pronunciation is spot on."
 
19
  return feedback
20
 
21
  def transcribe_and_feedback(audio, reference_text):
22
  """
23
- Transcribes audio and provides pronunciation feedback.
24
  """
25
- # Transcribe the audio using Whisper
26
  result = model.transcribe(audio)
27
  transcription = result['text']
28
 
29
- # Provide basic pronunciation feedback
30
  feedback = pronunciation_feedback(transcription, reference_text)
31
 
32
  return transcription, feedback
33
 
34
- # Create the Gradio interface for real-time transcription and feedback
35
  interface = gr.Interface(
36
- fn=transcribe_and_feedback, # Function to transcribe and give feedback
37
- inputs=[gr.Audio(source="microphone", type="filepath"), gr.Textbox(label="Expected Text")],
38
- outputs=[gr.Textbox(label="Transcription"), gr.Textbox(label="Pronunciation Feedback")],
39
- live=True # Enables real-time transcription
 
 
 
 
 
 
40
  )
41
 
42
- # Launch the Gradio interface
43
  interface.launch(share=True)
 
1
  import gradio as gr
2
  import whisper
3
+ import difflib
4
 
5
+ # Load the Whisper model (base model is a good balance between speed and accuracy)
6
  model = whisper.load_model("base")
7
 
8
  def pronunciation_feedback(transcription, reference_text):
9
  """
10
+ Function to provide basic pronunciation feedback by comparing the transcription
11
+ with the reference (expected) text.
12
  """
13
+ # Compare transcription with reference text using difflib
14
  diff = difflib.ndiff(reference_text.split(), transcription.split())
15
+
16
+ # Identify words that are incorrect or missing in transcription
17
+ errors = [word for word in diff if word.startswith('- ')]
18
+
19
  if errors:
20
+ feedback = "Mispronounced words: " + ', '.join([error[2:] for error in errors])
21
  else:
22
  feedback = "Great job! Your pronunciation is spot on."
23
+
24
  return feedback
25
 
26
  def transcribe_and_feedback(audio, reference_text):
27
  """
28
+ Transcribe the audio and provide pronunciation feedback.
29
  """
30
+ # Transcribe the audio using Whisper model
31
  result = model.transcribe(audio)
32
  transcription = result['text']
33
 
34
+ # Provide pronunciation feedback
35
  feedback = pronunciation_feedback(transcription, reference_text)
36
 
37
  return transcription, feedback
38
 
39
+ # Set up the Gradio interface
40
  interface = gr.Interface(
41
+ fn=transcribe_and_feedback, # Function to transcribe and provide feedback
42
+ inputs=[
43
+ gr.Audio(source="microphone", type="filepath"), # Live audio input
44
+ gr.Textbox(label="Expected Text") # User provides the reference text
45
+ ],
46
+ outputs=[
47
+ gr.Textbox(label="Transcription"), # Display transcription
48
+ gr.Textbox(label="Pronunciation Feedback") # Display feedback
49
+ ],
50
+ live=True # Enable real-time transcription
51
  )
52
 
53
+ # Launch the Gradio interface on Hugging Face Spaces
54
  interface.launch(share=True)