File size: 2,664 Bytes
4a1c368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c968025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a1c368
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import ast
import unittest
from pathlib import Path


APP_PATH = Path(__file__).resolve().parents[1] / "app.py"
KEEP_AWAKE_WORKFLOW_PATH = (
    Path(__file__).resolve().parents[1] / ".github" / "workflows" / "keep-space-awake.yml"
)


class ZeroGPURuntimeContractTests(unittest.TestCase):
    def test_app_declares_a_zero_gpu_function(self) -> None:
        tree = ast.parse(APP_PATH.read_text(encoding="utf-8"))

        imported_modules = {
            alias.name
            for node in tree.body
            if isinstance(node, ast.Import)
            for alias in node.names
        }
        self.assertIn("spaces", imported_modules)

        gpu_functions = [
            node
            for node in tree.body
            if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
            and any(
                isinstance(decorator, ast.Attribute)
                and isinstance(decorator.value, ast.Name)
                and decorator.value.id == "spaces"
                and decorator.attr == "GPU"
                for decorator in node.decorator_list
            )
        ]
        self.assertTrue(gpu_functions)

    def test_zero_gpu_function_is_registered_with_a_gradio_app(self) -> None:
        tree = ast.parse(APP_PATH.read_text(encoding="utf-8"))

        imported_modules = {
            alias.asname or alias.name
            for node in tree.body
            if isinstance(node, ast.Import)
            for alias in node.names
        }
        self.assertIn("gr", imported_modules)

        callback_calls = [
            node
            for node in ast.walk(tree)
            if isinstance(node, ast.Call)
            and isinstance(node.func, ast.Attribute)
            and node.func.attr == "click"
            and any(
                isinstance(argument, ast.Name) and argument.id == "zero_gpu_runtime_probe"
                for argument in node.args
            )
        ]
        self.assertTrue(callback_calls)

        launch_calls = [
            node
            for node in ast.walk(tree)
            if isinstance(node, ast.Call)
            and isinstance(node.func, ast.Attribute)
            and node.func.attr == "launch"
        ]
        self.assertTrue(launch_calls)

    def test_keep_awake_workflow_checks_the_space_every_six_hours(self) -> None:
        self.assertTrue(KEEP_AWAKE_WORKFLOW_PATH.is_file())
        workflow = KEEP_AWAKE_WORKFLOW_PATH.read_text(encoding="utf-8")

        self.assertIn("cron: '17 */6 * * *'", workflow)
        self.assertIn("https://catalna-ginchiee-bot.hf.space/", workflow)
        self.assertIn("curl --fail", workflow)


if __name__ == "__main__":
    unittest.main()