multimodalart HF staff commited on
Commit
0227cff
1 Parent(s): 44cc1f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -10
app.py CHANGED
@@ -104,9 +104,10 @@ def process_url(url, profile, do_download=True, folder="."):
104
  else:
105
  raise gr.Error("Something went wrong in fetching CivitAI API")
106
 
107
- def create_readme(info, downloaded_files, is_author=True, folder="."):
108
  readme_content = ""
109
  original_url = f"https://civitai.com/models/{info['id']}"
 
110
  non_author_disclaimer = f'This model was originally uploaded on [CivitAI]({original_url}), by [{info["creator"]}](https://civitai.com/user/{info["creator"]}/models). The information below was provided by the author on CivitAI:'
111
  default_tags = ["text-to-image", "stable-diffusion", "lora", "diffusers"]
112
  civit_tags = [t for t in info["tags"] if t not in default_tags]
@@ -124,7 +125,7 @@ widget:
124
  - text: {widget_prompts}
125
  ---
126
 
127
- # {info["name"]}
128
 
129
  {non_author_disclaimer if not is_author else ''}
130
 
@@ -203,15 +204,35 @@ def swap_fill(profile: Optional[gr.OAuthProfile]):
203
 
204
  def show_output():
205
  return gr.update(visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
 
207
- def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], url, progress=gr.Progress(track_tqdm=True)):
 
 
208
  if not profile.name:
209
  return gr.Error("Are you sure you are logged in?")
210
 
211
  folder = str(uuid.uuid4())
212
  os.makedirs(folder, exist_ok=False)
213
  info, downloaded_files = process_url(url, profile, folder=folder)
214
- create_readme(info, downloaded_files, folder=folder)
215
  try:
216
  api = HfApi(token=os.environ["HUGGING_FACE_HUB_TOKEN"])
217
  username = api.whoami()["name"]
@@ -252,6 +273,10 @@ def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], url, progress=gr.Prog
252
  return f'''# Model uploaded to 🤗!
253
  ## Access it here [{user_repo_id}](https://huggingface.co/{user_repo_id}) '''
254
 
 
 
 
 
255
 
256
  css = '''
257
  #login {
@@ -294,11 +319,21 @@ Get diffusers compatibility, a free GPU-based Inference Widget and possibility t
294
  )
295
  submit_button_civit = gr.Button("Upload model to Hugging Face and submit", interactive=False)
296
  with gr.Column(visible=False) as enabled_area:
297
- with gr.Row():
298
- submit_source_civit = gr.Textbox(
299
- label="CivitAI model URL",
300
- info="URL of the CivitAI model, for now only SDXL LoRAs are supported",
301
- )
 
 
 
 
 
 
 
 
 
 
302
  instructions = gr.HTML("")
303
  try_again_button = gr.Button("I have added my HF profile to my account (it may take 1 minute to refresh)", visible=False)
304
  submit_button_civit = gr.Button("Upload model to Hugging Face", interactive=False)
@@ -306,8 +341,10 @@ Get diffusers compatibility, a free GPU-based Inference Widget and possibility t
306
 
307
  demo.load(fn=swap_fill, outputs=[disabled_area, enabled_area])
308
  submit_source_civit.change(fn=check_civit_link, inputs=[submit_source_civit], outputs=[instructions, submit_button_civit, try_again_button, submit_button_civit])
 
309
  try_again_button.click(fn=check_civit_link, inputs=[submit_source_civit], outputs=[instructions, submit_button_civit, try_again_button, submit_button_civit])
310
- submit_button_civit.click(fn=show_output, inputs=[], outputs=[output]).then(fn=upload_civit_to_hf, inputs=[submit_source_civit], outputs=[output])
 
311
  gr.LogoutButton(elem_id="logout")
312
  demo.queue()
313
  demo.launch()
 
104
  else:
105
  raise gr.Error("Something went wrong in fetching CivitAI API")
106
 
107
+ def create_readme(info, downloaded_files, link_civit=False, is_author=True, folder="."):
108
  readme_content = ""
109
  original_url = f"https://civitai.com/models/{info['id']}"
110
+ link_civit_disclaimer = f'([CivitAI]({original_url})'
111
  non_author_disclaimer = f'This model was originally uploaded on [CivitAI]({original_url}), by [{info["creator"]}](https://civitai.com/user/{info["creator"]}/models). The information below was provided by the author on CivitAI:'
112
  default_tags = ["text-to-image", "stable-diffusion", "lora", "diffusers"]
113
  civit_tags = [t for t in info["tags"] if t not in default_tags]
 
125
  - text: {widget_prompts}
126
  ---
127
 
128
+ # {info["name"]} {link_civit_disclaimer if link_civit else ''}
129
 
130
  {non_author_disclaimer if not is_author else ''}
131
 
 
204
 
205
  def show_output():
206
  return gr.update(visible=True)
207
+
208
+ def list_civit_models(username):
209
+ url = f"https://civitai.com/api/v1/models?username={username}&limit=100"
210
+ json_models_list = []
211
+
212
+ while url:
213
+ response = requests.get(url)
214
+ data = response.json()
215
+
216
+ # Add current page items to the list
217
+ json_models_list.extend(data.get('items', []))
218
+
219
+ # Check if there is a nextPage URL in the metadata
220
+ metadata = data.get('metadata', {})
221
+ url = metadata.get('nextPage', None)
222
+ urls = ""
223
+ for model in json_models_list:
224
+ urls += f'https://civitai.com/models/{model["id"]}/{slugify(model["name"])}\n'
225
 
226
+ return urls
227
+
228
+ def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], url, link_civit=False, progress=gr.Progress(track_tqdm=True)):
229
  if not profile.name:
230
  return gr.Error("Are you sure you are logged in?")
231
 
232
  folder = str(uuid.uuid4())
233
  os.makedirs(folder, exist_ok=False)
234
  info, downloaded_files = process_url(url, profile, folder=folder)
235
+ create_readme(info, downloaded_files, link_civit, folder=folder)
236
  try:
237
  api = HfApi(token=os.environ["HUGGING_FACE_HUB_TOKEN"])
238
  username = api.whoami()["name"]
 
273
  return f'''# Model uploaded to 🤗!
