Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,57 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
-
# فرض الموافقة التلقائية على شروط الاستخدام
|
| 4 |
-
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 5 |
-
os.system("echo 'I have read, understood and agreed to the Terms and Conditions.' > ~/.local/share/tts/accept_tos")
|
| 6 |
-
|
| 7 |
import gradio as gr
|
| 8 |
from TTS.api import TTS
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# التأكد من وجود مجلد الأصوات
|
| 11 |
os.makedirs("voices", exist_ok=True)
|
| 12 |
|
| 13 |
-
# تحميل نموذج XTTS v2
|
| 14 |
-
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False
|
| 15 |
|
| 16 |
-
# دالة تحويل النص إلى صوت
|
| 17 |
def text_to_speech(text, speaker_wav):
|
| 18 |
-
|
|
|
|
| 19 |
return "يجب رفع ملف صوتي كمرجع!"
|
| 20 |
-
|
| 21 |
output_path = "output.wav"
|
| 22 |
tts.tts_to_file(text=text, speaker_wav=speaker_wav, language="ar", file_path=output_path)
|
| 23 |
-
|
| 24 |
return output_path
|
| 25 |
|
| 26 |
-
# دالة رفع ملف صوتي جديد
|
| 27 |
def upload_voice(file):
|
| 28 |
-
|
|
|
|
| 29 |
return "لم يتم رفع أي ملف!"
|
| 30 |
-
|
| 31 |
save_path = os.path.join("voices", file.name)
|
| 32 |
file.save(save_path)
|
| 33 |
-
|
| 34 |
return f"تم حفظ الصوت باسم: {file.name}"
|
| 35 |
|
| 36 |
-
# الحصول على قائمة الأصوات المرفوعة
|
| 37 |
def get_voices():
|
|
|
|
| 38 |
return os.listdir("voices")
|
| 39 |
|
| 40 |
# واجهة Gradio
|
| 41 |
with gr.Blocks() as app:
|
| 42 |
gr.Markdown("# 🗣️ تحويل النص إلى صوت باستخدام XTTS v2")
|
| 43 |
-
|
| 44 |
with gr.Row():
|
| 45 |
text_input = gr.Textbox(label="📝 أدخل النص", lines=3)
|
| 46 |
speaker_choice = gr.Dropdown(choices=get_voices(), label="🎤 اختر صوتًا مرجعًا")
|
| 47 |
output_audio = gr.Audio(label="🔊 الصوت الناتج")
|
| 48 |
-
|
| 49 |
generate_button = gr.Button("🎙️ تحويل النص إلى صوت")
|
| 50 |
generate_button.click(text_to_speech, inputs=[text_input, speaker_choice], outputs=output_audio)
|
| 51 |
-
|
| 52 |
with gr.Row():
|
| 53 |
upload_input = gr.File(label="📤 رفع ملف صوتي")
|
| 54 |
upload_button = gr.Button("📥 حفظ الصوت")
|
| 55 |
upload_output = gr.Textbox(label="📌 حالة الرفع")
|
| 56 |
-
|
| 57 |
upload_button.click(upload_voice, inputs=[upload_input], outputs=upload_output)
|
| 58 |
|
| 59 |
app.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from TTS.api import TTS
|
| 4 |
|
| 5 |
+
def setup_tos():
|
| 6 |
+
"""يتأكد من الموافقة التلقائية على شروط الخدمة وإنشاء المجلدات اللازمة."""
|
| 7 |
+
tos_path = "/home/user/.local/share/tts/accept_tos"
|
| 8 |
+
os.makedirs(os.path.dirname(tos_path), exist_ok=True)
|
| 9 |
+
with open(tos_path, "w") as f:
|
| 10 |
+
f.write("I have read, understood and agreed to the Terms and Conditions.")
|
| 11 |
+
|
| 12 |
+
# تهيئة البيئة
|
| 13 |
+
setup_tos()
|
| 14 |
+
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 15 |
+
|
| 16 |
# التأكد من وجود مجلد الأصوات
|
| 17 |
os.makedirs("voices", exist_ok=True)
|
| 18 |
|
| 19 |
+
# تحميل نموذج XTTS v2 بدون GPU
|
| 20 |
+
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
| 21 |
|
|
|
|
| 22 |
def text_to_speech(text, speaker_wav):
|
| 23 |
+
"""تحويل النص إلى صوت باستخدام الصوت المرجعي."""
|
| 24 |
+
if not speaker_wav:
|
| 25 |
return "يجب رفع ملف صوتي كمرجع!"
|
|
|
|
| 26 |
output_path = "output.wav"
|
| 27 |
tts.tts_to_file(text=text, speaker_wav=speaker_wav, language="ar", file_path=output_path)
|
|
|
|
| 28 |
return output_path
|
| 29 |
|
|
|
|
| 30 |
def upload_voice(file):
|
| 31 |
+
"""رفع ملف صوتي جديد وحفظه."""
|
| 32 |
+
if not file:
|
| 33 |
return "لم يتم رفع أي ملف!"
|
|
|
|
| 34 |
save_path = os.path.join("voices", file.name)
|
| 35 |
file.save(save_path)
|
|
|
|
| 36 |
return f"تم حفظ الصوت باسم: {file.name}"
|
| 37 |
|
|
|
|
| 38 |
def get_voices():
|
| 39 |
+
"""الحصول على قائمة الأصوات المخزنة."""
|
| 40 |
return os.listdir("voices")
|
| 41 |
|
| 42 |
# واجهة Gradio
|
| 43 |
with gr.Blocks() as app:
|
| 44 |
gr.Markdown("# 🗣️ تحويل النص إلى صوت باستخدام XTTS v2")
|
|
|
|
| 45 |
with gr.Row():
|
| 46 |
text_input = gr.Textbox(label="📝 أدخل النص", lines=3)
|
| 47 |
speaker_choice = gr.Dropdown(choices=get_voices(), label="🎤 اختر صوتًا مرجعًا")
|
| 48 |
output_audio = gr.Audio(label="🔊 الصوت الناتج")
|
|
|
|
| 49 |
generate_button = gr.Button("🎙️ تحويل النص إلى صوت")
|
| 50 |
generate_button.click(text_to_speech, inputs=[text_input, speaker_choice], outputs=output_audio)
|
|
|
|
| 51 |
with gr.Row():
|
| 52 |
upload_input = gr.File(label="📤 رفع ملف صوتي")
|
| 53 |
upload_button = gr.Button("📥 حفظ الصوت")
|
| 54 |
upload_output = gr.Textbox(label="📌 حالة الرفع")
|
|
|
|
| 55 |
upload_button.click(upload_voice, inputs=[upload_input], outputs=upload_output)
|
| 56 |
|
| 57 |
app.launch()
|