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", )