Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +80 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import VitsModel, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
import scipy.io.wavfile
|
| 5 |
+
|
| 6 |
+
# تحميل النموذج و "tokenizer"
|
| 7 |
+
model_name = "wasmdashai/vits-ar-sa-huba-v2"
|
| 8 |
+
try:
|
| 9 |
+
model = VitsModel.from_pretrained(model_name)
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
+
model_loaded_successfully = True
|
| 12 |
+
print(f"تم تحميل النموذج '{model_name}' بنجاح.")
|
| 13 |
+
except Exception as e:
|
| 14 |
+
model_loaded_successfully = False
|
| 15 |
+
error_message = f"حدث خطأ أثناء تحميل النموذج '{model_name}': {e}"
|
| 16 |
+
print(error_message)
|
| 17 |
+
# يمكنك اختيار إيقاف التطبيق هنا أو عرض رسالة خطأ في الواجهة
|
| 18 |
+
|
| 19 |
+
# دالة التنبؤ (تحويل النص إلى كلام)
|
| 20 |
+
def text_to_speech(text):
|
| 21 |
+
if not model_loaded_successfully:
|
| 22 |
+
return None, f"خطأ: لم يتم تحميل النموذج. {error_message}"
|
| 23 |
+
if not text:
|
| 24 |
+
return None, "الرجاء إدخال نص."
|
| 25 |
+
try:
|
| 26 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
output = model(**inputs).waveform
|
| 29 |
+
|
| 30 |
+
# التأكد من أن المخرجات هي 1D tensor
|
| 31 |
+
if output.ndim > 1:
|
| 32 |
+
# إذا كانت متعددة الأبعاد، جرب أخذ القناة الأولى أو المتوسط
|
| 33 |
+
# هنا نفترض أن القناة الأولى هي الصوت المطلوب
|
| 34 |
+
waveform = output[0].cpu().numpy()
|
| 35 |
+
else:
|
| 36 |
+
waveform = output.cpu().numpy()
|
| 37 |
+
|
| 38 |
+
# معدل أخذ العينات للنموذج (عادة ما يكون مذكورًا في بطاقة النموذج أو تكوينه)
|
| 39 |
+
# إذا لم يكن متاحًا بشكل مباشر، قد تحتاج إلى التحقق من config النموذج
|
| 40 |
+
# القيمة الشائعة لنماذج VITS هي 22050 أو 16000
|
| 41 |
+
# بالنظر إلى بطاقة النموذج، يبدو أنها لا تحدد صراحةً، سنجرب 22050
|
| 42 |
+
sampling_rate = model.config.sampling_rate if hasattr(model.config, 'sampling_rate') else 22050
|
| 43 |
+
|
| 44 |
+
# حفظ الملف الصوتي مؤقتًا
|
| 45 |
+
output_path = "output_audio.wav"
|
| 46 |
+
scipy.io.wavfile.write(output_path, rate=sampling_rate, data=waveform)
|
| 47 |
+
return output_path, None # لا يوجد رسالة خطأ
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"حدث خطأ أثناء تحويل النص إلى كلام: {e}")
|
| 50 |
+
return None, f"حدث خطأ أثناء المعالجة: {str(e)}"
|
| 51 |
+
|
| 52 |
+
# إنشاء واجهة Gradio
|
| 53 |
+
iface = gr.Interface(
|
| 54 |
+
fn=text_to_speech,
|
| 55 |
+
inputs=gr.Textbox(lines=3, placeholder="أدخل النص العربي هنا...", label="النص العربي"),
|
| 56 |
+
outputs=[
|
| 57 |
+
gr.Audio(label="الصوت الناتج", type="filepath"),
|
| 58 |
+
gr.Textbox(label="رسالة الخطأ (إن وجدت)") # لإظهار رسائل الخطأ بشكل واضح
|
| 59 |
+
],
|
| 60 |
+
title="تحويل النص إلى كلام باللغة العربية (نموذج vits-ar-sa-huda-v2)",
|
| 61 |
+
description="أدخل نصًا باللغة العربية ليتم تحويله إلى كلام باستخدام نموذج VITS المدرب على اللهجة السعودية . النموذج من تطوير `wasmdashai`.",
|
| 62 |
+
allow_flagging="never",
|
| 63 |
+
examples=[
|
| 64 |
+
["السلام عليكم ورحمة الله وبركاته."],
|
| 65 |
+
["كيف حالك اليوم؟"],
|
| 66 |
+
["أهلاً وسهلاً بكم في هذه المساحة."],
|
| 67 |
+
]
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# تشغيل الواجهة
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
if model_loaded_successfully:
|
| 73 |
+
iface.launch()
|
| 74 |
+
else:
|
| 75 |
+
# إذا لم يتم تحميل النموذج، قم بتشغيل واجهة بسيطة تعرض الخطأ
|
| 76 |
+
def error_interface():
|
| 77 |
+
with gr.Blocks() as demo:
|
| 78 |
+
gr.Markdown(f"# خطأ في تحميل النموذج\n{error_message}")
|
| 79 |
+
return demo
|
| 80 |
+
error_interface().launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
torchaudio
|
| 4 |
+
gradio
|
| 5 |
+
librosa
|
| 6 |
+
phonemizer
|
| 7 |
+
espeak-ng
|