John6666 commited on
Commit
82ce3ab
1 Parent(s): 4bee8c1

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +17 -7
  2. civitai_to_hf.py +25 -14
  3. utils.py +1 -1
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from civitai_to_hf import (search_civitai, download_civitai, select_civitai_item, add_civitai_item, get_civitai_tag,
3
  CIVITAI_TYPE, CIVITAI_BASEMODEL, CIVITAI_SORT, CIVITAI_PERIOD)
4
 
5
  css = """
@@ -19,16 +19,24 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
19
  with gr.Row():
20
  search_civitai_sort = gr.Radio(label="Sort", choices=CIVITAI_SORT, value=CIVITAI_SORT[0])
21
  search_civitai_period = gr.Radio(label="Period", choices=CIVITAI_PERIOD, value="Month")
 
 
22
  with gr.Row():
23
  search_civitai_query = gr.Textbox(label="Query", placeholder="flux", lines=1)
24
  search_civitai_tag = gr.Dropdown(label="Tag", choices=get_civitai_tag(), value=get_civitai_tag()[0], allow_custom_value=True)
25
  search_civitai_user = gr.Textbox(label="Username", lines=1)
26
  search_civitai_submit = gr.Button("Search on Civitai")
27
- with gr.Row():
28
- search_civitai_desc = gr.Markdown(value="", visible=False, elem_classes="desc")
29
- search_civitai_json = gr.JSON(value={}, visible=False)
30
- search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value="", allow_custom_value=True, visible=False, multiselect=True)
31
- search_civitai_add = gr.Button("Add to download URLs")
 
 
 
 
 
 
32
  with gr.Group():
33
  dl_url = gr.Textbox(label="Download URL(s)", placeholder="https://civitai.com/api/download/models/28907\n...", value="", lines=3, max_lines=255)
34
  with gr.Column():
@@ -58,13 +66,15 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
58
  gr.on(
59
  triggers=[search_civitai_submit.click, search_civitai_query.submit, search_civitai_user.submit],
60
  fn=search_civitai,
61
- inputs=[search_civitai_query, search_civitai_type, search_civitai_basemodel, search_civitai_sort, search_civitai_period, search_civitai_tag, search_civitai_user],
 
62
  outputs=[search_civitai_result, search_civitai_desc, search_civitai_submit, search_civitai_query],
63
  queue=True,
64
  show_api=False,
65
  )
66
  search_civitai_result.change(select_civitai_item, [search_civitai_result], [search_civitai_desc, search_civitai_json], queue=False, show_api=False)
67
  search_civitai_add.click(add_civitai_item, [search_civitai_result, dl_url], [dl_url], queue=False, show_api=False)
 
68
 
69
  demo.queue()
70
  demo.launch()
 
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
  CIVITAI_TYPE, CIVITAI_BASEMODEL, CIVITAI_SORT, CIVITAI_PERIOD)
4
 
5
  css = """
 
19
  with gr.Row():
20
  search_civitai_sort = gr.Radio(label="Sort", choices=CIVITAI_SORT, value=CIVITAI_SORT[0])
21
  search_civitai_period = gr.Radio(label="Period", choices=CIVITAI_PERIOD, value="Month")
22
+ search_civitai_limit = gr.Slider(label="Limit", minimum=1, maximum=100, step=1, value=100)
23
+ search_civitai_page = gr.Slider(label="Page", minimum=1, maximum=100, step=1, value=1)
24
  with gr.Row():
25
  search_civitai_query = gr.Textbox(label="Query", placeholder="flux", lines=1)
26
  search_civitai_tag = gr.Dropdown(label="Tag", choices=get_civitai_tag(), value=get_civitai_tag()[0], allow_custom_value=True)
27
  search_civitai_user = gr.Textbox(label="Username", lines=1)
28
  search_civitai_submit = gr.Button("Search on Civitai")
29
+ with gr.Accordion("Results", open=True):
30
+ with gr.Row():
31
+ search_civitai_desc = gr.Markdown(value="", visible=False, elem_classes="desc")
32
+ search_civitai_json = gr.JSON(value={}, visible=False)
33
+ with gr.Row():
34
+ with gr.Column(scale=9):
35
+ search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value=[],
36
+ allow_custom_value=True, visible=True, multiselect=True)
37
+ with gr.Column(scale=1):
38
+ search_civitai_add = gr.Button("Add to download URLs")
39
+ search_civitai_select_all = gr.Button("Select All", variant="secondary", size="sm")
40
  with gr.Group():
41
  dl_url = gr.Textbox(label="Download URL(s)", placeholder="https://civitai.com/api/download/models/28907\n...", value="", lines=3, max_lines=255)
42
  with gr.Column():
 
66
  gr.on(
67
  triggers=[search_civitai_submit.click, search_civitai_query.submit, search_civitai_user.submit],
68
  fn=search_civitai,
69
+ inputs=[search_civitai_query, search_civitai_type, search_civitai_basemodel, search_civitai_sort,
70
+ search_civitai_period, search_civitai_tag, search_civitai_user, search_civitai_limit, search_civitai_page],
71
  outputs=[search_civitai_result, search_civitai_desc, search_civitai_submit, search_civitai_query],
72
  queue=True,
73
  show_api=False,
74
  )
75
  search_civitai_result.change(select_civitai_item, [search_civitai_result], [search_civitai_desc, search_civitai_json], queue=False, show_api=False)
76
  search_civitai_add.click(add_civitai_item, [search_civitai_result, dl_url], [dl_url], queue=False, show_api=False)
77
+ 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)
78
 
79
  demo.queue()
80
  demo.launch()
civitai_to_hf.py CHANGED
@@ -131,11 +131,11 @@ CIVITAI_PERIOD = ["AllTime", "Year", "Month", "Week", "Day"]
131
 
132
 
133
  def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [], limit: int = 100,
134
- sort: str = "Highest Rated", period: str = "AllTime", tag: str = "", user: str = ""):
135
  user_agent = get_user_agent()
136
  headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
137
  base_url = 'https://civitai.com/api/v1/models'
138
- params = {'sort': sort, 'period': period, 'limit': limit, 'nsfw': 'true'}
139
  if len(types) != 0: params["types"] = types
140
  if query: params["query"] = query
141
  if tag: params["tag"] = tag
@@ -174,14 +174,16 @@ def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [],
174
 
175
 
176
  civitai_last_results = {}
 
177
 
178
 
179
- def search_civitai(query, types, base_model=[], sort=CIVITAI_SORT[0], period=CIVITAI_PERIOD[0], tag="", user=""):
180
- global civitai_last_results
181
- items = search_on_civitai(query, types, base_model, 100, sort, period, tag, user)
182
- if not items: return gr.update(choices=[("", "")], value="", visible=False),\
183
- gr.update(value="", visible=False), gr.update(visible=True), gr.update(visible=True)
184
  civitai_last_results = {}
 
 
 
185
  choices = []
186
  for item in items:
187
  base_model_name = "Pony🐴" if item['base_model'] == "Pony" else item['base_model']
@@ -189,11 +191,13 @@ def search_civitai(query, types, base_model=[], sort=CIVITAI_SORT[0], period=CIV
189
  value = item['dl_url']
190
  choices.append((name, value))
191
  civitai_last_results[value] = item
192
- if not choices: return gr.update(choices=[("", "")], value="", visible=False),\
193
  gr.update(value="", visible=False), gr.update(visible=True), gr.update(visible=True)
194
- result = civitai_last_results.get(choices[0][1], "None")
195
- md = result['md'] if result else ""
196
- return gr.update(choices=choices, value=choices[0][1], visible=True), gr.update(value=md, visible=True),\
 
 
197
  gr.update(visible=True), gr.update(visible=True)
198
 
199
 
@@ -264,10 +268,10 @@ def get_civitai_tag():
264
 
265
 
266
  def select_civitai_item(results: list[str]):
267
- if "http" not in "".join(results): return gr.update(value="None", visible=True)
268
- result = civitai_last_results.get(results[-1], "None")
269
- md = result['md'] if result else ""
270
  json = {}
 
 
 
271
  #json, html, image = get_civitai_json(results[-1], True) # for debugging
272
  return gr.update(value=md, visible=True), gr.update(value=json, visible=False)
273
 
@@ -280,3 +284,10 @@ def add_civitai_item(results: list[str], dl_url: str):
280
  new_url += f"\n{result}" if new_url else f"{result}"
281
  new_url = uniq_urls(new_url)
282
  return gr.update(value=new_url)
 
 
 
 
 
 
 
 
131
 
132
 
133
  def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [], limit: int = 100,
134
+ sort: str = "Highest Rated", period: str = "AllTime", tag: str = "", user: str = "", page: int = 1):
135
  user_agent = get_user_agent()
