HabibiBear commited on
Commit
62da79e
·
verified ·
1 Parent(s): 94ddcee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -54
app.py CHANGED
@@ -1,56 +1,55 @@
1
- # WebUI by mrfakename
2
- # Demo also available on HF Spaces: https://huggingface.co/spaces/mrfakename/MeloTTS
3
  import gradio as gr
4
- import os, torch, io
5
- os.system('python -m unidic download')
6
- # print("Make sure you've downloaded unidic (python -m unidic download) for this WebUI to work.")
7
- from melo.api import TTS
8
- speed = 1.0
9
- import tempfile
10
- import nltk
11
- nltk.download('averaged_perceptron_tagger_eng')
12
- device = 'cuda' if torch.cuda.is_available() else 'cpu'
13
- models = {
14
- 'EN': TTS(language='EN', device=device),
15
- 'ES': TTS(language='ES', device=device),
16
- 'FR': TTS(language='FR', device=device),
17
- 'ZH': TTS(language='ZH', device=device),
18
- 'JP': TTS(language='JP', device=device),
19
- 'KR': TTS(language='KR', device=device),
20
- }
21
- speaker_ids = models['EN'].hps.data.spk2id
22
-
23
- default_text_dict = {
24
- 'EN': 'The field of text-to-speech has seen rapid development recently.',
25
- 'ES': 'El campo de la conversión de texto a voz ha experimentado un rápido desarrollo recientemente.',
26
- 'FR': 'Le domaine de la synthèse vocale a connu un développement rapide récemment',
27
- 'ZH': 'text-to-speech 领域近年来发展迅速',
28
- 'JP': 'テキスト読み上げの分野は最近急速な発展を遂げています',
29
- 'KR': '최근 텍스트 음성 변환 분야가 급속도로 발전하고 있습니다.',
30
  }
31
-
32
- def synthesize(text, speaker, speed, language, progress=gr.Progress()):
33
- bio = io.BytesIO()
34
- models[language].tts_to_file(text, models[language].hps.data.spk2id[speaker], bio, speed=speed, pbar=progress.tqdm, format='wav')
35
- return bio.getvalue()
36
- def load_speakers(language, text):
37
- if text in list(default_text_dict.values()):
38
- newtext = default_text_dict[language]
39
- else:
40
- newtext = text
41
- return gr.update(value=list(models[language].hps.data.spk2id.keys())[0], choices=list(models[language].hps.data.spk2id.keys())), newtext
42
- with gr.Blocks() as demo:
43
- gr.Markdown('# MeloTTS Demo\n\nAn unofficial demo for [MeloTTS](https://github.com/myshell-ai/MeloTTS). **Make sure to try out several speakers, for example EN-Default!**')
44
- with gr.Group():
45
- speaker = gr.Dropdown(speaker_ids.keys(), interactive=True, value='EN-US', label='Speaker')
46
- language = gr.Radio(['EN', 'ES', 'FR', 'ZH', 'JP', 'KR'], label='Language', value='EN')
47
- speed = gr.Slider(label='Speed', minimum=0.1, maximum=10.0, value=1.0, interactive=True, step=0.1)
48
- text = gr.Textbox(label="Text to speak", value=default_text_dict['EN'])
49
- language.input(load_speakers, inputs=[language, text], outputs=[speaker, text])
50
- btn = gr.Button('Synthesize', variant='primary')
51
- aud = gr.Audio(interactive=False)
52
- btn.click(synthesize, inputs=[text, speaker, speed, language], outputs=[aud])
53
- gr.Markdown('Demo by [mrfakename](https://twitter.com/realmrfakename).')
54
-
55
-
56
- demo.queue(api_open=True, default_concurrency_limit=10).launch(show_api=True)
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import spaces
3
+ import torch
4
+
5
+ from transformers import pipeline
6
+
7
+ # Pre-Initialize
8
+ DEVICE = "auto"
9
+ if DEVICE == "auto":
10
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
11
+ print(f"[SYSTEM] | Using {DEVICE} type compute device.")
12
+
13
+ # Variables
14
+ DEFAULT_TASK = "transcribe"
15
+ BATCH_SIZE = 8
16
+
17
+ repo = pipeline(task="automatic-speech-recognition", model="openai/whisper-large-v3-turbo", chunk_length_s=30, device=DEVICE)
18
+
19
+ css = '''
20
+ .gradio-container{max-width: 560px !important}
21
+ h1{text-align:center}
22
+ footer {
23
+ visibility: hidden
 
 
 
 
24
  }
25
+ '''
26
+
27
+ # Functions
28
+ @spaces.GPU(duration=10)
29
+ def transcribe(input=None, task=DEFAULT_TASK):
30
+ print(input)
31
+ if input is None: raise gr.Error("Invalid input.")
32
+ output = repo(input, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
33
+ return output
34
+
35
+ def cloud():
36
+ print("[CLOUD] | Space maintained.")
37
+
38
+ # Initialize
39
+ with gr.Blocks(css=css) as main:
40
+ with gr.Column():
41
+ gr.Markdown("🪄 Transcribe audio to text.")
42
+
43
+ with gr.Column():
44
+ input = gr.Audio(sources="upload", type="filepath", label="Input")
45
+ task = gr.Radio(["transcribe", "translate"], label="Task", value=DEFAULT_TASK)
46
+ submit = gr.Button("▶")
47
+ maintain = gr.Button("☁️")
48
+
49
+ with gr.Column():
50
+ output = gr.Textbox(lines=1, value="", label="Output")
51
+
52
+ submit.click(transcribe, inputs=[input, task], outputs=[output], queue=False)
53
+ maintain.click(cloud, inputs=[], outputs=[], queue=False)
54
+
55
+ main.launch(show_api=True)