Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
|
4 |
+
def click_js():
|
5 |
+
return """function audioRecord() {
|
6 |
+
var xPathRes = document.evaluate ('//*[contains(@class, "record")]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
|
7 |
+
xPathRes.singleNodeValue.click();}"""
|
8 |
+
|
9 |
+
|
10 |
+
def action(btn):
|
11 |
+
"""Changes button text on click"""
|
12 |
+
if btn == 'Speak': return 'Stop'
|
13 |
+
else: return 'Speak'
|
14 |
+
|
15 |
+
|
16 |
+
def check_btn(btn):
|
17 |
+
"""Checks for correct button text before invoking transcribe()"""
|
18 |
+
if btn != 'Speak': raise Exception('Recording...')
|
19 |
+
|
20 |
+
|
21 |
+
def transcribe():
|
22 |
+
return 'Success'
|
23 |
+
|
24 |
+
with gr.Blocks() as demo:
|
25 |
+
msg = gr.Textbox()
|
26 |
+
audio_box = gr.Audio(label="Audio", sources="microphone", type="filepath", elem_id='audio')
|
27 |
+
|
28 |
+
with gr.Row():
|
29 |
+
audio_btn = gr.Button('Speak')
|
30 |
+
clear = gr.Button("Clear")
|
31 |
+
|
32 |
+
audio_btn.click(fn=action, inputs=audio_btn, outputs=audio_btn).\
|
33 |
+
then(fn=lambda: None, js=click_js()).\
|
34 |
+
then(fn=check_btn, inputs=audio_btn).\
|
35 |
+
success(fn=transcribe, outputs=msg)
|
36 |
+
|
37 |
+
clear.click(lambda: None, None, msg, queue=False)
|
38 |
+
|
39 |
+
demo.queue().launch(debug=True)
|