malekradwan130's picture
Update app.py
61bd2fc verified
import os
import gradio as gr
from TTS.api import TTS
def setup_tos():
"""يتأكد من الموافقة التلقائية على شروط الخدمة وإنشاء المجلدات اللازمة."""
tos_path = "/home/user/.local/share/tts/accept_tos"
os.makedirs(os.path.dirname(tos_path), exist_ok=True)
with open(tos_path, "w") as f:
f.write("I have read, understood and agreed to the Terms and Conditions.")
# تهيئة البيئة
setup_tos()
os.environ["COQUI_TOS_AGREED"] = "1"
# التأكد من وجود مجلد الأصوات
os.makedirs("voices", exist_ok=True)
# تحميل نموذج XTTS v2 بدون GPU
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
def text_to_speech(text, speaker_wav):
"""تحويل النص إلى صوت باستخدام الصوت المرجعي."""
if not speaker_wav:
return "يجب رفع ملف صوتي كمرجع!"
output_path = "output.wav"
tts.tts_to_file(text=text, speaker_wav=speaker_wav, language="ar", file_path=output_path)
return output_path
def upload_voice(file):
"""رفع ملف صوتي جديد وحفظه."""
if not file:
return "لم يتم رفع أي ملف!"
save_path = os.path.join("voices", file.name)
file.save(save_path)
return f"تم حفظ الصوت باسم: {file.name}"
def get_voices():
"""الحصول على قائمة الأصوات المخزنة."""
return os.listdir("voices")
# واجهة Gradio
with gr.Blocks() as app:
gr.Markdown("# 🗣️ تحويل النص إلى صوت باستخدام XTTS v2")
with gr.Row():
text_input = gr.Textbox(label="📝 أدخل النص", lines=3)
speaker_choice = gr.Dropdown(choices=get_voices(), label="🎤 اختر صوتًا مرجعًا")
output_audio = gr.Audio(label="🔊 الصوت الناتج")
generate_button = gr.Button("🎙️ تحويل النص إلى صوت")
generate_button.click(text_to_speech, inputs=[text_input, speaker_choice], outputs=output_audio)
with gr.Row():
upload_input = gr.File(label="📤 رفع ملف صوتي")
upload_button = gr.Button("📥 حفظ الصوت")
upload_output = gr.Textbox(label="📌 حالة الرفع")
upload_button.click(upload_voice, inputs=[upload_input], outputs=upload_output)
app.launch()