Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,29 @@
|
|
1 |
import speech_recognition as sr
|
2 |
import difflib
|
3 |
-
import wave
|
4 |
-
import pyaudio
|
5 |
import gradio as gr
|
6 |
|
7 |
-
# Step 1:
|
8 |
-
def
|
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 |
-
#
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
67 |
def compare_texts(reference_text, transcribed_text):
|
68 |
word_scores = []
|
69 |
reference_words = reference_text.split()
|
@@ -100,12 +60,9 @@ def compare_texts(reference_text, transcribed_text):
|
|
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(
|
109 |
|
110 |
# Compare the original paragraph with the transcribed text
|
111 |
comparison_result = compare_texts(paragraph, transcribed_text)
|
@@ -116,7 +73,10 @@ def gradio_function(paragraph):
|
|
116 |
# Gradio Interface
|
117 |
interface = gr.Interface(
|
118 |
fn=gradio_function,
|
119 |
-
inputs=
|
|
|
|
|
|
|
120 |
outputs="json",
|
121 |
title="Speech Recognition Comparison",
|
122 |
description="Input a paragraph, record your audio, and compare the transcription to the original text."
|
|
|
1 |
import speech_recognition as sr
|
2 |
import difflib
|
|
|
|
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Step 1: Transcribe the audio file
|
6 |
+
def transcribe_audio(audio):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
recognizer = sr.Recognizer()
|
8 |
|
9 |
+
# Convert audio into recognizable format for the Recognizer
|
10 |
+
audio_file = sr.AudioFile(audio.name)
|
11 |
+
|
12 |
+
with audio_file as source:
|
13 |
+
audio_data = recognizer.record(source)
|
14 |
+
|
15 |
+
try:
|
16 |
+
# Recognize the audio using Google Web Speech API
|
17 |
+
print("Transcribing the audio...")
|
18 |
+
transcription = recognizer.recognize_google(audio_data)
|
19 |
+
print("Transcription completed.")
|
20 |
+
return transcription
|
21 |
+
except sr.UnknownValueError:
|
22 |
+
return "Google Speech Recognition could not understand the audio"
|
23 |
+
except sr.RequestError as e:
|
24 |
+
return f"Error with Google Speech Recognition service: {e}"
|
25 |
+
|
26 |
+
# Step 2: Compare the transcribed text with the input paragraph
|
27 |
def compare_texts(reference_text, transcribed_text):
|
28 |
word_scores = []
|
29 |
reference_words = reference_text.split()
|
|
|
60 |
return output
|
61 |
|
62 |
# Gradio Interface Function
|
63 |
+
def gradio_function(paragraph, audio):
|
|
|
|
|
|
|
64 |
# Transcribe the audio
|
65 |
+
transcribed_text = transcribe_audio(audio)
|
66 |
|
67 |
# Compare the original paragraph with the transcribed text
|
68 |
comparison_result = compare_texts(paragraph, transcribed_text)
|
|
|
73 |
# Gradio Interface
|
74 |
interface = gr.Interface(
|
75 |
fn=gradio_function,
|
76 |
+
inputs=[
|
77 |
+
gr.inputs.Textbox(lines=5, label="Input Paragraph"),
|
78 |
+
gr.inputs.Audio(source="microphone", type="file", label="Record Audio")
|
79 |
+
],
|
80 |
outputs="json",
|
81 |
title="Speech Recognition Comparison",
|
82 |
description="Input a paragraph, record your audio, and compare the transcription to the original text."
|