import torch import scipy import tempfile import os import uuid from pathlib import Path from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse, FileResponse, JSONResponse import uvicorn from diffusers import AudioLDMPipeline print("Loading AudioLDM Small model...") pipe = AudioLDMPipeline.from_pretrained( "cvssp/audioldm-s-full-v2", torch_dtype=torch.float32, ) print("Model loaded successfully!") app = FastAPI() OUTPUT_DIR = Path(tempfile.gettempdir()) / "sfx_outputs" OUTPUT_DIR.mkdir(exist_ok=True) HTML_PAGE = """ 🔊 AudioLDM - Sound Effects Generator

🔊 AudioLDM SFX Generator

Generate sound effects from text using AudioLDM (fast model)

1s10s
10 (fast)50 (quality)

⬇ Download WAV

Examples (click to fill):

cinematic whoosh bass impact footsteps gravel thunder rain glitch beep sword slash car engine ocean waves
""" @app.get("/", response_class=HTMLResponse) async def index(): return HTML_PAGE @app.post("/api/generate") async def generate(request: Request): import time body = await request.json() prompt = body.get("prompt", "").strip() if not prompt: return JSONResponse({"detail": "Prompt is required"}, status_code=400) negative_prompt = body.get("negative_prompt", "Low quality, distorted.") duration = min(max(int(body.get("duration", 3)), 1), 10) steps = min(max(int(body.get("steps", 20)), 10), 50) start = time.time() audio = pipe( prompt=prompt, negative_prompt=negative_prompt, num_inference_steps=steps, audio_length_in_s=float(duration), guidance_scale=2.5, num_waveforms_per_prompt=1, ).audios[0] gen_time = time.time() - start filename = f"sfx_{uuid.uuid4().hex[:8]}.wav" out_path = OUTPUT_DIR / filename scipy.io.wavfile.write(str(out_path), rate=16000, data=audio) return {"url": f"/files/{filename}", "generation_time": gen_time} @app.get("/files/{filename}") async def get_file(filename: str): filepath = OUTPUT_DIR / filename if not filepath.exists(): return JSONResponse({"detail": "File not found"}, status_code=404) return FileResponse(str(filepath), media_type="audio/wav", filename=filename) if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)