Spaces:
Running on Zero
Running on Zero
Fix README.md
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215import osimport platformimport sysimport shutilimport tempfileimport tracebackimport torchimport torchaudioimport gradio as grimport spacesfrom fastapi import FastAPIfrom fastapi.responses import JSONResponsefrom einops import rearrangefrom huggingface_hub import loginfrom stable_audio_3 import StableAudioModelfor _stream in (sys.stdout, sys.stderr):if hasattr(_stream, "reconfigure"):_stream.reconfigure(encoding="utf-8", errors="backslashreplace")hf_token = os.environ.get("HF_TOKEN")if hf_token:login(token=hf_token)# GPU touch removed — it was timing out and crashing the Space build.def _log(message):print(message.encode("ascii", "backslashreplace").decode("ascii"), flush=True)def _get_ram_bytes():try:with open("/proc/meminfo", "r", encoding="utf-8") as meminfo:for line in meminfo:if line.startswith("MemTotal:"):return int(line.split()[1]) * 1024except (FileNotFoundError, OSError, ValueError):passreturn None# ---------------------------------------------------------------------------# API metadata# ---------------------------------------------------------------------------API_RESOURCES = {"audio_generation": {"name": "Audio generation","description": "Generate music or sound effects from a text prompt.","endpoint": "/api/audio/generate","method": "POST","input": {"prompt": "string","duration": "number (1-120 seconds)","steps": "integer (1-50)","cfg_scale": "number (0-10)","seed": "integer (-1 for random)","model": "small-music | small-sfx",},"output": "WAV audio file",}}API_SPECS = {"name": "Respite API","version": "1.0.0","description": "General-purpose AI API server with audio generation capabilities.","base_path": "/respite","authentication": "none","content_types": ["application/json", "audio/wav"],"resources_endpoint": "/respite/resources","specs_endpoint": "/respite/specs","limits": {"max_concurrent_requests": 1,"max_queue_size": 4,"audio_max_duration_seconds": 120,},}def _get_runtime_specs():storage = shutil.disk_usage(os.getcwd())return {"platform": platform.platform(),"python_version": platform.python_version(),"cpu_cores": os.cpu_count(),"ram_bytes": _get_ram_bytes(),"storage_total_bytes": storage.total,"storage_used_bytes": storage.used,"storage_free_bytes": storage.free,}# ---------------------------------------------------------------------------# Model cache# ---------------------------------------------------------------------------MODEL_CACHE = {}def load_model(model_name):if model_name not in MODEL_CACHE:_log(f"Loading {model_name} model...")model = StableAudioModel.from_pretrained(model_name, device="cpu")MODEL_CACHE[model_name] = model_log(f"{model_name} loaded successfully!")return MODEL_CACHE[model_name]def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):_log(f"Generating with {model_name}: prompt='{prompt}', "f"duration={duration}s, steps={steps}, cfg={cfg_scale}, seed={seed}")model = load_model(model_name)audio = model.generate(prompt=prompt, duration=duration, steps=steps,cfg_scale=cfg_scale, seed=seed, batch_size=1,)audio = rearrange(audio, "b d n -> d (b n)")audio = audio.to(torch.float32).clamp(-1, 1).mul(32767).to(torch.int16).cpu()output_path = os.path.join(tempfile.gettempdir(),f"stable_audio_{seed}_{hash(prompt) & 0xFFFFFFFF:08x}.wav",)torchaudio.save(output_path, audio, 44100)_log("Generation complete!")return output_path# ---------------------------------------------------------------------------# Gradio UI# ---------------------------------------------------------------------------with gr.Blocks(title="Respite API") as demo:gr.Markdown("# Respite API - Music & SFX Generation")gr.Markdown("Generate music and sound effects using ""Stability AI's Stable Audio 3 Small models.")with gr.Row():with gr.Column():model_name = gr.Dropdown(choices=["small-music", "small-sfx"],value="small-music", label="Model",)prompt = gr.Textbox(label="Prompt",placeholder="Describe the music or sound effect you want to generate...",lines=2,)duration = gr.Slider(minimum=1, maximum=120, value=30, step=1, label="Duration (seconds)")steps = gr.Slider(minimum=1, maximum=50, value=8, step=1, label="Steps")cfg_scale = gr.Slider(minimum=0.0, maximum=10.0, value=1.0, step=0.1, label="CFG Scale")seed = gr.Number(value=-1, label="Seed (-1 for random)")btn = gr.Button("Generate", variant="primary")with gr.Column():audio_output = gr.Audio(label="Generated Audio", type="filepath")btn.click(fn=generate_audio,inputs=[prompt, duration, steps, cfg_scale, seed, model_name],outputs=audio_output,)# ---------------------------------------------------------------------------# Inject discovery routes into Gradio's internal FastAPI server.## The HF Spaces Gradio runner calls demo.launch() directly, so our own# FastAPI app is never served. We monkey-patch Blocks.launch so that# AFTER Gradio creates its server, we inject routes onto that server.# ---------------------------------------------------------------------------_original_blocks_launch = gr.Blocks.launchdef _patched_blocks_launch(self, *args, **kwargs):_log("Intercepted Blocks.launch() - calling original...")try:result = _original_blocks_launch(self, *args, **kwargs)except Exception:_log("ERROR in original launch:")_log(traceback.format_exc())raiseif self is demo:try:fa_app = getattr(self, "app", None)_log(f"demo.app is: {fa_app}")if fa_app is not None:@fa_app.get("/respite/resources")def _resources():return JSONResponse({"resources": API_RESOURCES})@fa_app.get("/respite/specs")def _specs():return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})_log("Respite API routes injected successfully")else:_log("Warning: demo.app is None after launch")except Exception:_log("ERROR injecting routes:")_log(traceback.format_exc())return resultgr.Blocks.launch = _patched_blocks_launchdemo.queue(max_size=4, default_concurrency_limit=1)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647---title: Respite APIemoji: 🤖colorFrom: purplecolorTo: bluesdk: gradiosdk_version: 6.3.0app_file: app.pypinned: false---# Respite APIA Hugging Face Space for generating music using [Stability AI's Stable Audio 3](https://github.com/Stability-AI/stable-audio-3) Small model.## Features- **Model**: `small-music` (433M parameters)- **Duration**: 1–120 seconds- **Sample rate**: 44.1 kHz stereo- Gradio 6.3.0 interface## API discovery endpointsRespite API exposes machine-readable API metadata alongside the Gradio UI:- `GET /respite/resources` lists callable capabilities, including each resource's method, endpoint, inputs, and output.- `GET /respite/specs` lists server-wide specifications such as version, supported content types, limits, discovery URLs, CPU cores, total RAM, and storage total/used/free space.Both endpoints return JSON and currently require no authentication. The metadata is designed to grow as additional AI capabilities are added.## Usage1. Enter a text prompt describing the music you want2. Adjust duration, steps, CFG scale, and seed3. Click **Generate** and wait for the audio## Setup```bashpip install -r requirements.txtpython app.py```## LicenseThis project uses the Stable Audio 3 model. Please refer to Stability AI's [license](https://huggingface.co/stabilityai/stable-audio-3-small) for usage terms.
README.md+33-201