136
  headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
137
  base_url = 'https://civitai.com/api/v1/models'
138
+ params = {'sort': sort, 'period': period, 'limit': int(limit), 'page': int(page),'nsfw': 'true'}
139
  if len(types) != 0: params["types"] = types
140
  if query: params["query"] = query
141
  if tag: params["tag"] = tag
 
174
 
175
 
176
  civitai_last_results = {}
177
+ civitai_last_choices = [("", "")]
178
 
179
 
180
+ def search_civitai(query, types, base_model=[], sort=CIVITAI_SORT[0], period=CIVITAI_PERIOD[0], tag="", user="", limit=100, page=1):
181
+ global civitai_last_results, civitai_last_choices
182
+ civitai_last_choices = [("", "")]
 
 
183
  civitai_last_results = {}
184
+ items = search_on_civitai(query, types, base_model, int(limit), sort, period, tag, user, int(page))
185
+ if not items: return gr.update(choices=[("", "")], value=[], visible=True),\
186
+ gr.update(value="", visible=False), gr.update(visible=True), gr.update(visible=True)
187
  choices = []
188
  for item in items:
189
  base_model_name = "Pony🐴" if item['base_model'] == "Pony" else item['base_model']
 
191
  value = item['dl_url']
192
  choices.append((name, value))
193
  civitai_last_results[value] = item
194
+ if not choices: return gr.update(choices=[("", "")], value=[], visible=True),\
195
  gr.update(value="", visible=False), gr.update(visible=True), gr.update(visible=True)
196
+ #result = civitai_last_results.get(choices[0][1], "None") # for debug
197
+ #md = result['md'] if result else "" # for debug
198
+ md = ""
199
+ civitai_last_choices = choices
200
+ return gr.update(choices=choices, value=[], visible=True), gr.update(value=md, visible=True),\
201
  gr.update(visible=True), gr.update(visible=True)
202
 
203
 
 
268
 
269
 
270
  def select_civitai_item(results: list[str]):
 
 
 
271
  json = {}
272
+ if "http" not in "".join(results) or len(results) == 0: return gr.update(value="", visible=True), gr.update(value=json, visible=False)
273
+ result = civitai_last_results.get(results[-1], None)
274
+ md = result['md'] if result else ""
275
  #json, html, image = get_civitai_json(results[-1], True) # for debugging
276
  return gr.update(value=md, visible=True), gr.update(value=json, visible=False)
277
 
 
284
  new_url += f"\n{result}" if new_url else f"{result}"
285
  new_url = uniq_urls(new_url)
286
  return gr.update(value=new_url)
287
+
288
+
289
+ def select_civitai_all_item(button_name: str):
290
+ if button_name not in ["Select All", "Deselect All"]: return gr.update(value=button_name), gr.Update(visible=True)
291
+ selected = [t[1] for t in civitai_last_choices if t[1] != ""] if button_name == "Select All" else []
292
+ new_button_name = "Select All" if button_name == "Deselect All" else "Deselect All"
293
+ return gr.update(value=new_button_name), gr.update(value=selected, choices=civitai_last_choices)
utils.py CHANGED
@@ -77,7 +77,7 @@ def is_repo_name(s):
77
 
78
  def split_hf_url(url: str):
79
  try:
80
- s = list(re.findall(r'^(?:https?://huggingface.co/)(?:(datasets)/)?(.+?/.+?)/\w+?/.+?/(?:(.+)/)?(.+?.safetensors)(?:\?download=true)?$', url)[0])
81
  if len(s) < 4: return "", "", "", ""
82
  repo_id = s[1]
83
  repo_type = "dataset" if s[0] == "datasets" else "model"
 
77
 
78
  def split_hf_url(url: str):
79
  try:
80
+ s = list(re.findall(r'^(?:https?://huggingface.co/)(?:(datasets)/)?(.+?/.+?)/\w+?/.+?/(?:(.+)/)?(.+?.\w+)(?:\?download=true)?$', url)[0])
81
  if len(s) < 4: return "", "", "", ""
82
  repo_id = s[1]
83
  repo_type = "dataset" if s[0] == "datasets" else "model"