Spaces:
Running
Running
core space code
Browse files
app.py
CHANGED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import VitsModel, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
import logging
|
5 |
+
|
6 |
+
# Configure logging
|
7 |
+
logging.basicConfig(level=logging.INFO)
|
8 |
+
logger = logging.getLogger(__name__)
|
9 |
+
|
10 |
+
# Example text in Bambara
|
11 |
+
example = "An filɛ ni ye yɔrɔ minna ni an ye an sigi ka a layɛ yala an bɛ ka baara min kɛ ɛsike a kɛlen don ka Ɲɛ wa ?"
|
12 |
+
|
13 |
+
# Load model and tokenizer
|
14 |
+
try:
|
15 |
+
logger.info("Loading Bambara TTS model and tokenizer...")
|
16 |
+
model = VitsModel.from_pretrained("sudoping01/bambara-tts")
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained("sudoping01/bambara-tts")
|
18 |
+
logger.info("Successfully loaded model and tokenizer")
|
19 |
+
except Exception as e:
|
20 |
+
logger.error(f"Failed to load model: {str(e)}")
|
21 |
+
raise Exception(f"Model loading failed: {str(e)}")
|
22 |
+
|
23 |
+
def generate_audio(text):
|
24 |
+
if not text.strip():
|
25 |
+
return None, "Please enter some text to synthesize."
|
26 |
+
|
27 |
+
try:
|
28 |
+
inputs = tokenizer(text, return_tensors="pt")
|
29 |
+
|
30 |
+
with torch.no_grad():
|
31 |
+
output = model(**inputs).waveform
|
32 |
+
|
33 |
+
waveform = output.squeeze().cpu().numpy()
|
34 |
+
sample_rate = model.config.sampling_rate
|
35 |
+
|
36 |
+
return (sample_rate, waveform), None
|
37 |
+
except Exception as e:
|
38 |
+
logger.error(f"Error during inference: {str(e)}")
|
39 |
+
return None, f"Error generating audio: {str(e)}"
|
40 |
+
|
41 |
+
def load_example():
|
42 |
+
return example
|
43 |
+
|
44 |
+
# Create Gradio interface
|
45 |
+
with gr.Blocks(title="Bambara TTS") as demo:
|
46 |
+
gr.Markdown(
|
47 |
+
"""
|
48 |
+
# Bambara TTS: Text-to-Speech for Bambara Language 🇲🇱
|
49 |
+
|
50 |
+
High-quality text-to-speech for Bambara (Bamanankan), spoken by over 14 million people.
|
51 |
+
|
52 |
+
- ✅ Real-time TTS with fast response
|
53 |
+
- ✅ Runs on CPU
|
54 |
+
|
55 |
+
## How to Use
|
56 |
+
1. Enter your Bambara text or load the example
|
57 |
+
2. Click **"Generate Audio"** to listen
|
58 |
+
"""
|
59 |
+
)
|
60 |
+
|
61 |
+
with gr.Row():
|
62 |
+
with gr.Column():
|
63 |
+
text = gr.Textbox(label="Bambara Text", lines=5, placeholder="Type your text in Bambara here...")
|
64 |
+
example_btn = gr.Button("Load Example")
|
65 |
+
|
66 |
+
generate_btn = gr.Button("Generate Audio", variant="primary")
|
67 |
+
audio_output = gr.Audio(label="Generated Audio", type="numpy")
|
68 |
+
error_msg = gr.Textbox(label="Status", visible=False)
|
69 |
+
|
70 |
+
# Footer
|
71 |
+
gr.Markdown(
|
72 |
+
"""
|
73 |
+
By [sudoping01](https://huggingface.co/sudoping01). Model: [sudoping01/bambara-tts](https://huggingface.co/sudoping01/bambara-tts). Fine-tuned on Meta's MMS TTS. License: CC BY-NC 4.0 (non-commercial use).
|
74 |
+
"""
|
75 |
+
)
|
76 |
+
|
77 |
+
# Connect buttons to functions
|
78 |
+
generate_btn.click(
|
79 |
+
fn=generate_audio,
|
80 |
+
inputs=[text],
|
81 |
+
outputs=[audio_output, error_msg]
|
82 |
+
)
|
83 |
+
example_btn.click(
|
84 |
+
fn=load_example,
|
85 |
+
inputs=[],
|
86 |
+
outputs=text
|
87 |
+
)
|
88 |
+
|
89 |
+
# Launch the demo
|
90 |
+
demo.launch()
|