Ra-Is commited on
Commit
d6c20cc
1 Parent(s): f6c1536

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -32
app.py CHANGED
@@ -1,33 +1,40 @@
1
- import requests
2
  import gradio as gr
3
- import os
4
-
5
- def send_audio(audio):
6
- url = os.getenv("BASE_URL")
7
-
8
- # Save audio to a temporary file
9
- temp_audio_path = "temp_audio.wav"
10
- with open(temp_audio_path, 'wb') as f:
11
- f.write(audio.read())
12
-
13
- # Prepare form data for request
14
- files = {
15
- 'file': open(temp_audio_path, 'rb'),
16
- }
17
- data = {
18
- 'lang': 'english-twi'
19
- }
20
-
21
- response = requests.post(url, files=files, data=data)
22
- return response.json()
23
-
24
- # Gradio interface for recording speech
25
- iface = gr.Interface(
26
- fn=send_audio,
27
- inputs=gr.Audio(source="microphone", type="file"),
28
- outputs="text",
29
- title="Speech Translation",
30
- description="Record speech and send for processing"
31
- )
32
-
33
- iface.launch()
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+
3
+
4
+ def click_js():
5
+ return """function audioRecord() {
6
+ var xPathRes = document.evaluate ('//*[@id="audio"]//button', 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
+
25
+ with gr.Blocks() as demo:
26
+ msg = gr.Textbox()
27
+ audio_box = gr.Audio(label="Audio", source="microphone", type="filepath", elem_id='audio')
28
+
29
+ with gr.Row():
30
+ audio_btn = gr.Button('Speak')
31
+ clear = gr.Button("Clear")
32
+
33
+ audio_btn.click(fn=action, inputs=audio_btn, outputs=audio_btn).\
34
+ then(fn=lambda: None, _js=click_js()).\
35
+ then(fn=check_btn, inputs=audio_btn).\
36
+ success(fn=transcribe, outputs=msg)
37
+
38
+ clear.click(lambda: None, None, msg, queue=False)
39
+
40
+ demo.queue().launch(debug=True)