Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from transformers import BarkModel
|
5 |
+
from optimum.bettertransformer import BetterTransformer
|
6 |
+
|
7 |
+
bark_model = BarkModel.from_pretrained("suno/bark", torch_dtype=torch.float16)
|
8 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
9 |
+
bark_model = bark_model.to(device)
|
10 |
+
|
11 |
+
from transformers import AutoProcessor
|
12 |
+
processor = AutoProcessor.from_pretrained("suno/bark")
|
13 |
+
|
14 |
+
# Use bettertransform for flash attention
|
15 |
+
bark_model = BetterTransformer.transform(bark_model, keep_original_model=False)
|
16 |
+
|
17 |
+
# Enable CPU offload
|
18 |
+
bark_model.enable_cpu_offload()
|
19 |
+
|
20 |
+
from TTS.tts.configs.bark_config import BarkConfig
|
21 |
+
from TTS.tts.models.bark import Bark
|
22 |
+
|
23 |
+
config = BarkConfig()
|
24 |
+
model = Bark.init_from_config(config)
|
25 |
+
model.load_checkpoint(config, checkpoint_dir=bark_model, eval=True)
|
26 |
+
|
27 |
+
def infer(prompt):
|
28 |
+
|
29 |
+
text = "Hello, my name is Manmay , how are you?"
|
30 |
+
|
31 |
+
# with random speaker
|
32 |
+
output_dict = model.synthesize(text, config, speaker_id="random", voice_dirs=None)
|
33 |
+
|
34 |
+
return "done"
|
35 |
+
|
36 |
+
gr.Interface(fn=infer, inputs=[gr.Textbox()], outputs=[gr.Textbox()]).launch()
|