Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,54 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import whisper
|
| 3 |
-
import difflib
|
| 4 |
|
| 5 |
-
# Load the Whisper model
|
| 6 |
model = whisper.load_model("base")
|
| 7 |
|
| 8 |
def pronunciation_feedback(transcription, reference_text):
|
| 9 |
"""
|
| 10 |
-
Function to
|
| 11 |
-
|
| 12 |
"""
|
|
|
|
| 13 |
diff = difflib.ndiff(reference_text.split(), transcription.split())
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
if errors:
|
| 16 |
-
feedback = "
|
| 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 |
-
|
| 24 |
"""
|
| 25 |
-
# Transcribe the audio using Whisper
|
| 26 |
result = model.transcribe(audio)
|
| 27 |
transcription = result['text']
|
| 28 |
|
| 29 |
-
# Provide
|
| 30 |
feedback = pronunciation_feedback(transcription, reference_text)
|
| 31 |
|
| 32 |
return transcription, feedback
|
| 33 |
|
| 34 |
-
#
|
| 35 |
interface = gr.Interface(
|
| 36 |
-
fn=transcribe_and_feedback, # Function to transcribe and
|
| 37 |
-
inputs=[
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|