274
  ## Access it here [{user_repo_id}](https://huggingface.co/{user_repo_id}) '''
275
 
276
+ def bulk_upload(profile: Optional[gr.OAuthProfile], urls, link_civit=False, progress=gr.Progress(track_tqdm=True)):
277
+ for url in urls.split("\n"):
278
+ if(url):
279
+ yield upload_civit_to_hf(profile, url, link_civit)
280
 
281
  css = '''
282
  #login {
 
319
  )
320
  submit_button_civit = gr.Button("Upload model to Hugging Face and submit", interactive=False)
321
  with gr.Column(visible=False) as enabled_area:
322
+ with gr.Column():
323
+ submit_source_civit = gr.Textbox(
324
+ label="CivitAI model URL",
325
+ info="URL of the CivitAI model, for now only SDXL LoRAs are supported",
326
+ )
327
+ with gr.Accordion("Advanced options", open=False):
328
+ civit_username_to_bulk = gr.Textbox(label="CivitAI username (optional)", info="Type your CivitAI username here to automagically fill the bulk models URLs list below (optional, you can paste links down here directly)")
329
+ submit_bulk_civit = gr.Textbox(
330
+ label="CivitAI bulk models URLs",
331
+ info="Add one URL per line",
332
+ lines=6,
333
+ )
334
+ link_civit = gr.Checkbox(label="Link back to CivitAI?", value=False)
335
+ bulk_button = gr.Button("Bulk upload")
336
+
337
  instructions = gr.HTML("")
338
  try_again_button = gr.Button("I have added my HF profile to my account (it may take 1 minute to refresh)", visible=False)
339
  submit_button_civit = gr.Button("Upload model to Hugging Face", interactive=False)
 
341
 
342
  demo.load(fn=swap_fill, outputs=[disabled_area, enabled_area])
343
  submit_source_civit.change(fn=check_civit_link, inputs=[submit_source_civit], outputs=[instructions, submit_button_civit, try_again_button, submit_button_civit])
344
+ civit_username_to_bulk.change(fn=list_civit_models, inputs=[civit_username_to_bulk], outputs=[submit_bulk_civit])
345
  try_again_button.click(fn=check_civit_link, inputs=[submit_source_civit], outputs=[instructions, submit_button_civit, try_again_button, submit_button_civit])
346
+ submit_button_civit.click(fn=show_output, inputs=[], outputs=[output]).then(fn=upload_civit_to_hf, inputs=[submit_source_civit, link_civit], outputs=[output])
347
+ bulk_button.click(fn=show_output, inputs=[], outputs=[output]).then(fn=bulk_upload, inputs=[submit_bulk_civit, link_civit], outputs=[output])
348
  gr.LogoutButton(elem_id="logout")
349
  demo.queue()
350
  demo.launch()