YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

TTS-v0x (Private Model) ๐ŸŽ™๏ธโšก

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("ุฃู‡ู„ุงู‹ ุจูƒุŒ ุชู… ุชุดุบูŠู„ ู†ู…ูˆุฐุฌูƒ ุงู„ุฎุงุต ุจู†ุฌุงุญ.")
Downloads last month
165
Safetensors
Model size
0.5B params
Tensor type
F32
ยท
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support