John6666 commited on
Commit
5e85279
1 Parent(s): b779954

Upload 3 files

Browse files
Files changed (2) hide show
  1. app.py +9 -3
  2. civitai_to_hf.py +16 -6
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from civitai_to_hf import (search_civitai, download_civitai, select_civitai_item, add_civitai_item, get_civitai_tag, select_civitai_all_item,
3
- update_civitai_selection, CIVITAI_TYPE, CIVITAI_BASEMODEL, CIVITAI_SORT, CIVITAI_PERIOD)
 
4
 
5
  css = """
6
  .title { font-size: 3em; align-items: center; text-align: center; }
@@ -34,8 +35,11 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
34
  with gr.Column(scale=9):
35
  with gr.Accordion("Select from Gallery", open=False):
36
  search_civitai_gallery = gr.Gallery([], label="Results", allow_preview=False, columns=5, elem_id="gallery", show_share_button=False, interactive=False)
 
 
37
  search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value=[],
38
  allow_custom_value=True, visible=True, multiselect=True)
 
39
  with gr.Column(scale=1):
40
  search_civitai_add = gr.Button("Add to download URLs")
41
  search_civitai_select_all = gr.Button("Select All", variant="secondary", size="sm")
@@ -70,11 +74,13 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
70
  fn=search_civitai,
71
  inputs=[search_civitai_query, search_civitai_type, search_civitai_basemodel, search_civitai_sort,
72
  search_civitai_period, search_civitai_tag, search_civitai_user, search_civitai_limit, search_civitai_page, search_civitai_gallery],
73
- outputs=[search_civitai_result, search_civitai_desc, search_civitai_submit, search_civitai_query, search_civitai_gallery],
74
  queue=True,
75
  show_api=False,
76
  )
77
- search_civitai_result.change(select_civitai_item, [search_civitai_result], [search_civitai_desc, search_civitai_json], queue=False, show_api=False)
 
 
78
  search_civitai_add.click(add_civitai_item, [search_civitai_result, dl_url], [dl_url], queue=False, show_api=False)
79
  search_civitai_select_all.click(select_civitai_all_item, [search_civitai_select_all], [search_civitai_select_all, search_civitai_result], queue=False, show_api=False)
80
  search_civitai_gallery.select(update_civitai_selection, [search_civitai_result], [search_civitai_result], queue=False, show_api=False)
 
1
  import gradio as gr
2
  from civitai_to_hf import (search_civitai, download_civitai, select_civitai_item, add_civitai_item, get_civitai_tag, select_civitai_all_item,
3
+ update_civitai_selection, update_civitai_checkbox, from_civitai_checkbox,
4
+ CIVITAI_TYPE, CIVITAI_BASEMODEL, CIVITAI_SORT, CIVITAI_PERIOD)
5
 
6
  css = """
7
  .title { font-size: 3em; align-items: center; text-align: center; }
 
35
  with gr.Column(scale=9):
36
  with gr.Accordion("Select from Gallery", open=False):
37
  search_civitai_gallery = gr.Gallery([], label="Results", allow_preview=False, columns=5, elem_id="gallery", show_share_button=False, interactive=False)
38
+ with gr.Accordion("Select by Checkbox", open=False):
39
+ search_civitai_result_checkbox = gr.CheckboxGroup(label="", choices=[], value=[])
40
  search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value=[],
41
  allow_custom_value=True, visible=True, multiselect=True)
42
+ search_civitai_result_info = gr.Markdown("Search result.", elem_classes="info")
43
  with gr.Column(scale=1):
44
  search_civitai_add = gr.Button("Add to download URLs")
45
  search_civitai_select_all = gr.Button("Select All", variant="secondary", size="sm")
 
74
  fn=search_civitai,
75
  inputs=[search_civitai_query, search_civitai_type, search_civitai_basemodel, search_civitai_sort,
76
  search_civitai_period, search_civitai_tag, search_civitai_user, search_civitai_limit, search_civitai_page, search_civitai_gallery],
77
+ outputs=[search_civitai_result, search_civitai_desc, search_civitai_submit, search_civitai_query, search_civitai_gallery, search_civitai_result_checkbox, search_civitai_result_info],
78
  queue=True,
79
  show_api=False,
80
  )
81
+ search_civitai_result.change(select_civitai_item, [search_civitai_result], [search_civitai_desc, search_civitai_json], queue=False, show_api=False)\
82
+ .success(update_civitai_checkbox, [search_civitai_result], [search_civitai_result_checkbox], queue=True, show_api=False)
83
+ search_civitai_result_checkbox.select(from_civitai_checkbox, [search_civitai_result_checkbox], [search_civitai_result], queue=False, show_api=False)
84
  search_civitai_add.click(add_civitai_item, [search_civitai_result, dl_url], [dl_url], queue=False, show_api=False)
85
  search_civitai_select_all.click(select_civitai_all_item, [search_civitai_select_all], [search_civitai_select_all, search_civitai_result], queue=False, show_api=False)
86
  search_civitai_gallery.select(update_civitai_selection, [search_civitai_result], [search_civitai_result], queue=False, show_api=False)
