File size: 8,883 Bytes
e96efee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""
AutoMixAI Beat Generator β€” HuggingFace Space

AI-powered music/beat generation using Meta's MusicGen model.
Generates studio-quality beats, loops, and music from text prompts.

Endpoints:
  POST /generate   Generate beat/music from text prompt
  GET  /output/{id} Download generated audio
  GET  /health     Health check
"""

import os
import uuid
import tempfile
import time
from pathlib import Path

import numpy as np
import soundfile as sf
import torch
from transformers import AutoProcessor, MusicgenForConditionalGeneration

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from pydantic import BaseModel, Field

# ═══════════════════════════════════════════════════════════════════════════════
# CONFIG
# ═══════════════════════════════════════════════════════════════════════════════

OUTPUT_DIR = Path(tempfile.gettempdir()) / "automixai_beats"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

# Model selection: small for speed, medium for quality
MODEL_ID = os.environ.get("MUSICGEN_MODEL", "facebook/musicgen-small")
SAMPLE_RATE = 32000  # MusicGen outputs at 32kHz

# ═══════════════════════════════════════════════════════════════════════════════
# FASTAPI APP
# ═══════════════════════════════════════════════════════════════════════════════

app = FastAPI(
    title="AutoMixAI Beat Generator",
    description="AI-powered beat/music generation using Meta's MusicGen.",
    version="1.0.0",
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ═══════════════════════════════════════════════════════════════════════════════
# SCHEMAS
# ═══════════════════════════════════════════════════════════════════════════════

class GenerateRequest(BaseModel):
    prompt: str = Field(..., min_length=3, max_length=500,
        description="Text prompt describing the beat/music to generate")
    duration: int = Field(default=10, ge=3, le=30,
        description="Duration in seconds (3-30)")
    temperature: float = Field(default=1.0, ge=0.5, le=1.5,
        description="Generation temperature: lower=more predictable, higher=more creative")
    guidance_scale: float = Field(default=3.0, ge=1.0, le=10.0,
        description="How closely to follow the prompt (higher=stricter)")

class GenerateResponse(BaseModel):
    output_file_id: str
    prompt: str
    duration: float
    model: str
    sample_rate: int
    message: str = "Beat generated successfully."

# ═══════════════════════════════════════════════════════════════════════════════
# MUSICGEN MODEL (Lazy-loaded)
# ═══════════════════════════════════════════════════════════════════════════════

_model = None
_processor = None

def _load_model():
    """Lazy-load the MusicGen model and processor."""
    global _model, _processor
    if _model is None:
        print(f"Loading MusicGen model: {MODEL_ID}")
        start = time.time()

        _processor = AutoProcessor.from_pretrained(MODEL_ID)
        _model = MusicgenForConditionalGeneration.from_pretrained(MODEL_ID)

        # Use GPU if available
        device = "cuda" if torch.cuda.is_available() else "cpu"
        _model = _model.to(device)
        if device == "cuda":
            _model = _model.half()  # FP16 for faster GPU inference

        elapsed = time.time() - start
        print(f"MusicGen loaded on {device} in {elapsed:.1f}s")

    return _model, _processor


def generate_music(prompt: str, duration: int = 10, temperature: float = 1.0,
                   guidance_scale: float = 3.0) -> tuple:
    """
    Generate music/beat from text prompt using MusicGen.

    Returns (audio_array, sample_rate)
    """
    model, processor = _load_model()
    device = next(model.parameters()).device

    # Process the prompt
    inputs = processor(
        text=[prompt],
        padding=True,
        return_tensors="pt",
    ).to(device)

    # Calculate max_new_tokens from duration
    # MusicGen generates at ~50 tokens/second at 32kHz
    tokens_per_second = 50
    max_new_tokens = int(duration * tokens_per_second)

    print(f"Generating: '{prompt}' ({duration}s, temp={temperature}, guidance={guidance_scale})")
    start = time.time()

    with torch.no_grad():
        audio_values = model.generate(
            **inputs,
            max_new_tokens=max_new_tokens,
            temperature=temperature,
            guidance_scale=guidance_scale,
            do_sample=True,
        )

    elapsed = time.time() - start
    print(f"Generation complete in {elapsed:.1f}s")

    # Convert to numpy
    audio = audio_values[0, 0].cpu().numpy()

    # Normalize to prevent clipping
    peak = np.max(np.abs(audio))
    if peak > 0:
        audio = audio / peak * 0.95

    return audio, SAMPLE_RATE


# ═══════════════════════════════════════════════════════════════════════════════
# API ROUTES
# ═══════════════════════════════════════════════════════════════════════════════

@app.get("/")
def root():
    return {
        "status": "ok",
        "service": "AutoMixAI Beat Generator v1.0",
        "model": MODEL_ID,
        "features": ["text-to-music", "text-to-beat"],
    }

@app.get("/health")
def health():
    return {"status": "healthy", "model": MODEL_ID}


@app.post("/generate", response_model=GenerateResponse)
async def generate_beat(request: GenerateRequest):
    """Generate a beat/music clip from a text prompt using MusicGen."""
    output_id = uuid.uuid4().hex
    output_path = OUTPUT_DIR / f"{output_id}.wav"

    try:
        audio, sr = generate_music(
            prompt=request.prompt,
            duration=request.duration,
            temperature=request.temperature,
            guidance_scale=request.guidance_scale,
        )

        # Save as WAV
        sf.write(str(output_path), audio, sr, subtype="PCM_16")
        actual_duration = round(len(audio) / sr, 2)

    except Exception as exc:
        import traceback
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=f"Generation failed: {str(exc)}") from exc

    return GenerateResponse(
        output_file_id=output_id,
        prompt=request.prompt,
        duration=actual_duration,
        model=MODEL_ID,
        sample_rate=sr,
    )


@app.get("/output/{file_id}")
async def download_output(file_id: str):
    """Download a generated audio file."""
    output_path = OUTPUT_DIR / f"{file_id}.wav"
    if not output_path.exists():
        raise HTTPException(status_code=404, detail=f"Output '{file_id}' not found.")
    return FileResponse(str(output_path), media_type="audio/wav",
                        filename=f"automix_beat_{file_id}.wav")


# ═══════════════════════════════════════════════════════════════════════════════
# ENTRYPOINT
# ═══════════════════════════════════════════════════════════════════════════════

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)