Spaces:
Running
Running
Commit
·
0817061
1
Parent(s):
81518e2
Update app.py
Browse files
app.py
CHANGED
|
@@ -116,56 +116,6 @@ widget:
|
|
| 116 |
with open(f"{folder}/README.md", "w") as file:
|
| 117 |
file.write(readme_content)
|
| 118 |
|
| 119 |
-
|
| 120 |
-
def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], url, progress=gr.Progress(track_tqdm=True)):
|
| 121 |
-
if not profile.name:
|
| 122 |
-
return gr.Error("Are you sure you are logged in?")
|
| 123 |
-
|
| 124 |
-
folder = str(uuid.uuid4())
|
| 125 |
-
os.makedirs(folder, exist_ok=False)
|
| 126 |
-
info, downloaded_files = process_url(url, folder=folder)
|
| 127 |
-
create_readme(info, downloaded_files, folder=folder)
|
| 128 |
-
try:
|
| 129 |
-
api = HfApi(token=os.environ["HUGGING_FACE_HUB_TOKEN"])
|
| 130 |
-
username = api.whoami()["name"]
|
| 131 |
-
slug_name = slugify(info["name"])
|
| 132 |
-
repo_id = f"{username}/{profile.preferred_username}-{slug_name}"
|
| 133 |
-
api.create_repo(repo_id=repo_id, private=True, exist_ok=True)
|
| 134 |
-
api.upload_folder(
|
| 135 |
-
folder_path=folder,
|
| 136 |
-
repo_id=repo_id,
|
| 137 |
-
repo_type="model",
|
| 138 |
-
)
|
| 139 |
-
api.update_repo_visibility(repo_id=repo_id, private=False)
|
| 140 |
-
except:
|
| 141 |
-
raise gr.Error("something went wrong")
|
| 142 |
-
|
| 143 |
-
transfer_repos = gr.load("multimodalart/transfer_repos", hf_token=os.environ["HUGGING_FACE_HUB_TOKEN"], src="spaces")
|
| 144 |
-
user_repo_id = f"{profile.preferred_username}/{slug_name}"
|
| 145 |
-
response_code = transfer_repos(repo_id, user_repo_id)
|
| 146 |
-
i = 0
|
| 147 |
-
while response_code != "200":
|
| 148 |
-
message = None
|
| 149 |
-
if response_code == "409":
|
| 150 |
-
if i < 3:
|
| 151 |
-
user_repo_id = f"{profile.preferred_username}/{slug_name}-{i}"
|
| 152 |
-
response_code = transfer_repos(repo_id, user_repo_id)
|
| 153 |
-
i += 1
|
| 154 |
-
else:
|
| 155 |
-
message = "It seems this model has been uploaded already in your account."
|
| 156 |
-
elif response_code == "404":
|
| 157 |
-
message = "Something went wrong with the model upload. Try again."
|
| 158 |
-
else:
|
| 159 |
-
message = f"Unexpected response code: {response_code}."
|
| 160 |
-
|
| 161 |
-
if message:
|
| 162 |
-
api.delete_repo(repo_id=repo_id, repo_type="model")
|
| 163 |
-
raise gr.Error(message)
|
| 164 |
-
|
| 165 |
-
return f'''# Model uploaded to 🤗!
|
| 166 |
-
## Access it here [{user_repo_id}](https://huggingface.co/{user_repo_id}) '''
|
| 167 |
-
|
| 168 |
-
|
| 169 |
def get_creator(username):
|
| 170 |
url = f"https://civitai.com/api/trpc/user.getCreator?input=%7B%22json%22%3A%7B%22username%22%3A%22{username}%22%2C%22authed%22%3Atrue%7D%7D"
|
| 171 |
headers = {
|
|
@@ -206,12 +156,12 @@ def check_civit_link(profile: Optional[gr.OAuthProfile], url):
|
|
| 206 |
attributes_methods = dir(profile)
|
| 207 |
if(not hf_username):
|
| 208 |
no_username_text = f'Model by <b>{info["creator"]}</b> 🤗, if you are {info["creator"]}, hi! Your CivitAI profile seems to not have information about your Hugging Face account. Please visit <a href="https://civitai.com/user/account">https://civitai.com/user/account</a> and include it there<br><img width="60%" src="https://i.imgur.com/hCbo9uL.png" /><br>(if you are not {info["creator"]}, you cannot submit their model at this time)'
|
| 209 |
-
return no_username_text, gr.update(interactive=False), gr.update(visible=True)
|
| 210 |
if(profile.preferred_username != hf_username):
|
| 211 |
unmatched_username_text = '<h4>Oops, the Hugging Face account in your CivitAI profile seems to be different than the one your are using here. Please visit <a href="https://civitai.com/user/account">https://civitai.com/user/account</a> and update it there<br><img src="https://i.imgur.com/hCbo9uL.png" /></h4>'
|
| 212 |
-
return unmatched_username_text, gr.update(interactive=False), gr.update(visible=True)
|
| 213 |
else:
|
| 214 |
-
return '', gr.update(interactive=True), gr.update(visible=False)
|
| 215 |
|
| 216 |
def swap_fill(profile: Optional[gr.OAuthProfile]):
|
| 217 |
if profile is None:
|
|
@@ -221,6 +171,58 @@ def swap_fill(profile: Optional[gr.OAuthProfile]):
|
|
| 221 |
|
| 222 |
def show_output():
|
| 223 |
return gr.update(visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
css = '''
|
| 225 |
#login {
|
| 226 |
font-size: 0px;
|
|
@@ -271,11 +273,12 @@ Get diffusers compatibility, a free GPU-based Inference Widget and possibility t
|
|
| 271 |
try_again_button = gr.Button("I have added my HF profile to my account", visible=False)
|
| 272 |
submit_button_civit = gr.Button("Upload model to Hugging Face", interactive=False)
|
| 273 |
output = gr.Markdown(label="Output progress", visible=False)
|
| 274 |
-
|
|
|
|
| 275 |
demo.load(fn=swap_fill, outputs=[disabled_area, enabled_area])
|
| 276 |
-
submit_source_civit.change(fn=check_civit_link, inputs=[submit_source_civit], outputs=[instructions, submit_button_civit, try_again_button])
|
| 277 |
try_again_button.click(fn=check_civit_link, inputs=[submit_source_civit], outputs=[instructions, submit_button_civit, try_again_button])
|
| 278 |
-
submit_button_civit.click(fn=show_output, inputs=[], outputs=[output]).then(fn=upload_civit_to_hf, inputs=[submit_source_civit], outputs=[output])
|
| 279 |
gr.LogoutButton(elem_id="logout")
|
| 280 |
demo.queue()
|
| 281 |
demo.launch()
|
|
|
|
| 116 |
with open(f"{folder}/README.md", "w") as file:
|
| 117 |
file.write(readme_content)
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
def get_creator(username):
|
| 120 |
url = f"https://civitai.com/api/trpc/user.getCreator?input=%7B%22json%22%3A%7B%22username%22%3A%22{username}%22%2C%22authed%22%3Atrue%7D%7D"
|
| 121 |
headers = {
|
|
|
|
| 156 |
attributes_methods = dir(profile)
|
| 157 |
if(not hf_username):
|
| 158 |
no_username_text = f'Model by <b>{info["creator"]}</b> 🤗, if you are {info["creator"]}, hi! Your CivitAI profile seems to not have information about your Hugging Face account. Please visit <a href="https://civitai.com/user/account">https://civitai.com/user/account</a> and include it there<br><img width="60%" src="https://i.imgur.com/hCbo9uL.png" /><br>(if you are not {info["creator"]}, you cannot submit their model at this time)'
|
| 159 |
+
return no_username_text, gr.update(interactive=False), gr.update(visible=True), hf_username
|
| 160 |
if(profile.preferred_username != hf_username):
|
| 161 |
unmatched_username_text = '<h4>Oops, the Hugging Face account in your CivitAI profile seems to be different than the one your are using here. Please visit <a href="https://civitai.com/user/account">https://civitai.com/user/account</a> and update it there<br><img src="https://i.imgur.com/hCbo9uL.png" /></h4>'
|
| 162 |
+
return unmatched_username_text, gr.update(interactive=False), gr.update(visible=True), hf_username
|
| 163 |
else:
|
| 164 |
+
return '', gr.update(interactive=True), gr.update(visible=False), hf_username
|
| 165 |
|
| 166 |
def swap_fill(profile: Optional[gr.OAuthProfile]):
|
| 167 |
if profile is None:
|
|
|
|
| 171 |
|
| 172 |
def show_output():
|
| 173 |
return gr.update(visible=True)
|
| 174 |
+
|
| 175 |
+
def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], url, civit_username, progress=gr.Progress(track_tqdm=True)):
|
| 176 |
+
if not profile.name:
|
| 177 |
+
return gr.Error("Are you sure you are logged in?")
|
| 178 |
+
if civit_username != profile.preferred_username:
|
| 179 |
+
return gr.Error("You are not the model author")
|
| 180 |
+
|
| 181 |
+
folder = str(uuid.uuid4())
|
| 182 |
+
os.makedirs(folder, exist_ok=False)
|
| 183 |
+
info, downloaded_files = process_url(url, folder=folder)
|
| 184 |
+
create_readme(info, downloaded_files, folder=folder)
|
| 185 |
+
try:
|
| 186 |
+
api = HfApi(token=os.environ["HUGGING_FACE_HUB_TOKEN"])
|
| 187 |
+
username = api.whoami()["name"]
|
| 188 |
+
slug_name = slugify(info["name"])
|
| 189 |
+
repo_id = f"{username}/{profile.preferred_username}-{slug_name}"
|
| 190 |
+
api.create_repo(repo_id=repo_id, private=True, exist_ok=True)
|
| 191 |
+
api.upload_folder(
|
| 192 |
+
folder_path=folder,
|
| 193 |
+
repo_id=repo_id,
|
| 194 |
+
repo_type="model",
|
| 195 |
+
)
|
| 196 |
+
api.update_repo_visibility(repo_id=repo_id, private=False)
|
| 197 |
+
except:
|
| 198 |
+
raise gr.Error("something went wrong")
|
| 199 |
+
|
| 200 |
+
transfer_repos = gr.load("multimodalart/transfer_repos", hf_token=os.environ["HUGGING_FACE_HUB_TOKEN"], src="spaces")
|
| 201 |
+
user_repo_id = f"{profile.preferred_username}/{slug_name}"
|
| 202 |
+
response_code = transfer_repos(repo_id, user_repo_id)
|
| 203 |
+
i = 0
|
| 204 |
+
while response_code != "200":
|
| 205 |
+
message = None
|
| 206 |
+
if response_code == "409":
|
| 207 |
+
if i < 3:
|
| 208 |
+
user_repo_id = f"{profile.preferred_username}/{slug_name}-{i}"
|
| 209 |
+
response_code = transfer_repos(repo_id, user_repo_id)
|
| 210 |
+
i += 1
|
| 211 |
+
else:
|
| 212 |
+
message = "It seems this model has been uploaded already in your account."
|
| 213 |
+
elif response_code == "404":
|
| 214 |
+
message = "Something went wrong with the model upload. Try again."
|
| 215 |
+
else:
|
| 216 |
+
message = f"Unexpected response code: {response_code}."
|
| 217 |
+
|
| 218 |
+
if message:
|
| 219 |
+
api.delete_repo(repo_id=repo_id, repo_type="model")
|
| 220 |
+
raise gr.Error(message)
|
| 221 |
+
|
| 222 |
+
return f'''# Model uploaded to 🤗!
|
| 223 |
+
## Access it here [{user_repo_id}](https://huggingface.co/{user_repo_id}) '''
|
| 224 |
+
|
| 225 |
+
|
| 226 |
css = '''
|
| 227 |
#login {
|
| 228 |
font-size: 0px;
|
|
|
|
| 273 |
try_again_button = gr.Button("I have added my HF profile to my account", visible=False)
|
| 274 |
submit_button_civit = gr.Button("Upload model to Hugging Face", interactive=False)
|
| 275 |
output = gr.Markdown(label="Output progress", visible=False)
|
| 276 |
+
|
| 277 |
+
civit_username = gr.State()
|
| 278 |
demo.load(fn=swap_fill, outputs=[disabled_area, enabled_area])
|
| 279 |
+
submit_source_civit.change(fn=check_civit_link, inputs=[submit_source_civit], outputs=[instructions, submit_button_civit, try_again_button, civit_username])
|
| 280 |
try_again_button.click(fn=check_civit_link, inputs=[submit_source_civit], outputs=[instructions, submit_button_civit, try_again_button])
|
| 281 |
+
submit_button_civit.click(fn=show_output, inputs=[], outputs=[output]).then(fn=upload_civit_to_hf, inputs=[submit_source_civit, civit_username], outputs=[output])
|
| 282 |
gr.LogoutButton(elem_id="logout")
|
| 283 |
demo.queue()
|
| 284 |
demo.launch()
|