Mrlongpro commited on
Commit
e6d5013
·
verified ·
1 Parent(s): f42a490

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -2,14 +2,15 @@ import gradio as gr
2
  import speech_recognition as sr
3
  import difflib
4
 
5
- # Hàm chuyển giọng nói thành văn bản
6
- def transcribe_speech():
7
  recognizer = sr.Recognizer()
8
 
9
- with sr.Microphone() as source:
 
10
  # Điều chỉnh tiếng ồn nền và ghi âm
11
  recognizer.adjust_for_ambient_noise(source, duration=1)
12
- audio = recognizer.listen(source)
13
 
14
  try:
15
  # Chuyển giọng nói thành văn bản
@@ -37,8 +38,8 @@ def compare_transcription(transcribed_text, reference_text):
37
  return accuracy, incorrect_words
38
 
39
  # Hàm tích hợp để dùng trên Gradio
40
- def process_speech(reference_text):
41
- transcribed_text = transcribe_speech()
42
 
43
  if "Lỗi" in transcribed_text or "Không thể nhận diện" in transcribed_text:
44
  return transcribed_text, None, None
@@ -52,17 +53,21 @@ def build_interface():
52
  with gr.Blocks() as demo:
53
  # Input cho văn bản mẫu
54
  reference_text = gr.Textbox(label="Văn bản mẫu", value="Xin chào, tôi là ChatGPT", lines=2)
55
- inputs=gr.Audio(type="filepath"), # Input as audio, no need for 'source' argument
56
- # Button để ghi âm
57
- record_button = gr.Button("Ghi âm kiểm tra")
58
-
59
  # Output hiển thị kết quả
60
  transcribed_text = gr.Textbox(label="Văn bản bạn nói")
61
  accuracy = gr.Textbox(label="Độ chính xác (%)")
62
  incorrect_words = gr.Textbox(label="Những từ nói sai")
63
 
64
  # Nút ghi âm được kết nối với chức năng xử lý
65
- record_button.click(fn=process_speech, inputs=[reference_text], outputs=[transcribed_text, accuracy, incorrect_words])
 
 
 
 
66
 
67
  return demo
68
 
 
2
  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
10
+ with sr.AudioFile(audio_file) as source:
11
  # Điều chỉnh tiếng ồn nền và ghi âm
12
  recognizer.adjust_for_ambient_noise(source, duration=1)
13
+ audio = recognizer.record(source)
14
 
15
  try:
16
  # Chuyển giọng nói thành văn bản
 
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
+ transcribed_text = transcribe_speech(audio_file)
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
 
53
  with gr.Blocks() as demo:
54
  # Input cho văn bản mẫu
55
  reference_text = gr.Textbox(label="Văn bản mẫu", value="Xin chào, tôi là ChatGPT", lines=2)
56
+
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 ghi âm được kết nối với chức năng xử lý
66
+ gr.Button("Ghi âm kiểm tra").click(
67
+ fn=process_speech,
68
+ inputs=[reference_text, audio_input],
69
+ outputs=[transcribed_text, accuracy, incorrect_words]
70
+ )
71
 
72
  return demo
73