Downloads · 30 days
19
7% of all-time downloads
HRFYTRE45E/TTS-v0x
TTS-v0x is a machine learning model from HRFYTRE45E. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
Downloads · 30 days
19
7% of all-time downloads
All-time downloads
268
Public
Parameters
507M
7.3 GB on disk
Likes
1
Public
Click a slice to open those files.
.safetensors4.7 GB · 78%
From the Hugging Face model README
import os
import sys
import re
import subprocess
from huggingface_hub import login, snapshot_download
# ضع التوكن الخاص بك هنا لتشغيل الكود
TOKEN = "YOUR_HF_TOKEN_HERE"
login(token=TOKEN)
required_packages = [
"unsloth", "torch", "torchaudio", "soundfile",
"huggingface_hub", "transformers", "accelerate",
"omegaconf", "einx"
]
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q"] + required_packages)
if not os.path.exists("Spark-TTS"):
subprocess.check_call(["git", "clone", "-q", "[https://github.com/SparkAudio/Spark-TTS.git](https://github.com/SparkAudio/Spark-TTS.git)"])
spark_path = os.path.abspath("Spark-TTS")
if spark_path not in sys.path:
sys.path.append(spark_path)
import torch
import numpy as np
import soundfile as sf
from sparktts.models.audio_tokenizer import BiCodecTokenizer
from unsloth import FastModel
MODEL_ID = "HRFYTRE45E/TTS-v0x"
device = "cuda" if torch.cuda.is_available() else "cpu"
SNAPSHOT_DIR = "Spark-TTS-0.5B"
if not os.path.exists(os.path.join(SNAPSHOT_DIR, "config.yaml")):
SNAPSHOT_DIR = snapshot_download("SparkAudio/Spark-TTS-0.5B", local_dir=SNAPSHOT_DIR, token=TOKEN)
audio_tokenizer = BiCodecTokenizer(SNAPSHOT_DIR, device="cpu")
if device == "cuda":
audio_tokenizer.model.to(device)
print(f"جاري تحميل أوزان نموذجك الخاص من {MODEL_ID}...")
model, tokenizer = FastModel.from_pretrained(
model_name=MODEL_ID,
max_seq_length=2048,
dtype=torch.float32,
load_in_4bit=False,
token=TOKEN,
)
model.eval()
@torch.inference_mode()
def generate_audio(text, output_path="output.wav"):
cleaned_text = re.sub(r"\s+", " ", str(text)).strip()
prompt = f"<|task_tts|><|start_content|>{cleaned_text}<|end_content|><|start_global_token|>"
inputs = tokenizer(prompt, return_tensors="pt").to(device)
print("جاري توليد الصوت...")
generated_ids = model.generate(
**inputs,
max_new_tokens=2048,
do_sample=True,
temperature=0.30,
top_k=50,
top_p=0.95,
use_cache=True,
eos_token_id=tokenizer.convert_tokens_to_ids("<|im_end|>"),
pad_token_id=tokenizer.eos_token_id,
)
new_tokens = generated_ids[:, inputs.input_ids.shape[1]:]
predicts_text = tokenizer.batch_decode(new_tokens, skip_special_tokens=False)[0]
global_matches = re.findall(r"<\|bicodec_global_(\d+)\|>", predicts_text)
semantic_matches = re.findall(r"<\|bicodec_semantic_(\d+)\|>", predicts_text)
if not global_matches or not semantic_matches:
raise ValueError("فشل في توليد توكنات صوتية صالحة.")
pred_global_ids = torch.tensor([int(x) for x in global_matches], dtype=torch.long, device=device).unsqueeze(0)
pred_semantic_ids = torch.tensor([int(x) for x in semantic_matches], dtype=torch.long, device=device).unsqueeze(0)
wav = audio_tokenizer.detokenize(pred_global_ids, pred_semantic_ids)
if isinstance(wav, torch.Tensor):
wav = wav.detach().cpu().to(torch.float32).numpy()
wav = np.squeeze(wav).astype(np.float32)
sample_rate = int(audio_tokenizer.config.get("sample_rate", 16000))
sf.write(output_path, wav, sample_rate)
print(f"✓ تم حفظ الملف الصوتي بنجاح في: {os.path.abspath(output_path)}")
generate_audio("أهلاً بك، تم تشغيل نموذجك الخاص بنجاح.")