RakanAlsheraiwi
commited on
Commit
•
938d676
1
Parent(s):
2551a01
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,35 @@
|
|
1 |
import gradio as gr
|
2 |
import difflib
|
|
|
|
|
3 |
|
4 |
-
# Function to
|
5 |
-
def pronunciation_feedback(user_audio,
|
6 |
-
#
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Compare the user's text with the correct text
|
13 |
-
ratio = difflib.SequenceMatcher(None,
|
14 |
|
15 |
# Generate feedback based on similarity
|
16 |
if ratio > 0.9:
|
@@ -19,19 +38,20 @@ def pronunciation_feedback(user_audio, sentence):
|
|
19 |
feedback = "Good job! But you can improve your pronunciation on some words."
|
20 |
else:
|
21 |
feedback = "Keep practicing! Try to mimic the pronunciation more closely."
|
22 |
-
|
23 |
-
|
|
|
24 |
|
25 |
# Gradio Interface
|
26 |
interface = gr.Interface(
|
27 |
fn=pronunciation_feedback,
|
28 |
inputs=[
|
29 |
gr.Audio(type="filepath", label="Record your pronunciation"),
|
30 |
-
gr.
|
31 |
],
|
32 |
outputs="text",
|
33 |
-
title="Pronunciation Trainer",
|
34 |
-
description="Record yourself pronouncing the given sentence and receive feedback
|
35 |
)
|
36 |
|
37 |
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import difflib
|
3 |
+
import speech_recognition as sr
|
4 |
+
import random
|
5 |
|
6 |
+
# Function to perform speech recognition and provide feedback
|
7 |
+
def pronunciation_feedback(user_audio, language):
|
8 |
+
# Initialize recognizer
|
9 |
+
recognizer = sr.Recognizer()
|
10 |
+
|
11 |
+
# Load the audio file
|
12 |
+
with sr.AudioFile(user_audio) as source:
|
13 |
+
audio_data = recognizer.record(source)
|
14 |
+
|
15 |
+
# Convert audio to text
|
16 |
+
try:
|
17 |
+
user_text = recognizer.recognize_google(audio_data, language=language)
|
18 |
+
except sr.UnknownValueError:
|
19 |
+
return "Sorry, I couldn't understand the audio. Please try again."
|
20 |
+
except sr.RequestError:
|
21 |
+
return "There was an error with the speech recognition service. Please try again later."
|
22 |
|
23 |
+
# Provide a sentence for the user to pronounce (could be dynamically generated or chosen)
|
24 |
+
sentence = random.choice([
|
25 |
+
"This is a simple test sentence.",
|
26 |
+
"I would like to improve my pronunciation.",
|
27 |
+
"Artificial intelligence is transforming the world.",
|
28 |
+
"Gradio is a fantastic tool for machine learning applications."
|
29 |
+
]).lower()
|
30 |
|
31 |
# Compare the user's text with the correct text
|
32 |
+
ratio = difflib.SequenceMatcher(None, sentence, user_text.lower()).ratio()
|
33 |
|
34 |
# Generate feedback based on similarity
|
35 |
if ratio > 0.9:
|
|
|
38 |
feedback = "Good job! But you can improve your pronunciation on some words."
|
39 |
else:
|
40 |
feedback = "Keep practicing! Try to mimic the pronunciation more closely."
|
41 |
+
|
42 |
+
# Return feedback and the correct sentence for comparison
|
43 |
+
return f"Recognized Text: {user_text}\n\nTarget Sentence: {sentence}\n\nFeedback: {feedback}"
|
44 |
|
45 |
# Gradio Interface
|
46 |
interface = gr.Interface(
|
47 |
fn=pronunciation_feedback,
|
48 |
inputs=[
|
49 |
gr.Audio(type="filepath", label="Record your pronunciation"),
|
50 |
+
gr.Dropdown(["en-US", "en-GB", "fr-FR", "es-ES"], label="Select Language", value="en-US")
|
51 |
],
|
52 |
outputs="text",
|
53 |
+
title="Smart Pronunciation Trainer",
|
54 |
+
description="Record yourself pronouncing the given sentence in your selected language and receive AI-powered feedback."
|
55 |
)
|
56 |
|
57 |
interface.launch()
|