tts-s4dev / .py
Saad4web's picture
Create .py
0bb70c1 verified
import gradio as gr
from TTS.api import TTS
import torch
import os
# تحقق من توفر GPU، وإلا استخدم CPU
device = "cuda" if torch.cuda.is_available() else "cpu"
# المسار إلى المجلد الذي يحتوي على model.pth و config.json
# في Hugging Face Spaces، "." تعني المجلد الحالي.
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()