| import gradio as gr |
| import torch |
| import numpy as np |
| from transformers import pipeline |
|
|
| |
| device = "cuda:0" if torch.cuda.is_available() else "cpu" |
|
|
| print(f"Loading aoxo/swaram model on {device}...") |
|
|
| |
| try: |
| synthesizer = pipeline("text-to-speech", model="aoxo/swaram", device=device) |
| print("Model loaded successfully!") |
| except Exception as e: |
| print(f"Error loading model: {e}") |
| synthesizer = None |
|
|
| |
| def generate_audio(text): |
| if not text.strip(): |
| return None, "Please enter some text." |
| |
| if synthesizer is None: |
| return None, "Error: Model failed to load. Check Space logs." |
|
|
| try: |
| |
| speech = synthesizer(text) |
| |
| |
| |
| audio_data = speech["audio"] |
| sample_rate = speech["sampling_rate"] |
| |
| |
| |
| if len(audio_data.shape) > 1: |
| audio_data = np.squeeze(audio_data) |
| |
| return (sample_rate, audio_data), "Success!" |
| |
| except Exception as e: |
| return None, f"Generation Error: {str(e)}" |
|
|
| |
| with gr.Blocks(title="Swaram Malayalam TTS", theme=gr.themes.Soft()) as demo: |
| gr.Markdown( |
| """ |
| # 🗣️ Swaram Malayalam Text-to-Speech |
| Enter Malayalam text below to generate speech using the `aoxo/swaram` model. |
| """ |
| ) |
| |
| with gr.Row(): |
| with gr.Column(): |
| text_input = gr.Textbox( |
| label="Enter Malayalam Text", |
| placeholder="മലയാളം ടൈപ്പ് ചെയ്യുക...", |
| lines=5 |
| ) |
| |
| with gr.Row(): |
| clear_btn = gr.Button("Clear") |
| generate_btn = gr.Button("Generate Speech", variant="primary") |
| |
| gr.Examples( |
| examples=[ |
| ["നമസ്കാരം, ഇതെന്റെ പുതിയ ശബ്ദമാണ്."], |
| ["കേരളം ദൈവത്തിന്റെ സ്വന്തം നാടാണ്."], |
| ["കള്ളാ കടയാടി മോനെ"] |
| ], |
| inputs=[text_input], |
| label="Examples" |
| ) |
| |
| with gr.Column(): |
| audio_output = gr.Audio(label="Generated Audio", type="numpy", interactive=False) |
| status_output = gr.Textbox(label="Status", interactive=False) |
| |
| |
| generate_btn.click( |
| fn=generate_audio, |
| inputs=[text_input], |
| outputs=[audio_output, status_output], |
| api_name="synthesize" |
| ) |
| |
| clear_btn.click( |
| fn=lambda: (None, None, ""), |
| inputs=[], |
| outputs=[text_input, audio_output, status_output] |
| ) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |