import spaces
import torch
import torchaudio
from einops import rearrange
from stable_audio_tools import get_pretrained_model
from stable_audio_tools.inference.generation import generate_diffusion_cond
from huggingface_hub import cached_download, hf_hub_url
from transformers import AutoModelForAudioClassification
import os

token = os.environ.get("TOKEN")
model_name = "stabilityai/stable-audio-open-1.0"
model_config_url = hf_hub_url(repo_id=model_name, revision="main", filename="model_config.json")
model_config = cached_download(model_config_url, use_auth_token=token)

model = AutoModelForAudioClassification.from_pretrained(
    model_name, 
    cache_dir=None,
    use_auth_token=token
)
sample_rate = model_config["sample_rate"]
sample_size = model_config["sample_size"]

device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device)

# --- Gradio App ---

def generate_music(prompt, seconds_total, bpm, genre):
    """Generates music from a prompt using Stable Diffusion."""

    # Set up text and timing conditioning
    conditioning = [{
        "prompt": f"{bpm} BPM {genre} {prompt}",
        "seconds_start": 0, 
        "seconds_total": seconds_total
    }]

    # Generate stereo audio
    output = generate_diffusion_cond(
        model,
        steps=100,
        cfg_scale=7,
        conditioning=conditioning,
        sample_size=sample_size,
        sigma_min=0.3,
        sigma_max=500,
        sampler_type="dpmpp-3m-sde",
        device=device
    )

    # Rearrange audio batch to a single sequence
    output = rearrange(output, "b d n -> d (b n)")

    # Peak normalize, clip, convert to int16, and save to file
    output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
    return output

@spaces.GPU(duration=120)
def generate_music_and_save(prompt, seconds_total, bpm, genre):
    """Generates music, saves it to a file, and returns the file path."""

    output = generate_music(prompt, seconds_total, bpm, genre)
    filename = "output.wav"
    torchaudio.save(filename, output, sample_rate)
    return filename

# Create Gradio interface
iface = spaces.Interface(
    generate_music_and_save,
    inputs=[
        spaces.Textbox(label="Prompt (e.g., 'upbeat drum loop')", lines=1),
        spaces.Slider(label="Duration (seconds)", minimum=1, maximum=60, step=1),
        spaces.Slider(label="BPM", minimum=60, maximum=200, step=1),
        spaces.Dropdown(label="Genre", choices=["pop", "rock", "hip hop", "electronic", "classical"], value="pop")
    ],
    outputs=[
        spaces.Audio(label="Generated Music")
    ],
    title="Stable Audio Open",
    description="Generate music from text prompts using Stable Audio."
)

iface.launch(share=True)