Spaces:
Runtime error
Runtime error
reallynicejam
commited on
Commit
•
b1877e1
1
Parent(s):
844c0df
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,40 @@
|
|
1 |
import gradio as gr
|
2 |
-
import requests
|
3 |
-
from io import BytesIO
|
4 |
-
from pydub import AudioSegment
|
5 |
-
|
6 |
-
def translate_audio_to_audio(audio):
|
7 |
-
# Send the audio data to the translation model
|
8 |
-
translation_response = requests.post(
|
9 |
-
"https://api-inference.huggingface.co/models/facebook/xm_transformer_s2ut_hk-en",
|
10 |
-
files={"file": audio},
|
11 |
-
)
|
12 |
-
|
13 |
-
# Get the translated audio from the response
|
14 |
-
translated_audio_bytes = translation_response.content
|
15 |
-
translated_audio = AudioSegment.from_file(BytesIO(translated_audio_bytes))
|
16 |
-
|
17 |
-
return translated_audio
|
18 |
-
|
19 |
-
gr.Interface(
|
20 |
-
fn=translate_audio_to_audio,
|
21 |
-
inputs=gr.Audio(source="microphone", type="file", label="Speak something"),
|
22 |
-
outputs=gr.Audio(type="file", label="Translated Audio"),
|
23 |
-
live=True
|
24 |
-
).launch()
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|