import os import torch # حل مشكلة الأمان في النسخ الحديثة من PyTorch try: from TTS.tts.configs.xtts_config import XttsConfig torch.serialization.add_safe_globals([XttsConfig]) except ImportError: pass # الموافقة الآلية على شروط الاستخدام os.environ["COQUI_TOS_AGREED"] = "1" import gradio as gr from TTS.api import TTS # إعداد الجهاز (GPU أو CPU) device = "cuda" if torch.cuda.is_available() else "cpu" print("جاري تهيئة نموذج XTTS v2 العربي...") try: # تحميل النموذج tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device) print("تم تحميل النموذج بنجاح!") except Exception as e: print(f"حدث خطأ أثناء تحميل النموذج: {e}") def process_tts(text, audio_reference): if not text or not audio_reference: return None, "يرجى إدخال النص ورفع ملف صوتي مرجعي." output_file = "output_voice.wav" try: tts.tts_to_file( text=text, speaker_wav=audio_reference, language="ar", file_path=output_file ) return output_file, "تم توليد الصوت بنجاح!" except Exception as e: return None, f"خطأ أثناء التوليد: {str(e)}" # واجهة المستخدم with gr.Blocks() as demo: gr.Markdown("# 🎙️ المحرك العربي النهائي لاستنساخ الصوت") with gr.Row(): with gr.Column(): txt_input = gr.Textbox(label="النص العربي", lines=5, placeholder="اكتب النص هنا...") audio_input = gr.Audio(label="عينة الصوت المرجعية", type="filepath") generate_btn = gr.Button("توليد الصوت الآن", variant="primary") with gr.Column(): audio_output = gr.Audio(label="الصوت الناتج") status_msg = gr.Textbox(label="حالة النظام", interactive=False) generate_btn.click( fn=process_tts, inputs=[txt_input, audio_input], outputs=[audio_output, status_msg] ) demo.launch()