Spaces:
Running on Zero
Running on Zero
Download tests/test_zero_gpu.py from paleocybernetic/rickroll: direct link, hf CLI and curl.
- Browser
- Download file 4.65 kB
-
https://huggingface.co/spaces/paleocybernetic/rickroll/resolve/main/tests/test_zero_gpu.py
- Command line
-
hf download hf://spaces/paleocybernetic/rickroll/tests/test_zero_gpu.py
-
curl -L -o test_zero_gpu.py https://huggingface.co/spaces/paleocybernetic/rickroll/resolve/main/tests/test_zero_gpu.py
4.65 kB
| """Gradio Server and synchronous ZeroGPU generation contracts.""" | |
| from __future__ import annotations | |
| import io | |
| import os | |
| import tempfile | |
| import unittest | |
| import wave | |
| from pathlib import Path | |
| from types import SimpleNamespace | |
| # Importing app builds the module-level hosted app, whose storage empties its | |
| # OUTPUT_DIR on start. Never let a test run wipe the developer's ./outputs. | |
| os.environ["OUTPUT_DIR"] = tempfile.mkdtemp(prefix="rickroll-test-outputs-") | |
| try: | |
| from app import create_zero_gpu_app | |
| except ModuleNotFoundError as exc: # optional hosted dependencies are absent locally | |
| raise unittest.SkipTest(f"Hosted Gradio dependencies unavailable: {exc}") from exc | |
| from fastapi.testclient import TestClient | |
| from backend.storage import StorageError | |
| class _AudioRuntime: | |
| def __init__(self) -> None: | |
| self.last_request: dict | None = None | |
| def initialize(self) -> None: | |
| return None | |
| def info(self) -> dict: | |
| return { | |
| "ready": True, | |
| "model": {"loaded": True}, | |
| "vae": {"loaded": True}, | |
| "adapter": {"loaded": True}, | |
| } | |
| def generate( | |
| self, | |
| lyrics: str, | |
| style: str, | |
| stage: Path, | |
| progress, | |
| *, | |
| duration_seconds: int = 60, | |
| input_audio_path: Path | None = None, | |
| ) -> dict: | |
| self.last_request = { | |
| "style": style, | |
| "duration_seconds": duration_seconds, | |
| "input_audio": input_audio_path.read_bytes() if input_audio_path else None, | |
| } | |
| progress({"stage": "synthesizing", "percent": 50}) | |
| output = Path(stage) / "audio.wav" | |
| with wave.open(str(output), "wb") as audio: | |
| audio.setnchannels(2) | |
| audio.setsampwidth(2) | |
| audio.setframerate(48_000) | |
| audio.writeframes(bytes(48_000 * 2 * 2 // 10)) | |
| return { | |
| "audio_path": str(output), | |
| "metadata": { | |
| "requested_duration": duration_seconds, | |
| "generation_mode": "text", | |
| }, | |
| } | |
| class ZeroGpuAppTest(unittest.TestCase): | |
| def test_gradio_transport_runs_and_publishes_for_the_cookie_session(self) -> None: | |
| with tempfile.TemporaryDirectory() as directory: | |
| runtime = _AudioRuntime() | |
| app = create_zero_gpu_app(runtime=runtime, output_dir=directory) | |
| with TestClient(app, base_url="https://testserver") as client: | |
| session_response = client.get("/api/v1/tracks") | |
| health = client.get("/api/v1/health").json() | |
| request = SimpleNamespace( | |
| headers={"host": "testserver", "origin": "https://testserver"}, | |
| cookies={"yue_session": client.cookies.get("yue_session")}, | |
| ) | |
| job = app.state.zero_gpu_generate("Neon signal", "Cyber synth", 90, request) | |
| track = client.get(f"/api/v1/tracks/{job['track_id']}") | |
| self.assertEqual(session_response.status_code, 200) | |
| self.assertEqual(health["generation_transport"], "gradio") | |
| self.assertTrue(health["ready"]) | |
| self.assertEqual(job["status"], "completed") | |
| self.assertEqual(track.status_code, 200) | |
| self.assertEqual(track.json()["lyrics"], "Neon signal") | |
| self.assertEqual(track.json()["metadata"]["requested_duration"], 90) | |
| self.assertEqual(runtime.last_request["duration_seconds"], 90) | |
| self.assertEqual(runtime.last_request["style"], "Cyber synth") | |
| def test_worker_artifact_is_published_only_by_parent_state(self) -> None: | |
| with tempfile.TemporaryDirectory() as directory: | |
| app = create_zero_gpu_app(runtime=_AudioRuntime(), output_dir=directory) | |
| with TestClient(app, base_url="https://testserver"): | |
| session, _ = app.state.session_manager.get_or_create(None) | |
| job = app.state.jobs.submit(session_id=session.id, lyrics="Parent index", style="Cyber synth") | |
| job, stage = app.state.jobs.begin(job.id, session.id) | |
| result = app.state.jobs.generate_artifact(stage, job.lyrics, job.style) | |
| self.assertEqual(app.state.storage.list(session.id), []) | |
| completed = app.state.jobs.complete(job.id, session.id, stage, result) | |
| stored = app.state.storage.get(completed.track_id, session.id) | |
| self.assertEqual(completed.status, "completed") | |
| self.assertEqual(stored.payload["lyrics"], "Parent index") | |
| if __name__ == "__main__": | |
| unittest.main() | |