PaDoc / tests /test_rendering.py
TonyZhan's picture
Deploy parallel streaming Gradio demo
8e8a34f verified
Raw
History Blame Contribute Delete
1.82 kB
from __future__ import annotations
import ast
import json
import re
from pathlib import Path
ROOT = Path(__file__).parents[1]
def _app_functions(*names: str):
source = (ROOT / "app.py").read_text(encoding="utf-8")
module = ast.parse(source)
selected = [
node
for node in module.body
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name in names
]
namespace = {
"json": json,
"_LAYOUT_RE": re.compile(
r"<SP_LAYOUT>(\d+)\s+(\d+)\s+(\d+)\s+(\d+)</SP_LAYOUT>"
),
"_META_RE": re.compile(r"<SP_META>(\{.*?\})</SP_META>"),
}
helper_module = ast.Module(body=selected, type_ignores=[])
exec(compile(helper_module, "app.py", "exec"), namespace)
return namespace
def test_layout_parser_accepts_only_valid_complete_boxes() -> None:
functions = _app_functions("parse_layout_boxes")
parse = functions["parse_layout_boxes"]
text = (
"<SP_LAYOUT>10 20 300 400</SP_LAYOUT>"
"<SP_LAYOUT>300 20 10 400</SP_LAYOUT>"
"<SP_LAYOUT>1 2 3"
)
assert parse(text) == [(10, 20, 300, 400)]
def test_branch_parser_extracts_metadata_and_content() -> None:
functions = _app_functions("parse_branch_text")
parse = functions["parse_branch_text"]
category, content = parse('<SP_META>{"category":"title"}</SP_META>Quarterly report')
assert category == "title"
assert content == "Quarterly report"
def test_space_is_parallel_only_and_streaming() -> None:
source = (ROOT / "app.py").read_text(encoding="utf-8")
assert '@spaces.GPU(duration=120, size="large")' in source
assert 'execution_mode="parallel"' in source
assert 'os.environ.get("SPACES_ZERO_GPU") == "1"' in source
assert "yield (" in source
assert "gr.Radio" not in source