Tonic commited on
Commit
e667211
1 Parent(s): 737d2c6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torchaudio
3
+ import torch
4
+ import os
5
+ import time
6
+ import soundfile as sf
7
+
8
+ welcome_message = """
9
+ # Welcome to Tonic's Unity On Device!
10
+
11
+ Tonic's Unity On Device uses [facebook/seamless-m4t-unity-small](https://huggingface.co/facebook/seamless-m4t-unity-small) for audio translation & accessibility.
12
+ Tonic's Unity On Device!🚀 on your own data & in your own way by cloning this space. Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/TeamTonic/SeamlessOnDevice?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
13
+ ### Join us :
14
+ TeamTonic is always making cool demos! Join our active builder's community on Discord: [Discord](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Polytonic](https://github.com/tonic-ai) & contribute to 🌟 [PolyGPT](https://github.com/tonic-ai/polygpt-alpha)"
15
+ """
16
+
17
+ # Define the list of target languages
18
+ languages = {
19
+ "English": "eng",
20
+ "Hindi": "hin",
21
+ "Portuguese": "por",
22
+ "Russian": "rus",
23
+ "Spanish": "spa"
24
+ }
25
+
26
+ def save_audio(audio_input, output_dir="saved_audio"):
27
+ if not os.path.exists(output_dir):
28
+ os.makedirs(output_dir)
29
+
30
+ # Extract sample rate and audio data
31
+ sample_rate, audio_data = audio_input
32
+
33
+ # Generate a unique file name
34
+ file_name = f"audio_{int(time.time())}.wav"
35
+ file_path = os.path.join(output_dir, file_name)
36
+
37
+ # Save the audio file
38
+ sf.write(file_path, audio_data, sample_rate)
39
+
40
+ return file_path
41
+
42
+ def speech_to_text(audio_data, tgt_lang):
43
+ file_path = save_audio(audio_data)
44
+ audio_input, _ = torchaudio.load(file_path)
45
+ s2t_model = torch.jit.load("unity_on_device_s2t.ptl")
46
+ with torch.no_grad():
47
+ text = s2t_model(audio_input, tgt_lang=languages[tgt_lang])
48
+ return text
49
+
50
+ def speech_to_speech_translation(audio_data, tgt_lang):
51
+ file_path = save_audio(audio_data)
52
+ audio_input, _ = torchaudio.load(file_path)
53
+ s2st_model = torch.jit.load("unity_on_device.ptl")
54
+ with torch.no_grad():
55
+ text, units, waveform = s2st_model(audio_input, tgt_lang=languages[tgt_lang])
56
+ output_file = "/tmp/result.wav"
57
+ torchaudio.save(output_file, waveform.unsqueeze(0), sample_rate=16000)
58
+ return text, output_file
59
+
60
+ def create_interface():
61
+ with gr.Markdown(welcome_message)
62
+ with gr.Blocks(theme='ParityError/Anime') as interface:
63
+ # Dropdown for language selection
64
+ input_language = gr.Dropdown(list(languages.keys()), label="Select Target Language", value="English")
65
+
66
+ with gr.Accordion("Speech to Text", open=False) as stt_accordion:
67
+ audio_input_stt = gr.Audio(label="Upload or Record Audio")
68
+ text_output_stt = gr.Text(label="Transcribed Text")
69
+ stt_button = gr.Button("Transcribe")
70
+ stt_button.click(speech_to_text, inputs=[audio_input_stt, input_language], outputs=text_output_stt)
71
+
72
+ with gr.Accordion("Speech to Speech Translation", open=False) as s2st_accordion:
73
+ audio_input_s2st = gr.Audio(label="Upload or Record Audio")
74
+ text_output_s2st = gr.Text(label="Translated Text")
75
+ audio_output_s2st = gr.Audio(label="Translated Audio", type="filepath")
76
+ s2st_button = gr.Button("Translate")
77
+ s2st_button.click(speech_to_speech_translation, inputs=[audio_input_s2st, input_language], outputs=[text_output_s2st, audio_output_s2st])
78
+
79
+ return interface
80
+
81
+ app = create_interface()
82
+ app.launch(show_error=True, debug=True)