File size: 2,935 Bytes
0fc235e
b0f7b0a
0fc235e
 
ac36ca5
b0f7b0a
ac36ca5
7403ab9
 
ac36ca5
0fc235e
b23c183
 
7403ab9
 
 
 
 
 
 
be2ea82
7403ab9
 
 
 
 
 
0896d8d
7403ab9
 
 
0fc235e
b0f7b0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b23c183
 
 
 
 
 
 
 
 
 
 
 
 
b0f7b0a
b23c183
 
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
86
#!/usr/bin/env python
import json

import gradio as gr
from gradio_space_ci import enable_space_ci
from huggingface_hub import CommitScheduler, hf_hub_download

from open_pr import open_pr

enable_space_ci()

TITLE = "Listen to Pull Requests and start ephemeral Spaces on new PRs! 🚀"

DESCRIPTION = """
## Gradio Space CI is a tool to create ephemeral Spaces for each PR opened on your Space repo.

The goal is to improve developer experience by making the review process as lean as possible.
The app below let you open a PR to enable Space CI on a Space.

The steps are the following:
1. Sign-in with Hugging Face.
2. Input a Space id from the Hub
3. Click "Submit"
4. That's it! You'll get feedback if it works or not, and if it worked, you'll get the URL of the opened PR 🔥

#### ⚠️ Disclaimer: **Gradio Space CI** works only on public Spaces.

For more details about **Gradio Space CI**, checkout [this page](https://huggingface.co/spaces/Wauplin/gradio-space-ci/blob/main/README.md).

If you find any issues, please report here: https://huggingface.co/spaces/Wauplin/gradio-space-ci/discussions
"""

SUCCESS_MESSAGE = """
### Success 🔥

Yay! A PR has been open to enable Space CI on {space_id}. Check it out here: [{pr_url}]({pr_url}).

You can contact the Space owner to let them know about this PR.
"""

ERROR_MESSAGE = """
### Error ❌

An error happened while trying to open a PR to enable Space CI on {space_id}.

{error}"""

DATASET_ID = "Wauplin/gradio-space-ci-report"
csv_path = hf_hub_download(repo_id=DATASET_ID, repo_type="dataset", filename="report.jsonl", local_dir="./report")
scheduler = CommitScheduler(repo_id=DATASET_ID, repo_type="dataset", folder_path="./report")


def append(**kwargs):
    with scheduler.lock:
        with open(csv_path, "a") as f:
            f.write(json.dumps(kwargs) + "\n")


def fn(space_id_or_url: str, oauth_profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None) -> str:
    user = oauth_profile.username if oauth_profile is not None else "???"
    try:
        pr_url = open_pr(space_id_or_url, oauth_token)
        append(space_id=space_id_or_url, status="success", pr_url=pr_url, user=user)
        return SUCCESS_MESSAGE.format(space_id=space_id_or_url, pr_url=pr_url)
    except Exception as e:
        append(space_id=space_id_or_url, status="error", error=str(e), user=user)
        return ERROR_MESSAGE.format(space_id=space_id_or_url, error=str(e))


with gr.Blocks() as blocks:
    gr.Markdown(f"<h1 style='text-align: center; margin-bottom: 1rem'>{TITLE}</h1>")
    gr.Markdown(DESCRIPTION)

    with gr.Row(equal_height=False):
        with gr.Column():
            space_id = gr.Textbox(label="Space ID or URL", lines=1)
            gr.LoginButton()
            submit_btn = gr.Button("Submit", variant="primary")

        with gr.Column():
            output = gr.Markdown()

        submit_btn.click(fn, space_id, output)

blocks.launch()