Create .py
Browse files
.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from TTS.api import TTS
|
| 3 |
+
import torch
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# تحقق من توفر GPU، وإلا استخدم CPU
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
|
| 9 |
+
# المسار إلى المجلد الذي يحتوي على model.pth و config.json
|
| 10 |
+
# في Hugging Face Spaces، "." تعني المجلد الحالي.
|
| 11 |
+
MODEL_DIR = "."
|
| 12 |
+
|
| 13 |
+
print("Loading XTTS model...")
|
| 14 |
+
# تحميل النموذج مرة واحدة عند بدء تشغيل التطبيق
|
| 15 |
+
tts = TTS(model_path=MODEL_DIR).to(device)
|
| 16 |
+
print("Model loaded successfully!")
|
| 17 |
+
|
| 18 |
+
def synthesize(text, speaker_wav):
|
| 19 |
+
if not text or speaker_wav is None:
|
| 20 |
+
return None, "Please provide text and a speaker audio file."
|
| 21 |
+
|
| 22 |
+
output_path = "output.wav"
|
| 23 |
+
|
| 24 |
+
print(f"Synthesizing audio for text: '{text}'")
|
| 25 |
+
# توليد الصوت
|
| 26 |
+
tts.tts_to_file(
|
| 27 |
+
text=text,
|
| 28 |
+
speaker_wav=speaker_wav,
|
| 29 |
+
language="ar",
|
| 30 |
+
file_path=output_path
|
| 31 |
+
)
|
| 32 |
+
print("Synthesis complete.")
|
| 33 |
+
|
| 34 |
+
return output_path, None
|
| 35 |
+
|
| 36 |
+
# تصميم الواجهة الرسومية
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("# واجهة تجربة استنساخ الصوت باللغة العربية\n\nقم برفع ملف صوتي (WAV) واكتب نصًا لتوليد كلام بصوتك المُدرَّب.")
|
| 39 |
+
|
| 40 |
+
with gr.Row():
|
| 41 |
+
with gr.Column():
|
| 42 |
+
input_text = gr.Textbox(label="النص للكلام", placeholder="اكتب جملة هنا...")
|
| 43 |
+
ref_audio = gr.Audio(label="الملف الصوتي المرجعي (WAV)", type="filepath")
|
| 44 |
+
generate_btn = gr.Button("توليد الصوت")
|
| 45 |
+
with gr.Column():
|
| 46 |
+
output_audio = gr.Audio(label="الصوت الناتج")
|
| 47 |
+
error_message = gr.Textbox(label="رسائل الخطأ", visible=False)
|
| 48 |
+
|
| 49 |
+
generate_btn.click(
|
| 50 |
+
fn=synthesize,
|
| 51 |
+
inputs=[input_text, ref_audio],
|
| 52 |
+
outputs=[output_audio, error_message]
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
demo.launch()
|