Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, BarkModel
|
3 |
+
import scipy.io.wavfile
|
4 |
+
import torch
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Initialize model and processor
|
8 |
+
processor = AutoProcessor.from_pretrained("suno/bark")
|
9 |
+
model = BarkModel.from_pretrained("suno/bark")
|
10 |
+
|
11 |
+
def text_to_speech(text, voice_preset="v2/hi_speaker_2"):
|
12 |
+
# Generate audio from text
|
13 |
+
inputs = processor(text, voice_preset=voice_preset)
|
14 |
+
|
15 |
+
# Generate audio
|
16 |
+
audio_array = model.generate(**inputs)
|
17 |
+
audio_array = audio_array.cpu().numpy().squeeze()
|
18 |
+
|
19 |
+
# Get sample rate from model config
|
20 |
+
sample_rate = model.generation_config.sample_rate
|
21 |
+
|
22 |
+
# Create temporary file path
|
23 |
+
output_path = "temp_audio.wav"
|
24 |
+
|
25 |
+
# Save audio file
|
26 |
+
scipy.io.wavfile.write(output_path, rate=sample_rate, data=audio_array)
|
27 |
+
|
28 |
+
return output_path
|
29 |
+
|
30 |
+
# Define available voice presets
|
31 |
+
voice_presets = [
|
32 |
+
"v2/hi_speaker_1",
|
33 |
+
"v2/hi_speaker_2",
|
34 |
+
"v2/hi_speaker_3",
|
35 |
+
"v2/hi_speaker_4",
|
36 |
+
"v2/hi_speaker_5"
|
37 |
+
]
|
38 |
+
|
39 |
+
# Create Gradio interface
|
40 |
+
demo = gr.Interface(
|
41 |
+
fn=text_to_speech,
|
42 |
+
inputs=[
|
43 |
+
gr.Textbox(label="Enter text (Hindi or English)", placeholder="तुम बहुत अच्छे हो..."),
|
44 |
+
gr.Dropdown(choices=voice_presets, value="v2/hi_speaker_2", label="Select Voice")
|
45 |
+
],
|
46 |
+
outputs=gr.Audio(label="Generated Speech"),
|
47 |
+
title="Bark Text-to-Speech",
|
48 |
+
description="Convert text to speech using the Bark model. Supports Hindi and English text.",
|
49 |
+
examples=[
|
50 |
+
["तुम बहुत अच्छे हो और मैं भी तुम्हारी तरह अच्छा हूँ", "v2/hi_speaker_2"],
|
51 |
+
["You are very nice and I am also nice like you", "v2/hi_speaker_1"]
|
52 |
+
]
|
53 |
+
)
|
54 |
+
|
55 |
+
# Launch the app
|
56 |
+
if __name__ == "__main__":
|
57 |
+
demo.launch()
|