Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,95 @@
|
|
1 |
-
import os
|
2 |
-
import requests
|
3 |
-
import gradio as gr
|
4 |
-
|
5 |
-
|
6 |
-
def create_github_release(
|
7 |
-
owner: str,
|
8 |
-
repo: str,
|
9 |
-
token: str,
|
10 |
-
release_tag: str,
|
11 |
-
release_name: str,
|
12 |
-
release_description: str,
|
13 |
-
file_path: str,
|
14 |
-
):
|
15 |
-
file_name = os.path.basename(file_path)
|
16 |
-
try:
|
17 |
-
release_response = requests.post(
|
18 |
-
f"https://api.github.com/repos/{owner}/{repo}/releases",
|
19 |
-
headers={
|
20 |
-
"Authorization": f"token {token}",
|
21 |
-
"Accept": "application/vnd.github.v3+json",
|
22 |
-
},
|
23 |
-
json={
|
24 |
-
"tag_name": release_tag,
|
25 |
-
"name": release_name,
|
26 |
-
"body": release_description,
|
27 |
-
"draft": False,
|
28 |
-
"prerelease": False,
|
29 |
-
},
|
30 |
-
)
|
31 |
-
if release_response.status_code != 201:
|
32 |
-
return f"Failed to create release: {release_response.status_code}, {release_response.json()}"
|
33 |
-
# 获取上传 URL
|
34 |
-
release = release_response.json()
|
35 |
-
upload_url = release["upload_url"].split("{")[0]
|
36 |
-
with open(file_path, "rb") as binary_file:
|
37 |
-
upload_response = requests.post(
|
38 |
-
f"{upload_url}?name={file_name}",
|
39 |
-
headers={
|
40 |
-
"Authorization": f"token {token}",
|
41 |
-
"Content-Type": "application/octet-stream",
|
42 |
-
},
|
43 |
-
data=binary_file,
|
44 |
-
)
|
45 |
-
|
46 |
-
if upload_response.status_code == 201:
|
47 |
-
return f"Binary file '{file_name}' uploaded successfully!"
|
48 |
-
else:
|
49 |
-
return f"Failed to upload binary file: {upload_response.status_code}, {upload_response.json()}"
|
50 |
-
|
51 |
-
except Exception as e:
|
52 |
-
return f"Release failed: {e}"
|
53 |
-
|
54 |
-
|
55 |
-
if __name__ == "__main__":
|
56 |
-
gr.Interface(
|
57 |
-
fn=create_github_release,
|
58 |
-
inputs=[
|
59 |
-
gr.Textbox(
|
60 |
-
label="GitHub Owner",
|
61 |
-
placeholder="username / organization name",
|
62 |
-
show_copy_button=True,
|
63 |
-
),
|
64 |
-
gr.Textbox(
|
65 |
-
label="GitHub Repo",
|
66 |
-
placeholder="repo name",
|
67 |
-
show_copy_button=True,
|
68 |
-
),
|
69 |
-
gr.Textbox(
|
70 |
-
label="GitHub Token",
|
71 |
-
placeholder="personal access token",
|
72 |
-
type="password",
|
73 |
-
),
|
74 |
-
gr.Textbox(
|
75 |
-
label="Release Tag",
|
76 |
-
placeholder="v1.0.0",
|
77 |
-
show_copy_button=True,
|
78 |
-
),
|
79 |
-
gr.Textbox(
|
80 |
-
label="Release Name",
|
81 |
-
placeholder="My Binary Release",
|
82 |
-
show_copy_button=True,
|
83 |
-
),
|
84 |
-
gr.TextArea(
|
85 |
-
label="Describe this release",
|
86 |
-
placeholder="Release with binary file
|
87 |
-
show_copy_button=True,
|
88 |
-
),
|
89 |
-
gr.File(label="Binary File"),
|
90 |
-
],
|
91 |
-
outputs=gr.TextArea(label="Status", show_copy_button=True),
|
92 |
-
title="GitHub Release Creator",
|
93 |
-
description="Upload a binary file to create a new GitHub release.",
|
94 |
-
flagging_mode="never",
|
95 |
-
).launch()
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
|
6 |
+
def create_github_release(
|
7 |
+
owner: str,
|
8 |
+
repo: str,
|
9 |
+
token: str,
|
10 |
+
release_tag: str,
|
11 |
+
release_name: str,
|
12 |
+
release_description: str,
|
13 |
+
file_path: str,
|
14 |
+
):
|
15 |
+
file_name = os.path.basename(file_path)
|
16 |
+
try:
|
17 |
+
release_response = requests.post(
|
18 |
+
f"https://api.github.com/repos/{owner}/{repo}/releases",
|
19 |
+
headers={
|
20 |
+
"Authorization": f"token {token}",
|
21 |
+
"Accept": "application/vnd.github.v3+json",
|
22 |
+
},
|
23 |
+
json={
|
24 |
+
"tag_name": release_tag,
|
25 |
+
"name": release_name,
|
26 |
+
"body": release_description,
|
27 |
+
"draft": False,
|
28 |
+
"prerelease": False,
|
29 |
+
},
|
30 |
+
)
|
31 |
+
if release_response.status_code != 201:
|
32 |
+
return f"Failed to create release: {release_response.status_code}, {release_response.json()}"
|
33 |
+
# 获取上传 URL
|
34 |
+
release = release_response.json()
|
35 |
+
upload_url = release["upload_url"].split("{")[0]
|
36 |
+
with open(file_path, "rb") as binary_file:
|
37 |
+
upload_response = requests.post(
|
38 |
+
f"{upload_url}?name={file_name}",
|
39 |
+
headers={
|
40 |
+
"Authorization": f"token {token}",
|
41 |
+
"Content-Type": "application/octet-stream",
|
42 |
+
},
|
43 |
+
data=binary_file,
|
44 |
+
)
|
45 |
+
|
46 |
+
if upload_response.status_code == 201:
|
47 |
+
return f"Binary file '{file_name}' uploaded successfully!"
|
48 |
+
else:
|
49 |
+
return f"Failed to upload binary file: {upload_response.status_code}, {upload_response.json()}"
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
return f"Release failed: {e}"
|
53 |
+
|
54 |
+
|
55 |
+
if __name__ == "__main__":
|
56 |
+
gr.Interface(
|
57 |
+
fn=create_github_release,
|
58 |
+
inputs=[
|
59 |
+
gr.Textbox(
|
60 |
+
label="GitHub Owner",
|
61 |
+
placeholder="username / organization name",
|
62 |
+
show_copy_button=True,
|
63 |
+
),
|
64 |
+
gr.Textbox(
|
65 |
+
label="GitHub Repo",
|
66 |
+
placeholder="repo name",
|
67 |
+
show_copy_button=True,
|
68 |
+
),
|
69 |
+
gr.Textbox(
|
70 |
+
label="GitHub Token",
|
71 |
+
placeholder="personal access token",
|
72 |
+
type="password",
|
73 |
+
),
|
74 |
+
gr.Textbox(
|
75 |
+
label="Release Tag",
|
76 |
+
placeholder="v1.0.0",
|
77 |
+
show_copy_button=True,
|
78 |
+
),
|
79 |
+
gr.Textbox(
|
80 |
+
label="Release Name",
|
81 |
+
placeholder="My Binary Release",
|
82 |
+
show_copy_button=True,
|
83 |
+
),
|
84 |
+
gr.TextArea(
|
85 |
+
label="Describe this release",
|
86 |
+
placeholder="Release with binary file with source code.",
|
87 |
+
show_copy_button=True,
|
88 |
+
),
|
89 |
+
gr.File(label="Binary File"),
|
90 |
+
],
|
91 |
+
outputs=gr.TextArea(label="Status", show_copy_button=True),
|
92 |
+
title="GitHub Release Creator",
|
93 |
+
description="Upload a binary file to create a new GitHub release.",
|
94 |
+
flagging_mode="never",
|
95 |
+
).launch()
|