Spaces:
Running on Zero
Running on Zero
Download tests/test_profiles_and_queue.py from Simzy/Wan-2.2-Studio: direct link, hf CLI and curl.
- Browser
- Download file 5.33 kB
-
https://huggingface.co/spaces/Simzy/Wan-2.2-Studio/resolve/main/tests/test_profiles_and_queue.py
- Command line
-
hf download hf://spaces/Simzy/Wan-2.2-Studio/tests/test_profiles_and_queue.py
-
curl -L -o test_profiles_and_queue.py https://huggingface.co/spaces/Simzy/Wan-2.2-Studio/resolve/main/tests/test_profiles_and_queue.py
5.33 kB
| """CPU-only regression checks without loading Wan model weights.""" | |
| import ast | |
| import json | |
| import math | |
| import os | |
| from pathlib import Path | |
| import re | |
| import tempfile | |
| import time | |
| import types | |
| import unittest | |
| from prompt_assistant import resolve_writer | |
| SOURCE = Path(__file__).resolve().parents[1] / "app.py" | |
| TREE = ast.parse(SOURCE.read_text(encoding="utf-8")) | |
| def assigned(node, name): | |
| return isinstance(node, ast.Assign) and any( | |
| isinstance(target, ast.Name) and target.id == name for target in node.targets) | |
| def isolated_namespace(directory): | |
| start = next(i for i, node in enumerate(TREE.body) if assigned(node, "SETTINGS_KEYS")) | |
| end = next(i for i, node in enumerate(TREE.body) if assigned(node, "_FILE_DROPDOWN_KEYS")) | |
| functions = {"_profile_path", "list_profiles", "save_profile", "load_profile", | |
| "export_settings", "_settle_choice", "_restore_updates", "_queue_status", "add_to_queue", | |
| "get_num_frames", "_scene_duration"} | |
| nodes = TREE.body[start:end + 1] + [node for node in TREE.body | |
| if isinstance(node, ast.FunctionDef) and node.name in functions] | |
| namespace = { | |
| "gr": types.SimpleNamespace(update=lambda **kwargs: {"__type__": "update", **kwargs}), | |
| "os": os, "re": re, "json": json, "time": time, "tempfile": tempfile, | |
| "PROFILES_DIR": directory, "NO_PROFILE": "none", "REMOTE_SPACE": "owner/writer", | |
| "resolve_writer": resolve_writer, | |
| "np": types.SimpleNamespace(isfinite=math.isfinite), | |
| "_scene_count": int, "_runtime_event": lambda *a, **k: None, | |
| "FIXED_FPS": 16, "MIN_FRAMES_MODEL": 8, "MAX_FRAMES_MODEL": 321, | |
| "MIN_DURATION": 0.5, "MAX_DURATION": 20.1, | |
| } | |
| exec(compile(ast.Module(nodes, type_ignores=[]), str(SOURCE), "exec"), namespace) | |
| return namespace | |
| class ProfileAndQueueTests(unittest.TestCase): | |
| def setUp(self): | |
| self.directory = tempfile.TemporaryDirectory() | |
| self.addCleanup(self.directory.cleanup) | |
| self.ns = isolated_namespace(self.directory.name) | |
| def test_minimum_clip_stays_within_model_frame_bounds(self): | |
| frames = self.ns["get_num_frames"](0.5) | |
| self.assertEqual(frames, 9) | |
| self.assertEqual(frames % 4, 1) | |
| self.assertGreaterEqual(frames, self.ns["MIN_FRAMES_MODEL"]) | |
| self.assertEqual(frames / 16, self.ns["_scene_duration"](0.5)) | |
| self.assertEqual(self.ns["get_num_frames"](0.0), 9) | |
| self.assertEqual(self.ns["get_num_frames"](0.49), 9) | |
| self.assertEqual(self.ns["get_num_frames"](3.5), 53) | |
| self.assertEqual(self.ns["get_num_frames"](20.1), 321) | |
| self.assertEqual(self.ns["get_num_frames"](30.0), 321) | |
| def test_cfg_round_trip_in_named_and_downloaded_profiles(self): | |
| fields = next(node.value for node in ast.walk(TREE) if assigned(node, "_SETTINGS_FIELDS")) | |
| field_names = [node.id for node in fields.elts] | |
| keys = self.ns["SETTINGS_KEYS"] | |
| self.assertEqual(len(keys), len(field_names)) | |
| mapping = dict(zip(field_names, keys)) | |
| self.assertEqual(mapping.get("guidance_scale_input"), "guidance_scale") | |
| self.assertEqual(mapping.get("guidance_scale_2_input"), "guidance_scale_2") | |
| values = [2.5 if name == "guidance_scale_input" else | |
| 1.5 if name == "guidance_scale_2_input" else None for name in field_names] | |
| self.ns["save_profile"]("cfg-regression", *values) | |
| loaded = self.ns["load_profile"]("cfg-regression") | |
| for key, expected in (("guidance_scale", 2.5), ("guidance_scale_2", 1.5)): | |
| self.assertEqual(loaded[keys.index(key)]["value"], expected) | |
| exported = self.ns["export_settings"](*values)["value"] | |
| self.addCleanup(lambda: os.path.exists(exported) and os.remove(exported)) | |
| payload = json.loads(Path(exported).read_text(encoding="utf-8")) | |
| self.assertEqual(payload["guidance_scale"], 2.5) | |
| self.assertEqual(payload["guidance_scale_2"], 1.5) | |
| def test_old_profile_without_cfg_still_loads(self): | |
| updates = self.ns["_restore_updates"]({"prompt": "An older saved setup"}) | |
| self.assertEqual(updates[0]["value"], "An older saved setup") | |
| for key in ("guidance_scale", "guidance_scale_2"): | |
| self.assertEqual(updates[self.ns["SETTINGS_KEYS"].index(key)], {"__type__": "update"}) | |
| def test_failed_stitch_keeps_new_clip_for_retry(self): | |
| first = Path(self.directory.name) / "first.mp4" | |
| second = Path(self.directory.name) / "second.mp4" | |
| first.write_bytes(b"first original") | |
| second.write_bytes(b"second original") | |
| old_queue = [str(first)] | |
| def failed_stitch(paths, name): | |
| self.assertEqual(paths, [str(first), str(second)]) | |
| raise RuntimeError("simulated ffmpeg failure") | |
| self.ns["concat_videos"] = failed_stitch | |
| result = self.ns["add_to_queue"](str(second), old_queue, "my-scene") | |
| self.assertEqual(result[:2], (None, None)) | |
| self.assertEqual(result[2], [str(first), str(second)]) | |
| self.assertIn("retry", result[3].lower()) | |
| self.assertEqual(old_queue, [str(first)]) | |
| self.assertEqual(first.read_bytes(), b"first original") | |
| self.assertEqual(second.read_bytes(), b"second original") | |
| if __name__ == "__main__": | |
| unittest.main() | |