Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,142 +1,126 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
return
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
total_process_time = end_total_time - start_total_time
|
128 |
-
result += f"\nTổng thời gian hoàn thành tất cả các chức năng: {total_process_time:.2f} giây"
|
129 |
-
return result
|
130 |
-
|
131 |
-
# Tạo giao diện với Gradio
|
132 |
-
gr.Interface(
|
133 |
-
fn=gradio_interface,
|
134 |
-
inputs=[
|
135 |
-
gr.Textbox(label="URL Truyện", placeholder="Nhập URL của truyện từ truyenfull.tv"),
|
136 |
-
gr.Textbox(label="Số chương bắt đầu", placeholder="Nhập số chương bắt đầu"),
|
137 |
-
gr.Textbox(label="Số chương muốn tải", placeholder="Nhập số chương muốn tải")
|
138 |
-
],
|
139 |
-
outputs=["text","file"],
|
140 |
-
title="Truyện Full Downloader",
|
141 |
-
description="Công cụ tải truyện từ truyenfull.tv và tạo file EPUB."
|
142 |
-
).launch()
|
|
|
1 |
+
import speech_recognition as sr
|
2 |
+
import difflib
|
3 |
+
import wave
|
4 |
+
import pyaudio
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# Step 1: Record audio
|
8 |
+
def record_audio(filename):
|
9 |
+
chunk = 1024 # Record in chunks of 1024 samples
|
10 |
+
sample_format = pyaudio.paInt16 # 16 bits per sample
|
11 |
+
channels = 1
|
12 |
+
fs = 44100 # Record at 44100 samples per second
|
13 |
+
seconds = 10 # Length of recording
|
14 |
+
|
15 |
+
p = pyaudio.PyAudio() # Create an interface to PortAudio
|
16 |
+
|
17 |
+
print("Recording...")
|
18 |
+
stream = p.open(format=sample_format,
|
19 |
+
channels=channels,
|
20 |
+
rate=fs,
|
21 |
+
frames_per_buffer=chunk,
|
22 |
+
input=True)
|
23 |
+
|
24 |
+
frames = [] # Initialize array to store frames
|
25 |
+
|
26 |
+
# Store data in chunks for the specified duration
|
27 |
+
for _ in range(0, int(fs / chunk * seconds)):
|
28 |
+
data = stream.read(chunk)
|
29 |
+
frames.append(data)
|
30 |
+
|
31 |
+
# Stop and close the stream
|
32 |
+
stream.stop_stream()
|
33 |
+
stream.close()
|
34 |
+
p.terminate()
|
35 |
+
|
36 |
+
# Save the recorded audio as a WAV file
|
37 |
+
wf = wave.open(filename, 'wb')
|
38 |
+
wf.setnchannels(channels)
|
39 |
+
wf.setsampwidth(p.get_sample_size(sample_format))
|
40 |
+
wf.setframerate(fs)
|
41 |
+
wf.writeframes(b''.join(frames))
|
42 |
+
wf.close()
|
43 |
+
|
44 |
+
print("Recording completed.")
|
45 |
+
|
46 |
+
# Step 2: Transcribe the audio file
|
47 |
+
def transcribe_audio(filename):
|
48 |
+
recognizer = sr.Recognizer()
|
49 |
+
|
50 |
+
# Open the audio file for transcription
|
51 |
+
with sr.AudioFile(filename) as source:
|
52 |
+
audio = recognizer.record(source)
|
53 |
+
try:
|
54 |
+
# Recognize the audio using Google Web Speech API
|
55 |
+
print("Transcribing the audio...")
|
56 |
+
transcription = recognizer.recognize_google(audio)
|
57 |
+
print("Transcription completed.")
|
58 |
+
return transcription
|
59 |
+
except sr.UnknownValueError:
|
60 |
+
print("Google Speech Recognition could not understand the audio")
|
61 |
+
return ""
|
62 |
+
except sr.RequestError as e:
|
63 |
+
print(f"Error with Google Speech Recognition service: {e}")
|
64 |
+
return ""
|
65 |
+
|
66 |
+
# Step 3: Compare the transcribed text with the input paragraph
|
67 |
+
def compare_texts(reference_text, transcribed_text):
|
68 |
+
word_scores = []
|
69 |
+
reference_words = reference_text.split()
|
70 |
+
transcribed_words = transcribed_text.split()
|
71 |
+
|
72 |
+
sm = difflib.SequenceMatcher(None, reference_text, transcribed_text)
|
73 |
+
similarity_score = round(sm.ratio() * 100, 2)
|
74 |
+
|
75 |
+
for i, word in enumerate(reference_words):
|
76 |
+
try:
|
77 |
+
if word.lower() == transcribed_words[i].lower():
|
78 |
+
word_scores.append({"word": word, "quality_score": 100})
|
79 |
+
else:
|
80 |
+
word_scores.append({"word": word, "quality_score": 50}) # Assuming 50 if it's wrong
|
81 |
+
except IndexError:
|
82 |
+
word_scores.append({"word": word, "quality_score": 0})
|
83 |
+
|
84 |
+
fidelity_class = "CORRECT" if similarity_score > 50 else "INCORRECT"
|
85 |
+
|
86 |
+
output = {
|
87 |
+
"quota_remaining": -1,
|
88 |
+
"reference_text_from_application": reference_text,
|
89 |
+
"status": "success",
|
90 |
+
"text_score": {
|
91 |
+
"fidelity_class": fidelity_class,
|
92 |
+
"quality_score": similarity_score,
|
93 |
+
"text": reference_text,
|
94 |
+
"transcribedText": transcribed_text,
|
95 |
+
"word_score_list": word_scores
|
96 |
+
},
|
97 |
+
"version": "1.1"
|
98 |
+
}
|
99 |
+
|
100 |
+
return output
|
101 |
+
|
102 |
+
# Gradio Interface Function
|
103 |
+
def gradio_function(paragraph):
|
104 |
+
# Record the audio (the filename will be 'recorded_audio.wav')
|
105 |
+
record_audio("recorded_audio.wav")
|
106 |
+
|
107 |
+
# Transcribe the audio
|
108 |
+
transcribed_text = transcribe_audio("recorded_audio.wav")
|
109 |
+
|
110 |
+
# Compare the original paragraph with the transcribed text
|
111 |
+
comparison_result = compare_texts(paragraph, transcribed_text)
|
112 |
+
|
113 |
+
# Return comparison result
|
114 |
+
return comparison_result
|
115 |
+
|
116 |
+
# Gradio Interface
|
117 |
+
interface = gr.Interface(
|
118 |
+
fn=gradio_function,
|
119 |
+
inputs=gr.inputs.Textbox(lines=5, label="Input Paragraph"),
|
120 |
+
outputs="json",
|
121 |
+
title="Speech Recognition Comparison",
|
122 |
+
description="Input a paragraph, record your audio, and compare the transcription to the original text."
|
123 |
+
)
|
124 |
+
|
125 |
+
# Launch Gradio app
|
126 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|