Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
|
|
|
|
|
|
3 |
import os
|
4 |
|
5 |
# Define your sentences here
|
@@ -16,29 +19,66 @@ sents = [
|
|
16 |
]
|
17 |
|
18 |
def text_to_speech(selected_sentence, language):
|
19 |
-
# Adjust the language code for the British English accent
|
20 |
tld = 'co.uk' if language == "British English" else 'com'
|
21 |
-
|
22 |
sn = int(selected_sentence.split(".")[0]) # Extract the sentence number
|
23 |
mytext = sents[sn - 1] # Get the selected sentence
|
24 |
|
25 |
-
# Create a gTTS object with the selected language and tld for accent
|
26 |
tts = gTTS(text=mytext, lang='en', tld=tld, slow=False)
|
27 |
-
|
28 |
-
# Save the speech to a temporary file
|
29 |
filename = 'output.mp3'
|
30 |
tts.save(filename)
|
31 |
return filename
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
3 |
+
import speech_recognition as sr
|
4 |
+
from difflib import SequenceMatcher
|
5 |
+
import tempfile
|
6 |
import os
|
7 |
|
8 |
# Define your sentences here
|
|
|
19 |
]
|
20 |
|
21 |
def text_to_speech(selected_sentence, language):
|
|
|
22 |
tld = 'co.uk' if language == "British English" else 'com'
|
23 |
+
|
24 |
sn = int(selected_sentence.split(".")[0]) # Extract the sentence number
|
25 |
mytext = sents[sn - 1] # Get the selected sentence
|
26 |
|
|
|
27 |
tts = gTTS(text=mytext, lang='en', tld=tld, slow=False)
|
|
|
|
|
28 |
filename = 'output.mp3'
|
29 |
tts.save(filename)
|
30 |
return filename
|
31 |
|
32 |
+
def recognize_speech_from_microphone(audio_path):
|
33 |
+
recognizer = sr.Recognizer()
|
34 |
+
try:
|
35 |
+
with sr.AudioFile(audio_path) as source:
|
36 |
+
audio_data = recognizer.record(source)
|
37 |
+
text = recognizer.recognize_google(audio_data)
|
38 |
+
return text
|
39 |
+
except sr.UnknownValueError:
|
40 |
+
return "Could not understand the audio"
|
41 |
+
except sr.RequestError as e:
|
42 |
+
return f"Could not request results from Google Speech Recognition service; {e}"
|
43 |
+
except Exception as e:
|
44 |
+
return str(e)
|
45 |
+
|
46 |
+
def calculate_similarity(original_text, recognized_text):
|
47 |
+
return SequenceMatcher(None, original_text.lower(), recognized_text.lower()).ratio() * 100
|
48 |
+
|
49 |
+
def process_audio(selected_sentence, audio_path):
|
50 |
+
sn = int(selected_sentence.split(".")[0]) # Extract the sentence number
|
51 |
+
original_text = sents[sn - 1] # Get the selected sentence
|
52 |
+
recognized_text = recognize_speech_from_microphone(audio_path)
|
53 |
+
if "Error" in recognized_text or "Could not" in recognized_text:
|
54 |
+
return recognized_text, 0.0
|
55 |
+
similarity = calculate_similarity(original_text, recognized_text)
|
56 |
+
return recognized_text, similarity
|
57 |
+
|
58 |
+
def display_sentence(selected_sentence):
|
59 |
+
sn = int(selected_sentence.split(".")[0])
|
60 |
+
return sents[sn - 1]
|
61 |
+
|
62 |
+
with gr.Blocks() as demo:
|
63 |
+
with gr.Row():
|
64 |
+
with gr.Column():
|
65 |
+
gr.Markdown("### Text-to-Speech Converter")
|
66 |
+
dropdown_sentences = gr.Dropdown(choices=[f"{i}. {sents[i-1]}" for i in range(1, len(sents) + 1)], label="Select Sentence")
|
67 |
+
radio_language = gr.Radio(choices=['English', 'British English'], label="Language")
|
68 |
+
generate_tts_button = gr.Button("Generate Speech")
|
69 |
+
tts_audio_output = gr.Audio(type="filepath", label="Output Audio")
|
70 |
+
generate_tts_button.click(text_to_speech, inputs=[dropdown_sentences, radio_language], outputs=tts_audio_output)
|
71 |
+
selected_sentence_display = gr.Textbox(label="Selected Sentence", interactive=False)
|
72 |
+
dropdown_sentences.change(display_sentence, inputs=dropdown_sentences, outputs=selected_sentence_display)
|
73 |
+
|
74 |
+
with gr.Row():
|
75 |
+
with gr.Column():
|
76 |
+
gr.Markdown("### Pronunciation Evaluator")
|
77 |
+
mic_input = gr.Audio(label="Your Pronunciation", type="filepath")
|
78 |
+
result_button = gr.Button("Evaluate Pronunciation")
|
79 |
+
recognized_text = gr.Textbox(label="Recognized Text")
|
80 |
+
similarity_score = gr.Number(label="Similarity (%)")
|
81 |
+
|
82 |
+
result_button.click(process_audio, inputs=[dropdown_sentences, mic_input], outputs=[recognized_text, similarity_score])
|
83 |
+
|
84 |
+
demo.launch()
|