Add flags.
Browse files
app.py
CHANGED
@@ -16,6 +16,16 @@ api = HfApi()
|
|
16 |
def restart_space():
|
17 |
api.restart_space(repo_id="civitaiarchive/civitai-to-hf-uploader", token=os.environ["HF_TOKEN"])
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def download_file(url, file_path, folder, api_key=None):
|
21 |
headers = {}
|
@@ -43,10 +53,15 @@ def download_file(url, file_path, folder, api_key=None):
|
|
43 |
raise gr.Error(f"Error downloading file: {str(e)}")
|
44 |
|
45 |
|
46 |
-
def get_files_by_username(username, api_key=None):
|
47 |
-
url = f"https://civitai.com/api/v1/models?username={username}&limit=100
|
48 |
output = {}
|
49 |
headers = {}
|
|
|
|
|
|
|
|
|
|
|
50 |
if api_key:
|
51 |
headers['Authorization'] = f'Bearer {api_key}'
|
52 |
|
@@ -97,13 +112,13 @@ def get_files_by_model_id(model_id, api_key=None):
|
|
97 |
except requests.exceptions.RequestException as e:
|
98 |
raise gr.Error("Something went wrong in fetching CivitAI API")
|
99 |
|
100 |
-
def process_url(url, profile, user_repo_id, oauth_token, folder, api_key=None):
|
101 |
if url.startswith("https://civitai.com/models/"):
|
102 |
model_id = url.split('/')[4]
|
103 |
files = get_files_by_model_id(model_id, api_key)
|
104 |
elif url.startswith("https://civitai.com/user/"):
|
105 |
username = url.split('/')[4]
|
106 |
-
files = get_files_by_username(username, api_key)
|
107 |
else:
|
108 |
raise gr.Error("Unknown CivitAI URL format, please provide model URL or user profile URL")
|
109 |
|
@@ -175,7 +190,7 @@ def add_mirror(repo_id):
|
|
175 |
|
176 |
|
177 |
|
178 |
-
def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], oauth_token: gr.OAuthToken, url, destination_repo, civitai_api_key=None):
|
179 |
if not profile.name:
|
180 |
raise gr.Error("Are you sure you are logged in?")
|
181 |
|
@@ -186,6 +201,10 @@ def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], oauth_token: gr.OAuth
|
|
186 |
if not re.match(r'^[a-zA-Z0-9_-]+$', destination_repo):
|
187 |
raise gr.Error("Destination repository name must contain only alphanumeric characters, underscores, and hyphens")
|
188 |
|
|
|
|
|
|
|
|
|
189 |
folder = str(uuid.uuid4())
|
190 |
os.makedirs(folder, exist_ok=False)
|
191 |
gr.Info(f"Starting download from {url}")
|
@@ -201,7 +220,7 @@ def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], oauth_token: gr.OAuth
|
|
201 |
gr.Info(f"Repository {user_repo_id} already exists, will update it")
|
202 |
update_repo_visibility(repo_id=user_repo_id, private=False, token=oauth_token.token)
|
203 |
|
204 |
-
files = process_url(url, profile, user_repo_id, oauth_token.token, folder, civitai_api_key)
|
205 |
if not files or len(files.keys()) == 0:
|
206 |
raise gr.Error("No files were copied. Something went wrong.")
|
207 |
|
@@ -268,6 +287,16 @@ Once uploaded, it will add this repository to CivitaiArchive.com as a mirror.
|
|
268 |
label="CivitAI API Key",
|
269 |
info="Optional: Provide your own CivitAI API key to avoid rate limits. If not provided, a default key will be used.",
|
270 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
|
272 |
instructions = gr.HTML("")
|
273 |
submit_button_civit = gr.Button("Upload to Hugging Face", interactive=True)
|
@@ -277,7 +306,7 @@ Once uploaded, it will add this repository to CivitaiArchive.com as a mirror.
|
|
277 |
|
278 |
submit_button_civit.click(
|
279 |
fn=upload_civit_to_hf,
|
280 |
-
inputs=[submit_source_civit, destination_repo, civitai_api_key],
|
281 |
outputs=[output]
|
282 |
)
|
283 |
|
|
|
16 |
def restart_space():
|
17 |
api.restart_space(repo_id="civitaiarchive/civitai-to-hf-uploader", token=os.environ["HF_TOKEN"])
|
18 |
|
19 |
+
BOOLPARM = {
|
20 |
+
# Raw label conversion.
|
21 |
+
"Default": None,
|
22 |
+
"Include": True,
|
23 |
+
"Exclude": False,
|
24 |
+
# Url parm conversion.
|
25 |
+
True: "true",
|
26 |
+
False: "false",
|
27 |
+
None: "", # Should not pass parm in this case.
|
28 |
+
}
|
29 |
|
30 |
def download_file(url, file_path, folder, api_key=None):
|
31 |
headers = {}
|
|
|
53 |
raise gr.Error(f"Error downloading file: {str(e)}")
|
54 |
|
55 |
|
56 |
+
def get_files_by_username(username, api_key=None, nsfw=None, hidden=None):
|
57 |
+
url = f"https://civitai.com/api/v1/models?username={username}&limit=100"
|
58 |
output = {}
|
59 |
headers = {}
|
60 |
+
if nsfw is not None:
|
61 |
+
url = url + f"&nsfw={BOOLPARM[nsfw]}"
|
62 |
+
if hidden is not None:
|
63 |
+
url = url + f"&hidden={BOOLPARM[hidden]}"
|
64 |
+
gr.Info(f"SBM url: {url}")
|
65 |
if api_key:
|
66 |
headers['Authorization'] = f'Bearer {api_key}'
|
67 |
|
|
|
112 |
except requests.exceptions.RequestException as e:
|
113 |
raise gr.Error("Something went wrong in fetching CivitAI API")
|
114 |
|
115 |
+
def process_url(url, profile, user_repo_id, oauth_token, folder, api_key=None, nsfw=None, hidden=None):
|
116 |
if url.startswith("https://civitai.com/models/"):
|
117 |
model_id = url.split('/')[4]
|
118 |
files = get_files_by_model_id(model_id, api_key)
|
119 |
elif url.startswith("https://civitai.com/user/"):
|
120 |
username = url.split('/')[4]
|
121 |
+
files = get_files_by_username(username, api_key, nsfw, hidden)
|
122 |
else:
|
123 |
raise gr.Error("Unknown CivitAI URL format, please provide model URL or user profile URL")
|
124 |
|
|
|
190 |
|
191 |
|
192 |
|
193 |
+
def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], oauth_token: gr.OAuthToken, url, destination_repo, civitai_api_key=None, nsfw=None, hidden=None):
|
194 |
if not profile.name:
|
195 |
raise gr.Error("Are you sure you are logged in?")
|
196 |
|
|
|
201 |
if not re.match(r'^[a-zA-Z0-9_-]+$', destination_repo):
|
202 |
raise gr.Error("Destination repository name must contain only alphanumeric characters, underscores, and hyphens")
|
203 |
|
204 |
+
# Convert bool flags.
|
205 |
+
nsfw = BOOLPARM[nsfw]
|
206 |
+
hidden = BOOLPARM[hidden]
|
207 |
+
|
208 |
folder = str(uuid.uuid4())
|
209 |
os.makedirs(folder, exist_ok=False)
|
210 |
gr.Info(f"Starting download from {url}")
|
|
|
220 |
gr.Info(f"Repository {user_repo_id} already exists, will update it")
|
221 |
update_repo_visibility(repo_id=user_repo_id, private=False, token=oauth_token.token)
|
222 |
|
223 |
+
files = process_url(url, profile, user_repo_id, oauth_token.token, folder, civitai_api_key, nsfw, hidden)
|
224 |
if not files or len(files.keys()) == 0:
|
225 |
raise gr.Error("No files were copied. Something went wrong.")
|
226 |
|
|
|
287 |
label="CivitAI API Key",
|
288 |
info="Optional: Provide your own CivitAI API key to avoid rate limits. If not provided, a default key will be used.",
|
289 |
)
|
290 |
+
include_nsfw = gr.Dropdown(
|
291 |
+
choices=["Default", "Include", "Exclude"],
|
292 |
+
value="Include",
|
293 |
+
label="Optional: Include, exclude or do not specify inclusion of nsfw models."
|
294 |
+
)
|
295 |
+
include_hidden = gr.Dropdown(
|
296 |
+
choices=["Default", "Include", "Exclude"],
|
297 |
+
value="Default",
|
298 |
+
label="Optional: Include, exclude or do not specify inclusion of hidden models."
|
299 |
+
)
|
300 |
|
301 |
instructions = gr.HTML("")
|
302 |
submit_button_civit = gr.Button("Upload to Hugging Face", interactive=True)
|
|
|
306 |
|
307 |
submit_button_civit.click(
|
308 |
fn=upload_civit_to_hf,
|
309 |
+
inputs=[submit_source_civit, destination_repo, civitai_api_key, include_nsfw, include_hidden],
|
310 |
outputs=[output]
|
311 |
)
|
312 |
|