Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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
|
16 |
+
text = recognizer.recognize_google(audio, language="vi-VN")
|
17 |
+
return text
|
18 |
+
except sr.UnknownValueError:
|
19 |
+
return "Không thể nhận diện giọng nói"
|
20 |
+
except sr.RequestError as e:
|
21 |
+
return f"Lỗi kết nối dịch vụ Google: {e}"
|
22 |
+
|
23 |
+
# Hàm so sánh văn bản chuyển đổi với văn bản mẫu
|
24 |
+
def compare_transcription(transcribed_text, reference_text):
|
25 |
+
transcribed_words = transcribed_text.split()
|
26 |
+
reference_words = reference_text.split()
|
27 |
+
|
28 |
+
incorrect_words = []
|
29 |
+
|
30 |
+
for i, word in enumerate(transcribed_words):
|
31 |
+
if i >= len(reference_words) or word.lower() != reference_words[i].lower():
|
32 |
+
incorrect_words.append(word)
|
33 |
+
|
34 |
+
matches = difflib.SequenceMatcher(None, transcribed_words, reference_words)
|
35 |
+
accuracy = matches.ratio() * 100
|
36 |
+
|
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
|
45 |
+
|
46 |
+
accuracy, incorrect_words = compare_transcription(transcribed_text, reference_text)
|
47 |
+
|
48 |
+
return transcribed_text, f"{accuracy:.2f}%", ", ".join(incorrect_words) if incorrect_words else "Không có từ sai"
|
49 |
+
|
50 |
+
# Tạo giao diện với Gradio
|
51 |
+
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 |
+
|
56 |
+
# Button để ghi âm
|
57 |
+
record_button = gr.Button("Ghi âm và 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 |
+
|
69 |
+
# Chạy giao diện
|
70 |
+
demo = build_interface()
|
71 |
+
demo.launch()
|