| import gradio as gr |
| from TTS.api import TTS |
| import torch |
| import os |
|
|
| |
| device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
| |
| |
| MODEL_DIR = "." |
|
|
| print("Loading XTTS model...") |
| |
| tts = TTS(model_path=MODEL_DIR).to(device) |
| print("Model loaded successfully!") |
|
|
| def synthesize(text, speaker_wav): |
| if not text or speaker_wav is None: |
| return None, "Please provide text and a speaker audio file." |
|
|
| output_path = "output.wav" |
| |
| print(f"Synthesizing audio for text: '{text}'") |
| # توليد الصوت |
| tts.tts_to_file( |
| text=text, |
| speaker_wav=speaker_wav, |
| language="ar", |
| file_path=output_path |
| ) |
| print("Synthesis complete.") |
| |
| return output_path, None |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# واجهة تجربة استنساخ الصوت باللغة العربية\n\nقم برفع ملف صوتي (WAV) واكتب نصًا لتوليد كلام بصوتك المُدرَّب.") |
| |
| with gr.Row(): |
| with gr.Column(): |
| input_text = gr.Textbox(label="النص للكلام", placeholder="اكتب جملة هنا...") |
| ref_audio = gr.Audio(label="الملف الصوتي المرجعي (WAV)", type="filepath") |
| generate_btn = gr.Button("توليد الصوت") |
| with gr.Column(): |
| output_audio = gr.Audio(label="الصوت الناتج") |
| error_message = gr.Textbox(label="رسائل الخطأ", visible=False) |
|
|
| generate_btn.click( |
| fn=synthesize, |
| inputs=[input_text, ref_audio], |
| outputs=[output_audio, error_message] |
| ) |
|
|
| demo.launch() |