Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import speech_recognition as sr
|
|
| 3 |
import difflib
|
| 4 |
|
| 5 |
# Hàm chuyển giọng nói thành văn bản từ file
|
| 6 |
-
def transcribe_speech(audio_file):
|
| 7 |
recognizer = sr.Recognizer()
|
| 8 |
|
| 9 |
# Load audio file
|
|
@@ -13,8 +13,8 @@ def transcribe_speech(audio_file):
|
|
| 13 |
audio = recognizer.record(source)
|
| 14 |
|
| 15 |
try:
|
| 16 |
-
# Chuyển giọng nói thành văn bản
|
| 17 |
-
text = recognizer.recognize_google(audio, language=
|
| 18 |
return text
|
| 19 |
except sr.UnknownValueError:
|
| 20 |
return "Không thể nhận diện giọng nói"
|
|
@@ -38,8 +38,11 @@ def compare_transcription(transcribed_text, reference_text):
|
|
| 38 |
return accuracy, incorrect_words
|
| 39 |
|
| 40 |
# Hàm tích hợp để dùng trên Gradio
|
| 41 |
-
def process_speech(reference_text, audio_file):
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
if "Lỗi" in transcribed_text or "Không thể nhận diện" in transcribed_text:
|
| 45 |
return transcribed_text, None, None
|
|
@@ -57,15 +60,22 @@ def build_interface():
|
|
| 57 |
# Input cho file âm thanh
|
| 58 |
audio_input = gr.Audio(type="filepath", label="Tải lên file âm thanh")
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# Output hiển thị kết quả
|
| 61 |
transcribed_text = gr.Textbox(label="Văn bản bạn nói")
|
| 62 |
accuracy = gr.Textbox(label="Độ chính xác (%)")
|
| 63 |
incorrect_words = gr.Textbox(label="Những từ nói sai")
|
| 64 |
|
| 65 |
-
# Nút
|
| 66 |
gr.Button("Ghi âm và kiểm tra").click(
|
| 67 |
fn=process_speech,
|
| 68 |
-
inputs=[reference_text, audio_input],
|
| 69 |
outputs=[transcribed_text, accuracy, incorrect_words]
|
| 70 |
)
|
| 71 |
|
|
|
|
| 3 |
import difflib
|
| 4 |
|
| 5 |
# Hàm chuyển giọng nói thành văn bản từ file
|
| 6 |
+
def transcribe_speech(audio_file, language_code):
|
| 7 |
recognizer = sr.Recognizer()
|
| 8 |
|
| 9 |
# Load audio file
|
|
|
|
| 13 |
audio = recognizer.record(source)
|
| 14 |
|
| 15 |
try:
|
| 16 |
+
# Chuyển giọng nói thành văn bản với ngôn ngữ được chọn
|
| 17 |
+
text = recognizer.recognize_google(audio, language=language_code)
|
| 18 |
return text
|
| 19 |
except sr.UnknownValueError:
|
| 20 |
return "Không thể nhận diện giọng nói"
|
|
|
|
| 38 |
return accuracy, incorrect_words
|
| 39 |
|
| 40 |
# Hàm tích hợp để dùng trên Gradio
|
| 41 |
+
def process_speech(reference_text, audio_file, language):
|
| 42 |
+
# Map language selection to corresponding language code
|
| 43 |
+
language_code = "vi-VN" if language == "Vietnamese" else "en-US"
|
| 44 |
+
|
| 45 |
+
transcribed_text = transcribe_speech(audio_file, language_code)
|
| 46 |
|
| 47 |
if "Lỗi" in transcribed_text or "Không thể nhận diện" in transcribed_text:
|
| 48 |
return transcribed_text, None, None
|
|
|
|
| 60 |
# Input cho file âm thanh
|
| 61 |
audio_input = gr.Audio(type="filepath", label="Tải lên file âm thanh")
|
| 62 |
|
| 63 |
+
# Dropdown cho chọn ngôn ngữ
|
| 64 |
+
language_selector = gr.Dropdown(
|
| 65 |
+
choices=["English", "Vietnamese"],
|
| 66 |
+
label="Chọn ngôn ngữ",
|
| 67 |
+
value="Vietnamese" # Default value
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
# Output hiển thị kết quả
|
| 71 |
transcribed_text = gr.Textbox(label="Văn bản bạn nói")
|
| 72 |
accuracy = gr.Textbox(label="Độ chính xác (%)")
|
| 73 |
incorrect_words = gr.Textbox(label="Những từ nói sai")
|
| 74 |
|
| 75 |
+
# Nút kiểm tra được kết nối với chức năng xử lý
|
| 76 |
gr.Button("Ghi âm và kiểm tra").click(
|
| 77 |
fn=process_speech,
|
| 78 |
+
inputs=[reference_text, audio_input, language_selector],
|
| 79 |
outputs=[transcribed_text, accuracy, incorrect_words]
|
| 80 |
)
|
| 81 |
|