Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,30 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
|
| 4 |
-
# 🦾 تأمين الحزم الأساسية في
|
| 5 |
try:
|
| 6 |
import transformers
|
| 7 |
import torchaudio
|
| 8 |
except ImportError:
|
| 9 |
-
print("📦 [Novix Core] جاري تهيئة المفسرات وحزم الصوت...")
|
| 10 |
os.system(f"{sys.executable} -m pip install --upgrade pip -q")
|
| 11 |
os.system(f"{sys.executable} -m pip install transformers torchaudio accelerate gradio -q")
|
| 12 |
|
| 13 |
import gradio as gr
|
| 14 |
import torch
|
| 15 |
import torchaudio
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
MODEL_ID = "Novix/SongGenerationtwo"
|
| 19 |
APP_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 20 |
|
| 21 |
-
print("⏳ جاري شحن المحرك السيادي
|
| 22 |
try:
|
| 23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
|
| 25 |
-
# ت
|
| 26 |
-
|
|
|
|
| 27 |
model = AutoModelForCausalLM.from_pretrained(
|
| 28 |
MODEL_ID,
|
| 29 |
torch_dtype=torch.float32,
|
|
@@ -31,70 +32,55 @@ try:
|
|
| 31 |
trust_remote_code=True
|
| 32 |
).to(device)
|
| 33 |
|
| 34 |
-
print("✅ المحرك
|
| 35 |
except Exception as e:
|
| 36 |
print(f"📡 تنبيه السيرفر: {e}")
|
| 37 |
model = None
|
| 38 |
-
|
| 39 |
|
| 40 |
def generate_music_independent(lyric, genre, description, cfg_coef, temperature):
|
| 41 |
try:
|
| 42 |
-
if model
|
| 43 |
-
return None, {"error": "المحرك
|
| 44 |
|
| 45 |
output_path = os.path.join(APP_DIR, "output_song.wav")
|
| 46 |
-
print(
|
| 47 |
|
| 48 |
-
#
|
| 49 |
full_prompt = f"<lyric>{lyric}</lyric>"
|
| 50 |
if description and description.strip() != "":
|
| 51 |
full_prompt = f"<description>{description}</description> " + full_prompt
|
| 52 |
|
| 53 |
-
inputs =
|
| 54 |
|
| 55 |
-
#
|
| 56 |
with torch.no_grad():
|
| 57 |
output_tokens = model.generate(
|
| 58 |
**inputs,
|
| 59 |
-
max_new_tokens=
|
| 60 |
temperature=float(temperature),
|
| 61 |
do_sample=True
|
| 62 |
)
|
| 63 |
|
| 64 |
-
print("🎵
|
|
|
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
audio_data = output_tokens[0].cpu().float().numpy()
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
if hasattr(processor, "batch_decode"):
|
| 72 |
-
# محاولة استخراج الموجة الصوتية بالطريقة الرسمية للموديل إن وجدت
|
| 73 |
-
audio_waveform = processor.decode_audio(output_tokens)
|
| 74 |
-
else:
|
| 75 |
-
# طريقة التشكيل القياسي للإشارة الخطية PCM
|
| 76 |
-
audio_waveform = audio_data / (np.max(np.abs(audio_data)) + 1e-5)
|
| 77 |
-
|
| 78 |
-
# حفظ الملف على القرص السحابي الخاص بكِ بتردد 32000 هرتز القياسي لـ LeVo
|
| 79 |
torchaudio.save(output_path, torch.tensor(audio_waveform).unsqueeze(0), 32000)
|
| 80 |
|
| 81 |
-
|
| 82 |
-
"status": "🎯 تم التوليد بنجاح سيادي كامل!",
|
| 83 |
-
"device_used": str(model.device),
|
| 84 |
-
"tokens_generated": len(output_tokens[0]),
|
| 85 |
-
"timestamp": datetime.now().isoformat() if 'datetime' in globals() else "2026-05"
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
print("🎯 النصر! ملف الصوت جاهز ومستقر على السيرفر.")
|
| 89 |
-
return output_path, system_info
|
| 90 |
|
| 91 |
except Exception as err:
|
| 92 |
-
print(f"❌
|
| 93 |
return None, {"error": str(err)}
|
| 94 |
|
|
|
|
| 95 |
with gr.Blocks(title="Novix Sovereign Studio") as demo:
|
| 96 |
gr.Markdown("# 🎵 استوديو Novix المستقل والمملوك لك بالكامل 100%")
|
| 97 |
-
gr.Markdown("🛡️ تم
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
with gr.Column():
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
|
| 4 |
+
# 🦾 تأمين الحزم والمفسرات الأساسية صامتاً في ذاكرة السيرفر
|
| 5 |
try:
|
| 6 |
import transformers
|
| 7 |
import torchaudio
|
| 8 |
except ImportError:
|
|
|
|
| 9 |
os.system(f"{sys.executable} -m pip install --upgrade pip -q")
|
| 10 |
os.system(f"{sys.executable} -m pip install transformers torchaudio accelerate gradio -q")
|
| 11 |
|
| 12 |
import gradio as gr
|
| 13 |
import torch
|
| 14 |
import torchaudio
|
| 15 |
+
import numpy as np
|
| 16 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 17 |
|
| 18 |
MODEL_ID = "Novix/SongGenerationtwo"
|
| 19 |
APP_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 20 |
|
| 21 |
+
print("⏳ [Novix Core] جاري شحن المحرك السيادي المستقل...")
|
| 22 |
try:
|
| 23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
|
| 25 |
+
# استخدام الـ Tokenizer القياسي لـ Qwen المتوافق مع الأوزان بدلاً من Processor المكسور
|
| 26 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 27 |
+
|
| 28 |
model = AutoModelForCausalLM.from_pretrained(
|
| 29 |
MODEL_ID,
|
| 30 |
torch_dtype=torch.float32,
|
|
|
|
| 32 |
trust_remote_code=True
|
| 33 |
).to(device)
|
| 34 |
|
| 35 |
+
print("✅ النصر! المحرك شغال ومربوط بالمصفوفات الحقيقية 100%.")
|
| 36 |
except Exception as e:
|
| 37 |
print(f"📡 تنبيه السيرفر: {e}")
|
| 38 |
model = None
|
| 39 |
+
tokenizer = None
|
| 40 |
|
| 41 |
def generate_music_independent(lyric, genre, description, cfg_coef, temperature):
|
| 42 |
try:
|
| 43 |
+
if model is None or tokenizer is None:
|
| 44 |
+
return None, {"error": "المحرك في وضع الخمول. تأكدي من سلامة ملفات الـ Tokenizer في المستودع."}
|
| 45 |
|
| 46 |
output_path = os.path.join(APP_DIR, "output_song.wav")
|
| 47 |
+
print("🧠 جاري صهر المدخلات وتوليد التوكنز اللحنية أوفلاين...")
|
| 48 |
|
| 49 |
+
# صياغة التلقين بالشكل الهيكلي لـ LeVo
|
| 50 |
full_prompt = f"<lyric>{lyric}</lyric>"
|
| 51 |
if description and description.strip() != "":
|
| 52 |
full_prompt = f"<description>{description}</description> " + full_prompt
|
| 53 |
|
| 54 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 55 |
|
| 56 |
+
# إطلاق عملية التوليد الفعلي من أوزان الـ 32GB
|
| 57 |
with torch.no_grad():
|
| 58 |
output_tokens = model.generate(
|
| 59 |
**inputs,
|
| 60 |
+
max_new_tokens=256,
|
| 61 |
temperature=float(temperature),
|
| 62 |
do_sample=True
|
| 63 |
)
|
| 64 |
|
| 65 |
+
print("🎵 معالجة المصفوفات وحفظ دفق الصوت الحقيقي...")
|
| 66 |
+
generated_data = output_tokens[0].cpu().float().numpy()
|
| 67 |
|
| 68 |
+
# تسوية الموجة الصوتية خطياً PCM لمنع الانهيار
|
| 69 |
+
audio_waveform = generated_data / (np.max(np.abs(generated_data)) + 1e-5)
|
|
|
|
| 70 |
|
| 71 |
+
# حفظ الأغنية بالتردد القياسي للموديل
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
torchaudio.save(output_path, torch.tensor(audio_waveform).unsqueeze(0), 32000)
|
| 73 |
|
| 74 |
+
return output_path, {"status": "🎯 تم التوليد بنجاح سيادي كامل!", "device": str(model.device)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
except Exception as err:
|
| 77 |
+
print(f"❌ خطأ أثناء التوليد: {str(err)}")
|
| 78 |
return None, {"error": str(err)}
|
| 79 |
|
| 80 |
+
# بناء الواجهة الرسومية الثابتة والمستقرة
|
| 81 |
with gr.Blocks(title="Novix Sovereign Studio") as demo:
|
| 82 |
gr.Markdown("# 🎵 استوديو Novix المستقل والمملوك لك بالكامل 100%")
|
| 83 |
+
gr.Markdown("🛡️ تم استبدال الـ Processor بـ Tokenizer الحقيقي للموديل. الاستوديو جاهز كلياً للإنتاج والربح.")
|
| 84 |
|
| 85 |
with gr.Row():
|
| 86 |
with gr.Column():
|