Spaces:
Running
Running
alex buz
commited on
Commit
•
da77537
1
Parent(s):
976c1d9
new
Browse files- app copy 2.py +57 -0
- app copy 3.py +53 -0
- app copy 4.py +64 -0
- app copy 5.py +59 -0
- app copy 6.py +47 -0
- app copy.py +56 -0
- app.py +21 -0
- push.bat +3 -0
app copy 2.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import speech_recognition as sr
|
3 |
+
import os
|
4 |
+
|
5 |
+
def transcribe_audio(file_path):
|
6 |
+
"""Transcribes audio to text using the speech_recognition library."""
|
7 |
+
recognizer = sr.Recognizer()
|
8 |
+
with sr.AudioFile(file_path) as source:
|
9 |
+
audio_data = recognizer.record(source)
|
10 |
+
try:
|
11 |
+
text = recognizer.recognize_google(audio_data)
|
12 |
+
return text
|
13 |
+
except sr.UnknownValueError:
|
14 |
+
return "Google Speech Recognition could not understand audio"
|
15 |
+
except sr.RequestError as e:
|
16 |
+
return f"Could not request results from Google Speech Recognition service; {e}"
|
17 |
+
|
18 |
+
def handle_transcription(file_info):
|
19 |
+
"""Handle transcription after recording."""
|
20 |
+
if file_info is None:
|
21 |
+
return f" 111 No audio recorded or file not found: {file_info}"
|
22 |
+
print (file_info)
|
23 |
+
file_path = file_info
|
24 |
+
if os.path.exists(file_path):
|
25 |
+
return transcribe_audio(file_path)
|
26 |
+
return f"222 No audio recorded or file not found: {file_info}"
|
27 |
+
|
28 |
+
with gr.Blocks() as demo:
|
29 |
+
gr.Markdown("### Voice Recorder and Transcriber")
|
30 |
+
audio_box = gr.Audio(label="Record Audio", sources="microphone", type="filepath", elem_id='audio')
|
31 |
+
|
32 |
+
with gr.Row():
|
33 |
+
record_btn = gr.Button('Record/Stop')
|
34 |
+
transcribe_btn = gr.Button('Transcribe')
|
35 |
+
|
36 |
+
output_text = gr.Textbox(label="Transcription Output")
|
37 |
+
|
38 |
+
def manage_record(recording_state):
|
39 |
+
"""Toggle recording and manage UI updates."""
|
40 |
+
return not recording_state, "Stop" if not recording_state else "Record"
|
41 |
+
|
42 |
+
state = gr.State(False) # False indicates not recording, True indicates recording
|
43 |
+
|
44 |
+
record_btn.click(
|
45 |
+
fn=manage_record,
|
46 |
+
inputs=state,
|
47 |
+
outputs=[state, record_btn],
|
48 |
+
js="document.getElementById('audio').value = null; document.getElementById('audio').click();"
|
49 |
+
)
|
50 |
+
|
51 |
+
transcribe_btn.click(
|
52 |
+
fn=handle_transcription,
|
53 |
+
inputs=audio_box,
|
54 |
+
outputs=output_text
|
55 |
+
)
|
56 |
+
|
57 |
+
demo.launch(debug=True)
|
app copy 3.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import speech_recognition as sr
|
3 |
+
import os
|
4 |
+
|
5 |
+
def transcribe_audio(file_path):
|
6 |
+
"""Transcribes audio to text using the speech_recognition library."""
|
7 |
+
recognizer = sr.Recognizer()
|
8 |
+
with sr.AudioFile(file_path) as source:
|
9 |
+
audio_data = recognizer.record(source)
|
10 |
+
try:
|
11 |
+
text = recognizer.recognize_google(audio_data)
|
12 |
+
return text
|
13 |
+
except sr.UnknownValueError:
|
14 |
+
return "Google Speech Recognition could not understand audio"
|
15 |
+
except sr.RequestError as e:
|
16 |
+
return f"Could not request results from Google Speech Recognition service; {e}"
|
17 |
+
|
18 |
+
def handle_transcription(file_info):
|
19 |
+
"""Handle transcription after recording."""
|
20 |
+
if file_info is None:
|
21 |
+
return "No audio recorded or file not found."
|
22 |
+
file_path = file_info
|
23 |
+
if os.path.exists(file_path):
|
24 |
+
return transcribe_audio(file_path)
|
25 |
+
return "No audio recorded or file not found."
|
26 |
+
|
27 |
+
with gr.Blocks() as demo:
|
28 |
+
gr.Markdown("### Voice Recorder and Transcriber")
|
29 |
+
audio_box = gr.Audio(label="Record Audio", type="filepath", elem_id='audio')
|
30 |
+
|
31 |
+
with gr.Row():
|
32 |
+
record_btn = gr.Button('Record')
|
33 |
+
transcribe_btn = gr.Button('Transcribe')
|
34 |
+
|
35 |
+
output_text = gr.Textbox(label="Transcription Output")
|
36 |
+
|
37 |
+
def toggle_record(button_text):
|
38 |
+
"""Toggle the button text and manage the recording."""
|
39 |
+
return "Stop" if button_text == "Record" else "Record"
|
40 |
+
|
41 |
+
record_btn.click(
|
42 |
+
fn=toggle_record,
|
43 |
+
inputs=record_btn,
|
44 |
+
outputs=record_btn
|
45 |
+
)
|
46 |
+
|
47 |
+
transcribe_btn.click(
|
48 |
+
fn=handle_transcription,
|
49 |
+
inputs=audio_box,
|
50 |
+
outputs=output_text
|
51 |
+
)
|
52 |
+
|
53 |
+
demo.launch(debug=True)
|
app copy 4.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import speech_recognition as sr
|
3 |
+
import os
|
4 |
+
|
5 |
+
def transcribe_audio(file_path):
|
6 |
+
"""Transcribes audio to text using the speech_recognition library."""
|
7 |
+
recognizer = sr.Recognizer()
|
8 |
+
with sr.AudioFile(file_path) as source:
|
9 |
+
audio_data = recognizer.record(source)
|
10 |
+
try:
|
11 |
+
text = recognizer.recognize_google(audio_data)
|
12 |
+
return text
|
13 |
+
except sr.UnknownValueError:
|
14 |
+
return "Google Speech Recognition could not understand audio"
|
15 |
+
except sr.RequestError as e:
|
16 |
+
return f"Could not request results from Google Speech Recognition service; {e}"
|
17 |
+
|
18 |
+
def handle_transcription(file_info):
|
19 |
+
"""Handle transcription after recording."""
|
20 |
+
if file_info is None:
|
21 |
+
return "No audio recorded or file not found."
|
22 |
+
file_path = file_info
|
23 |
+
if os.path.exists(file_path):
|
24 |
+
return transcribe_audio(file_path)
|
25 |
+
return "No audio recorded or file not found."
|
26 |
+
|
27 |
+
with gr.Blocks() as demo:
|
28 |
+
gr.Markdown("### Voice Recorder and Transcriber")
|
29 |
+
audio_box = gr.Audio(label="Record Audio", sources="microphone", type="filepath", elem_id='audio')
|
30 |
+
|
31 |
+
with gr.Row():
|
32 |
+
record_btn = gr.Button('Record')
|
33 |
+
transcribe_btn = gr.Button('Transcribe')
|
34 |
+
|
35 |
+
output_text = gr.Textbox(label="Transcription Output")
|
36 |
+
|
37 |
+
def create_toggle_record(record_btn):
|
38 |
+
print(111)
|
39 |
+
def toggle_record( button_text):
|
40 |
+
if button_text == "Record":
|
41 |
+
print(222)
|
42 |
+
print(audio_box)
|
43 |
+
audio_box.start_recording()
|
44 |
+
return "Stop" # Return new button text (Stop)
|
45 |
+
else:
|
46 |
+
audio_box.stop_recording()
|
47 |
+
return "Record" # Return new button text (Record)
|
48 |
+
return toggle_record
|
49 |
+
|
50 |
+
# Create the closure and connect it to the button click
|
51 |
+
toggle_record_fn = create_toggle_record(record_btn)
|
52 |
+
record_btn.click(
|
53 |
+
fn=toggle_record_fn,
|
54 |
+
inputs=[record_btn], # Pass only the button (no need for text)
|
55 |
+
outputs=record_btn
|
56 |
+
)
|
57 |
+
|
58 |
+
transcribe_btn.click(
|
59 |
+
fn=handle_transcription,
|
60 |
+
inputs=audio_box,
|
61 |
+
outputs=output_text
|
62 |
+
)
|
63 |
+
|
64 |
+
demo.launch(debug=True)
|
app copy 5.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import time
|
3 |
+
import speech_recognition as sr
|
4 |
+
|
5 |
+
def transcribe(audio):
|
6 |
+
if audio is None:
|
7 |
+
return "No audio recorded."
|
8 |
+
|
9 |
+
recognizer = sr.Recognizer()
|
10 |
+
with sr.AudioFile(audio) as source:
|
11 |
+
audio_data = recognizer.record(source)
|
12 |
+
try:
|
13 |
+
text = recognizer.recognize_google(audio_data)
|
14 |
+
return text
|
15 |
+
except sr.UnknownValueError:
|
16 |
+
return "Google Speech Recognition could not understand audio"
|
17 |
+
except sr.RequestError as e:
|
18 |
+
return f"Could not request results from Google Speech Recognition service; {e}"
|
19 |
+
|
20 |
+
def toggle_recording(audio, state):
|
21 |
+
if state == "Idle":
|
22 |
+
return None, "Recording", "Recording... Click 'Stop' when finished."
|
23 |
+
else:
|
24 |
+
time.sleep(1) # Small delay to ensure audio is processed
|
25 |
+
if audio is not None:
|
26 |
+
transcription = transcribe(audio)
|
27 |
+
return None, "Idle", transcription
|
28 |
+
else:
|
29 |
+
return None, "Idle", "No audio recorded."
|
30 |
+
|
31 |
+
with gr.Blocks() as demo:
|
32 |
+
audio = gr.Audio(sources="microphone", type="filepath", elem_id="audio-component")
|
33 |
+
button = gr.Button("Record", elem_id="record-button")
|
34 |
+
state = gr.State("Idle")
|
35 |
+
output = gr.Textbox(label="Transcription")
|
36 |
+
|
37 |
+
button.click(
|
38 |
+
fn=toggle_recording,
|
39 |
+
inputs=[audio, state],
|
40 |
+
outputs=[audio, state, output],
|
41 |
+
js="""
|
42 |
+
async (audio, state) => {
|
43 |
+
const audioEl = document.querySelector('#audio-component audio');
|
44 |
+
const recordButton = document.querySelector('#record-button');
|
45 |
+
|
46 |
+
if (state === "Idle") {
|
47 |
+
await audio.startRecording();
|
48 |
+
recordButton.textContent = "Stop";
|
49 |
+
return [null, "Recording", "Recording... Click 'Stop' when finished."];
|
50 |
+
} else {
|
51 |
+
await audio.stopRecording();
|
52 |
+
recordButton.textContent = "Record";
|
53 |
+
return [await audio.getValue(), "Idle", "Processing..."];
|
54 |
+
}
|
55 |
+
}
|
56 |
+
"""
|
57 |
+
)
|
58 |
+
|
59 |
+
demo.queue().launch(debug=True)
|
app copy 6.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
def click_js():
|
4 |
+
return """
|
5 |
+
function(audio_btn, update_status) {
|
6 |
+
const recordBtn = document.querySelector('#audio button');
|
7 |
+
if (audio_btn == 'Speak') {
|
8 |
+
recordBtn.click(); // Start recording
|
9 |
+
update_status('Stop'); // Update the button to show 'Stop'
|
10 |
+
return 'Recording...';
|
11 |
+
} else {
|
12 |
+
recordBtn.click(); // Stop recording
|
13 |
+
update_status('Speak'); // Reset button text
|
14 |
+
return new Promise(resolve => {
|
15 |
+
setTimeout(() => { // Wait a small delay to ensure recording has stopped
|
16 |
+
resolve('Done recording');
|
17 |
+
}, 500);
|
18 |
+
});
|
19 |
+
}
|
20 |
+
}
|
21 |
+
"""
|
22 |
+
|
23 |
+
def transcribe(recording_status):
|
24 |
+
if recording_status == 'Done recording':
|
25 |
+
print('Transcribing...')
|
26 |
+
return 'Success'
|
27 |
+
else:
|
28 |
+
return recording_status
|
29 |
+
|
30 |
+
with gr.Blocks() as demo:
|
31 |
+
msg = gr.Textbox()
|
32 |
+
audio_box = gr.Audio(label="Audio", sources="microphone", type="filepath", elem_id='audio')
|
33 |
+
|
34 |
+
with gr.Row():
|
35 |
+
audio_btn = gr.Button('Speak')
|
36 |
+
clear = gr.Button("Clear")
|
37 |
+
|
38 |
+
audio_btn.click(
|
39 |
+
js=click_js(),
|
40 |
+
inputs=[audio_btn],
|
41 |
+
outputs=[audio_btn, msg],
|
42 |
+
fn=transcribe
|
43 |
+
)
|
44 |
+
|
45 |
+
clear.click(lambda: "", inputs=None, outputs=msg, queue=False)
|
46 |
+
|
47 |
+
demo.launch(debug=True)
|
app copy.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import speech_recognition as sr
|
3 |
+
from pprint import pprint as pp
|
4 |
+
import os
|
5 |
+
|
6 |
+
def transcribe_audio(file_path):
|
7 |
+
"""Transcribes audio to text using the speech_recognition library."""
|
8 |
+
recognizer = sr.Recognizer()
|
9 |
+
with sr.AudioFile(file_path) as source:
|
10 |
+
audio_data = recognizer.record(source)
|
11 |
+
try:
|
12 |
+
# Using Google's speech recognition service. Note: It requires internet.
|
13 |
+
text = recognizer.recognize_google(audio_data)
|
14 |
+
return text
|
15 |
+
except sr.UnknownValueError:
|
16 |
+
return "Google Speech Recognition could not understand audio"
|
17 |
+
except sr.RequestError as e:
|
18 |
+
return f"Could not request results from Google Speech Recognition service; {e}"
|
19 |
+
|
20 |
+
def manage_record():
|
21 |
+
"""Toggle recording and manage UI updates."""
|
22 |
+
js_code = """
|
23 |
+
const btn = document.getElementById('record_btn');
|
24 |
+
const recordingText = 'Stop';
|
25 |
+
const idleText = 'Record';
|
26 |
+
if (btn.textContent.includes(idleText)) {
|
27 |
+
btn.textContent = recordingText;
|
28 |
+
} else {
|
29 |
+
btn.textContent = idleText;
|
30 |
+
}
|
31 |
+
"""
|
32 |
+
return gr.update(js=js_code)
|
33 |
+
|
34 |
+
def handle_transcription(file_info):
|
35 |
+
"""Handle transcription after recording."""
|
36 |
+
print(file_info)
|
37 |
+
file_path = file_info
|
38 |
+
print(file_path )
|
39 |
+
if os.path.exists(file_path):
|
40 |
+
return transcribe_audio(file_path)
|
41 |
+
return "No audio recorded or file not found."
|
42 |
+
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown("### Voice Recorder and Transcriber")
|
45 |
+
audio_box = gr.Audio(label="Record Audio", sources="microphone", type="filepath", elem_id='audio')
|
46 |
+
|
47 |
+
with gr.Row():
|
48 |
+
record_btn = gr.Button('Record/Stop', elem_id='record_btn')
|
49 |
+
transcribe_btn = gr.Button('Transcribe')
|
50 |
+
|
51 |
+
output_text = gr.Textbox(label="Transcription Output")
|
52 |
+
|
53 |
+
record_btn.click(fn=manage_record)
|
54 |
+
transcribe_btn.click(fn=handle_transcription, inputs=audio_box, outputs=output_text)
|
55 |
+
|
56 |
+
demo.launch(debug=True)
|
app.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
|
6 |
+
|
7 |
+
def transcribe(audio):
|
8 |
+
sr, y = audio
|
9 |
+
y = y.astype(np.float32)
|
10 |
+
y /= np.max(np.abs(y))
|
11 |
+
|
12 |
+
return transcriber({"sampling_rate": sr, "raw": y})["text"]
|
13 |
+
|
14 |
+
|
15 |
+
demo = gr.Interface(
|
16 |
+
transcribe,
|
17 |
+
gr.Audio(sources=["microphone"]),
|
18 |
+
"text",
|
19 |
+
)
|
20 |
+
|
21 |
+
demo.launch()
|
push.bat
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
git add .
|
2 |
+
git commit -m "%1"
|
3 |
+
git push
|