civitai_to_hf.py CHANGED
@@ -140,7 +140,7 @@ def download_civitai(dl_url, civitai_key, hf_token, urls,
140
 
141
 
142
  CIVITAI_TYPE = ["Checkpoint", "TextualInversion", "Hypernetwork", "AestheticGradient", "LORA", "Controlnet", "Poses"]
143
- CIVITAI_BASEMODEL = ["Pony", "Illustrious", "SDXL 1.0", "SD 1.5", "Flux.1 D", "Flux.1 S"]
144
  CIVITAI_SORT = ["Highest Rated", "Most Downloaded", "Newest"]
145
  CIVITAI_PERIOD = ["AllTime", "Year", "Month", "Week", "Day"]
146
 
@@ -200,27 +200,30 @@ def search_civitai(query, types, base_model=[], sort=CIVITAI_SORT[0], period=CIV
200
  civitai_last_choices = [("", "")]
201
  civitai_last_gallery = []
202
  civitai_last_results = {}
 
203
  items = search_on_civitai(query, types, base_model, int(limit), sort, period, tag, user, int(page))
204
  if not items: return gr.update(choices=[("", "")], value=[], visible=True),\
205
- gr.update(value="", visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
206
  choices = []
207
  gallery = []
208
  for item in items:
209
  base_model_name = "Pony🐴" if item['base_model'] == "Pony" else item['base_model']
210
- name = f"{item['name']} (for {base_model_name} / By: {item['creator']} / Tags: {', '.join(item['tags'])})"
 
211
  value = item['dl_url']
212
  choices.append((name, value))
213
  gallery.append((item['img_url'], name))
214
  civitai_last_results[value] = item
215
  if not choices: return gr.update(choices=[("", "")], value=[], visible=True),\
216
- gr.update(value="", visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
217
  #result = civitai_last_results.get(choices[0][1], "None") # for debug
218
  #md = result['md'] if result else "" # for debug
219
  md = ""
220
  civitai_last_choices = choices
221
  civitai_last_gallery = gallery
 
222
  return gr.update(choices=choices, value=[], visible=True), gr.update(value=md, visible=True),\
223
- gr.update(visible=True), gr.update(visible=True), gr.update(value=gallery)
224
 
225
 
226
  def get_civitai_json(dl_url: str, is_html: bool=False, image_baseurl: str=""):
@@ -321,5 +324,12 @@ def update_civitai_selection(evt: gr.SelectData, value: list[str]):
321
  selected = list_uniq([v for v in value if v != ""] + [civitai_last_choices[selected_index][1]])
322
  return gr.update(value=selected)
323
  except Exception:
324
- return gr.update(visible=True)
325
 
 
 
 
 
 
 
 
 
140
 
141
 
142
  CIVITAI_TYPE = ["Checkpoint", "TextualInversion", "Hypernetwork", "AestheticGradient", "LORA", "Controlnet", "Poses"]
143
+ CIVITAI_BASEMODEL = ["Pony", "Illustrious", "SDXL 1.0", "SD 1.5", "Flux.1 D", "Flux.1 S", "SD 3.5"]
144
  CIVITAI_SORT = ["Highest Rated", "Most Downloaded", "Newest"]
145
  CIVITAI_PERIOD = ["AllTime", "Year", "Month", "Week", "Day"]
146
 
 
200
  civitai_last_choices = [("", "")]
201
  civitai_last_gallery = []
202
  civitai_last_results = {}
203
+ results_info = "No item found."
204
  items = search_on_civitai(query, types, base_model, int(limit), sort, period, tag, user, int(page))
205
  if not items: return gr.update(choices=[("", "")], value=[], visible=True),\
206
+ gr.update(value="", visible=False), gr.update(), gr.update(), gr.update(), gr.update(), results_info
207
  choices = []
208
  gallery = []
209
  for item in items:
210
  base_model_name = "Pony🐴" if item['base_model'] == "Pony" else item['base_model']
211
+ #name = f"{item['name']} (for {base_model_name} / By: {item['creator']} / Tags: {', '.join(item['tags'])})"
212
+ name = f"{item['name']} (for {base_model_name} / By: {item['creator']})"
213
  value = item['dl_url']
214
  choices.append((name, value))
215
  gallery.append((item['img_url'], name))
216
  civitai_last_results[value] = item
217
  if not choices: return gr.update(choices=[("", "")], value=[], visible=True),\
218
+ gr.update(value="", visible=False), gr.update(), gr.update(), gr.update(), gr.update(), results_info
219
  #result = civitai_last_results.get(choices[0][1], "None") # for debug
220
  #md = result['md'] if result else "" # for debug
221
  md = ""
222
  civitai_last_choices = choices
223
  civitai_last_gallery = gallery
224
+ results_info = f"{int(len(choices))} items found."
225
  return gr.update(choices=choices, value=[], visible=True), gr.update(value=md, visible=True),\
226
+ gr.update(), gr.update(), gr.update(value=gallery), gr.update(choices=choices, value=[]), results_info
227
 
228
 
229
  def get_civitai_json(dl_url: str, is_html: bool=False, image_baseurl: str=""):
 
324
  selected = list_uniq([v for v in value if v != ""] + [civitai_last_choices[selected_index][1]])
325
  return gr.update(value=selected)
326
  except Exception:
327
+ return gr.update()
328
 
329
+
330
+ def update_civitai_checkbox(selected: list[str]):
331
+ return gr.update(value=selected)
332
+
333
+
334
+ def from_civitai_checkbox(selected: list[str]):
335
+ return gr.update(value=selected)