muaadh019 commited on
Commit
9c76e54
·
verified ·
1 Parent(s): 01d02c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -28
app.py CHANGED
@@ -1,46 +1,88 @@
1
- import spaces # يجب أن يكون السطر الأول
2
  import gradio as gr
3
  import os
 
 
4
  import time
5
- import subprocess
 
 
6
 
7
- # تعريف وظيفة التوليد مع ديكوريتور الـ GPU لإرضاء نظام Hugging Face
8
- @spaces.GPU
9
- def synthesize_piper(text):
10
- if not text or len(text.strip()) < 2:
11
- return None, "الرجاء إدخال نص صالح."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
 
13
  start_time = time.time()
14
- output_file = "output.wav"
15
-
16
- # نموذج "خالد" الأردني - صوت وقور ومسترسل
17
- model_name = "ar_JO-khalid-medium"
18
 
19
  try:
20
- # تشغيل محرك Piper
21
- # ملاحظة: Piper سريع جداً لدرجة أنه قد لا يحتاج للـ GPU فعلياً، لكننا نضعه هنا ليعمل الـ Space
22
- command = f'echo "{text}" | piper --model {model_name} --output_file {output_file}'
23
- subprocess.run(command, shell=True, check=True)
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  duration = time.time() - start_time
26
- return output_file, f"✅ تم التوليد بنجاح في {duration:.2f} ثانية."
27
  except Exception as e:
28
  return None, f"❌ حدث خطأ: {str(e)}"
29
 
30
- # بناء الواجهة
31
- with gr.Blocks() as demo:
32
- gr.Markdown("# 🚀 قارئ المقالات السريع (Piper Arabic)")
33
- gr.Markdown("هذا المحرك مصمم للسرعة القصوى والاستقرار على Hugging Face.")
 
 
 
 
 
34
 
35
  with gr.Row():
36
  with gr.Column():
37
- input_text = gr.Textbox(label="نص المقال", lines=10, placeholder="ألصق النص هنا...", rtl=True)
38
- btn = gr.Button("🚀 توليد فوري", variant="primary")
 
 
 
39
  with gr.Column():
40
- output_audio = gr.Audio(label="الصوت المولد")
41
- status = gr.Textbox(label="الحالة")
42
-
43
- btn.click(synthesize_piper, inputs=input_text, outputs=[output_audio, status])
 
 
 
 
44
 
45
- # نقل التنسيق إلى هنا لإصلاح التحذير الأخير
46
- demo.launch(css=".gradio-container {direction: rtl; text-align: right;}")
 
1
+ import spaces # السطر الأول دائماً
2
  import gradio as gr
3
  import os
4
+ import torch
5
+ import soundfile as sf
6
  import time
7
+ from huggingface_hub import hf_hub_download
8
+ from f5_tts.model import DiT
9
+ from f5_tts.infer.utils_infer import load_model, load_vocoder, preprocess_ref_audio_text, infer_process
10
 
11
+ # إعدادات النموذج السعودي (NAMAA Saudi TTS)
12
+ REPO_ID = "NAMAA-Space/NAMAA-Saudi-TTS-V2"
13
+ V1_BASE_CFG = dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4)
14
+
15
+ # تحميل النموذج مرة واحدة
16
+ print("جاري تحميل المحرك السعودي الفخم...")
17
+ device = "cuda" if torch.cuda.is_available() else "cpu"
18
+ ckpt_path = hf_hub_download(repo_id=REPO_ID, filename="model_last.pt")
19
+ vocab_path = hf_hub_download(repo_id=REPO_ID, filename="vocab.txt")
20
+
21
+ model = load_model(DiT, V1_BASE_CFG, ckpt_path, vocab_file=vocab_path, device=device)
22
+ model = model.to(torch.float32).eval()
23
+ vocoder = load_vocoder()
24
+
25
+ @spaces.GPU(duration=120)
26
+ def saudi_tts_predict(text, ref_audio, ref_text):
27
+ if not text or not ref_audio:
28
+ return None, "الرجاء إدخال النص ورفع الصوت المرجعي."
29
+
30
+ # تنظيف النص ومعالجته
31
+ clean_text = text.replace("\n", " ").strip()
32
 
33
+ output_file = "saudi_output.wav"
34
  start_time = time.time()
 
 
 
 
35
 
36
  try:
37
+ # معالجة الصوت المرجعي
38
+ # إذا لم يتم إدخال نص المرجع، نستخدم نصاً افتراضياً أو نحاول استخراجه
39
+ final_ref_text = ref_text if ref_text else "مرحباً بك في نظام القراءة الآلي المطور"
40
+ ref_audio_data, ref_text_data = preprocess_ref_audio_text(ref_audio, final_ref_text)
41
 
42
+ # التوليد باستخدام المحرك السعودي
43
+ wave, sr, _ = infer_process(
44
+ ref_audio_data,
45
+ ref_text_data,
46
+ clean_text,
47
+ model,
48
+ vocoder,
49
+ nfe_step=32, # جودة عالية
50
+ speed=1.0,
51
+ device=device
52
+ )
53
+
54
+ sf.write(output_file, wave, sr)
55
  duration = time.time() - start_time
56
+ return output_file, f"✅ تم التوليد بنبرة سعودية فخمة في {duration:.2f} ثانية."
57
  except Exception as e:
58
  return None, f"❌ حدث خطأ: {str(e)}"
59
 
60
+ # واجهة مستخدم تليق بمشروع سعودي
61
+ custom_css = """
62
+ .gradio-container { direction: rtl; text-align: right; font-family: 'Cairo', sans-serif; }
63
+ #title { text-align: center; color: #006C35; } /* لون العلم السعودي */
64
+ """
65
+
66
+ with gr.Blocks(css=custom_css) as demo:
67
+ gr.Markdown("# 🇸🇦 قارئ المقالات السعودي المطور (NAMAA V2)", elem_id="title")
68
+ gr.Markdown("استخدم هذا المحرك للحصول على قراءة مسترسلة وفخمة للمقالات الإخبارية.")
69
 
70
  with gr.Row():
71
  with gr.Column():
72
+ input_text = gr.Textbox(label="نص المقال الإخباري", lines=10, placeholder="ألصق مقالك هنا...", rtl=True)
73
+ ref_audio_input = gr.Audio(label="ارفع صوت المذيع الذي تريد تقليده", type="filepath")
74
+ ref_text_input = gr.Textbox(label="ماذا يقول المذيع في المقطع المرفوع؟", placeholder="اكتب النص لضمان دقة 100%...", rtl=True)
75
+ generate_btn = gr.Button("🚀 توليد الصوت الفخم", variant="primary")
76
+
77
  with gr.Column():
78
+ output_audio = gr.Audio(label="صوت المذيع السعودي")
79
+ status_msg = gr.Textbox(label="حالة المعالجة", interactive=False)
80
+
81
+ generate_btn.click(
82
+ fn=saudi_tts_predict,
83
+ inputs=[input_text, ref_audio_input, ref_text_input],
84
+ outputs=[output_audio, status_msg]
85
+ )
86
 
87
+ if __name__ == "__main__":
88
+ demo.launch()