Spaces:
Running
Running
Commit
•
8334186
1
Parent(s):
5c7223b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
import requests
|
6 |
+
import time
|
7 |
+
|
8 |
+
from github import Github, GithubIntegration
|
9 |
+
|
10 |
+
import gradio as gr
|
11 |
+
|
12 |
+
|
13 |
+
app_id = '307916'
|
14 |
+
owner='duerrsimon'
|
15 |
+
repo_name='bioicons-test'
|
16 |
+
|
17 |
+
# with open('bot_key.pem','r') as cert_file:
|
18 |
+
# app_key = cert_file.read()
|
19 |
+
|
20 |
+
app_key = os.environ.get('GITHUB_TOKEN')
|
21 |
+
|
22 |
+
git_integration = GithubIntegration(
|
23 |
+
app_id,
|
24 |
+
app_key,
|
25 |
+
)
|
26 |
+
api = Github(
|
27 |
+
login_or_token=git_integration.get_access_token(
|
28 |
+
git_integration.get_repo_installation(owner, repo_name).id
|
29 |
+
).token
|
30 |
+
)
|
31 |
+
|
32 |
+
#api = Github(token)
|
33 |
+
site = api.get_repo('duerrsimon/bioicons-test')
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
licenses = ['mit', 'cc-by-3.0', 'cc-0', 'cc-by-4.0', 'bsd', 'cc-by-sa-3.0', 'cc-by-sa-4.0']
|
38 |
+
|
39 |
+
categories = ["All_icons", "Machine_Learning", "Genetics", "Chemistry", "Scientific_graphs", "Cell_membrane", "Microbiology", "Oncology", "Molecular_modelling", "Intracellular_components", "Cell_types", "Cell_lines", "Plants_Algae", "Chemo-_and_Bioinformatics", "Cell_culture", "Tissues", "Computer_hardware", "Nucleic_acids", "General_items", "Safety_symbols", "Receptors_channels", "Human_physiology", "Animals", "Lab_apparatus", "Blood_Immunology", "Viruses", "Parasites"]
|
40 |
+
|
41 |
+
|
42 |
+
def create_branch(branch_name):
|
43 |
+
return site.create_git_ref(
|
44 |
+
'refs/heads/{branch_name}'.format(**locals()),
|
45 |
+
site.get_branch('main').commit.sha
|
46 |
+
)
|
47 |
+
|
48 |
+
def create_pull_request(branch_name, github_user):
|
49 |
+
if github_user!="":
|
50 |
+
message = f"submitted by: @{github_user}."
|
51 |
+
else:
|
52 |
+
message ="No github account provided."
|
53 |
+
return site.create_pull(
|
54 |
+
title="New icons submitted via web",
|
55 |
+
body= message,
|
56 |
+
base="main",
|
57 |
+
head=branch_name
|
58 |
+
)
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
def remove_special_chars(author):
|
63 |
+
author = author.replace(" ", "-")
|
64 |
+
author = author.replace(".", "-")
|
65 |
+
author = author.replace("(", "-")
|
66 |
+
author = author.replace(")", "-")
|
67 |
+
author = author.replace(":", "-")
|
68 |
+
author = author.replace(";", "-")
|
69 |
+
author = author.replace(",", "-")
|
70 |
+
author = author.replace("!", "-")
|
71 |
+
author = author.replace("?", "-")
|
72 |
+
author = author.replace("=", "-")
|
73 |
+
author = author.replace("+", "-")
|
74 |
+
author = author.replace("/", "-")
|
75 |
+
return author
|
76 |
+
|
77 |
+
def process_and_create_pull(icon_files,category, new_cateogry_check, new_category, license, creator_name, creator_url, copyright, github_user):
|
78 |
+
if copyright:
|
79 |
+
if new_cateogry_check:
|
80 |
+
category = remove_special_chars(new_category)
|
81 |
+
hash = '{0:010x}'.format(int(time.time() * 256))[:8]
|
82 |
+
branch_name = f"new-icons-{hash}"
|
83 |
+
create_branch(branch_name)
|
84 |
+
|
85 |
+
authors = requests.get("https://bioicons.com/icons/authors.json").json()
|
86 |
+
|
87 |
+
#remove whitespace and other special characters
|
88 |
+
author = remove_special_chars(creator_name)
|
89 |
+
|
90 |
+
if author not in authors.keys():
|
91 |
+
index_file = site.get_contents('static/icons/authors.json')
|
92 |
+
|
93 |
+
authors[author]=creator_url
|
94 |
+
updated_content = json.dumps(authors, indent=4)
|
95 |
+
site.update_file(
|
96 |
+
path='static/icons/authors.json',
|
97 |
+
message='Insert author',
|
98 |
+
content=updated_content,
|
99 |
+
sha=index_file.sha,
|
100 |
+
branch=branch_name
|
101 |
+
)
|
102 |
+
for file in icon_files:
|
103 |
+
#get file content from filesystem
|
104 |
+
with open(file.name, 'r') as fp:
|
105 |
+
file_content = fp.read()
|
106 |
+
|
107 |
+
site.create_file(
|
108 |
+
path=f'static/icons/{license}/{category}/{author}/{os.path.basename(file.name)}',
|
109 |
+
message=f'create {os.path.basename(file.name)}',
|
110 |
+
content=file_content,
|
111 |
+
branch=branch_name
|
112 |
+
)
|
113 |
+
pull = create_pull_request(branch_name, github_user)
|
114 |
+
return f"""<link href="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.6.4/flowbite.min.css" rel="stylesheet" />
|
115 |
+
<div class="flex p-4 mb-4 text-sm text-green-800 rounded-lg bg-green-200 " role="alert">
|
116 |
+
|
117 |
+
<span class="sr-only">Success</span>
|
118 |
+
<div>
|
119 |
+
<span class="font-medium">Thank you!</span> Your pull request for the submitted illustrations has been submitted on GitHub. You can track your submission on Github at <br> <a class="text-green-400 text-lg hover:underline" href="{pull.html_url}">{pull.html_url}</a>.
|
120 |
+
</div>
|
121 |
+
</div>
|
122 |
+
"""
|
123 |
+
else:
|
124 |
+
return """<link href="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.6.4/flowbite.min.css" rel="stylesheet" />
|
125 |
+
<div class="flex p-4 mb-4 text-sm text-red-800 rounded-lg bg-red-200 dark:bg-gray-800 dark:text-red-400" role="alert">
|
126 |
+
<svg aria-hidden="true" class="flex-shrink-0 inline w-5 h-5 mr-3" fill="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
127 |
+
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z" />
|
128 |
+
</svg>
|
129 |
+
|
130 |
+
<span class="sr-only">Info</span>
|
131 |
+
<div>
|
132 |
+
<span class="font-medium">Copyright error.</span> You need to certify that the illustrations have a suitable license and that copyright allows to share it on Bioicons.
|
133 |
+
</div>
|
134 |
+
</div>
|
135 |
+
"""
|
136 |
+
|
137 |
+
def shownewcategory(new_category, check):
|
138 |
+
if check:
|
139 |
+
return gr.Textbox.update(visible=True)
|
140 |
+
return gr.Textbox.update(visible=False)
|
141 |
+
|
142 |
+
|
143 |
+
with gr.Blocks() as demo:
|
144 |
+
with gr.Row():
|
145 |
+
creator_name = gr.Textbox(label="Creator")
|
146 |
+
creator_url = gr.Textbox(label="Creator URL", placeholder="Link to personal website,Twitter,Mastodon, GitHub")
|
147 |
+
github_user = gr.Textbox(label="Your GitHub Username", placeholder="GitHub username is used to communicate with you")
|
148 |
+
icon_files = gr.File(label="Icon files", file_types=["svg"], file_count="multiple")
|
149 |
+
with gr.Row():
|
150 |
+
with gr.Box():
|
151 |
+
category = gr.Dropdown(label="Category", choices=categories)
|
152 |
+
new_category_check = gr.Checkbox(info="New category?", label="Desired category not listed?")
|
153 |
+
new_category = gr.Textbox(label="New category", visible=False)
|
154 |
+
new_category_check.change(fn=shownewcategory, inputs=[new_category,new_category_check], outputs=new_category)
|
155 |
+
license = gr.Dropdown(label="License", choices=licenses)
|
156 |
+
copyright = gr.Checkbox(info="Copyright", label="I certify that I own the copyright of the submitted illustrations or have appropriately credited the original creator as required by the license of the illustration.")
|
157 |
+
btn = gr.Button(value="Submit illustrations")
|
158 |
+
output = gr.HTML(label="Output", value="")
|
159 |
+
|
160 |
+
btn.click(process_and_create_pull, inputs=[icon_files,category, new_category_check, new_category, license, creator_name, creator_url, copyright, github_user], outputs=output, api_name="bioicons-submit")
|
161 |
+
|
162 |
+
demo.launch()
|