Tonic commited on
Commit
3eefcff
1 Parent(s): e4eae4e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torchaudio
3
+ import torch
4
+
5
+
6
+ def speech_to_text(audio_file):
7
+ audio_input, _ = torchaudio.load(audio_file.name)
8
+ s2t_model = torch.jit.load("unity_on_device_s2t.ptl")
9
+ with torch.no_grad():
10
+ text = s2t_model(audio_input, tgt_lang=TGT_LANG)
11
+ return text
12
+
13
+ def speech_to_speech_translation(audio_file):
14
+ audio_input, _ = torchaudio.load(audio_file.name)
15
+ s2st_model = torch.jit.load("unity_on_device.ptl")
16
+ with torch.no_grad():
17
+ text, units, waveform = s2st_model(audio_input, tgt_lang=TGT_LANG)
18
+ output_file = "/tmp/result.wav"
19
+ torchaudio.save(output_file, waveform.unsqueeze(0), sample_rate=16000)
20
+ return text, output_file
21
+
22
+ # Gradio interfaces
23
+ iface_s2t = gr.Interface(
24
+ fn=speech_to_text,
25
+ inputs=gr.inputs.Audio(type="file", label="Upload Audio for Speech to Text"),
26
+ outputs="text",
27
+ title="Speech to Text"
28
+ )
29
+
30
+ iface_s2st = gr.Interface(
31
+ fn=speech_to_speech_translation,
32
+ inputs=gr.inputs.Audio(type="file", label="Upload Audio for Speech to Speech Translation"),
33
+ outputs=["text", "audio"],
34
+ title="Speech to Speech Translation"
35
+ )
36
+
37
+ # Combine into a tabbed interface
38
+ iface = gr.TabbedInterface([iface_s2t, iface_s2st], ["Speech to Text", "Speech to Speech Translation"])
39
+ iface.launch()