File size: 2,970 Bytes
d8053fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
import gradio as gr
from enum import Enum
from database import is_space_existing, is_space_registered, update_status


TITLE = "⚙️ Spaces CI Bot ⚙️"
DESCRIPTION = """
This app lets you register your Space with the Spaces CI Bot.

Once your repository is watched, any PR opened on your Space will be deployed as a temporary Space to test the changes
on your demo. Any changes pushed to the PRs will trigger a re-deployment. Once the PR is merged, the temporary Space is
deleted.

If your app needs some secrets to run or a specific hardware, you will need to duplicate the temporary Space and to
setup your environment.
"""


class Action(Enum):
    REGISTER = "Enable CI Bot"
    UNREGISTER = "Disable CI Bot"
    CHECK_STATUS = "Check status"


def gradio_fn(space_id: str, action: str) -> str:
    if not is_space_existing(space_id):
        return f"""## Error
        Could not find Space '**{space_id}**' on the Hub.
        Please make sure you are trying to register a public repository.
        """

    registered = is_space_registered(space_id)
    if action == Action.REGISTER.value:
        if registered:
            return f"""## Did nothing
            The Space '**{space_id}**' is already in the watchlist. Any PR opened on
            this repository will trigger an ephemeral Space.
            """
        else:
            update_status(space_id, should_watch=True)
            return f"""## Success
            The Space '**{space_id}**' has been added to the watchlist. Any PR opened on
            this repository will trigger an ephemeral Space.
            """
    elif action == Action.UNREGISTER.value:
        if not registered:
            return f"""## Did nothing
            The Space '**{space_id}**' is currently not in the watchlist.
            """
        else:
            update_status(space_id, should_watch=False)
            return f"""## Success
            The Space '**{space_id}**' has been removed from the watchlist.
            """
    elif action == Action.CHECK_STATUS.value:
        if registered:
            return f"""## Watched
            The Space '**{space_id}**' is already in the watchlist. Any PR opened on
            this repository will trigger an ephemeral Space.
            """
        else:
            return f"""## Not watched
            The Space '**{space_id}**' is currently not in the watchlist.
            """
    else:
        return f"**Error:** action {action} not implemented."


def generate_ui() -> gr.Blocks:
    return gr.Interface(
        fn=gradio_fn,
        inputs=[
            gr.Textbox(lines=1, placeholder="username/my_cool_space", label="Space ID"),
            gr.Radio(
                [action.value for action in Action],
                value=Action.REGISTER.value,
                label="What should I do?",
            ),
        ],
        outputs=[gr.Markdown()],
        title=TITLE,
        description=DESCRIPTION,
        allow_flagging="never",
    )