Spaces:
Running
Running
Upload 2 files
Browse files- app.py +4 -3
- civitai_to_hf.py +36 -10
app.py
CHANGED
|
@@ -21,8 +21,8 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
|
|
| 21 |
with gr.Row():
|
| 22 |
search_civitai_sort = gr.Radio(label="Sort", choices=CIVITAI_SORT, value=CIVITAI_SORT[0])
|
| 23 |
search_civitai_period = gr.Radio(label="Period", choices=CIVITAI_PERIOD, value="Month")
|
| 24 |
-
search_civitai_limit = gr.
|
| 25 |
-
search_civitai_page = gr.
|
| 26 |
with gr.Row(equal_height=True):
|
| 27 |
search_civitai_query = gr.Textbox(label="Query", placeholder="flux", lines=1)
|
| 28 |
search_civitai_tag = gr.Dropdown(label="Tag", choices=get_civitai_tag(), value=get_civitai_tag()[0], allow_custom_value=True)
|
|
@@ -70,6 +70,7 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
|
|
| 70 |
fn=download_civitai,
|
| 71 |
inputs=[dl_url, civitai_key, hf_token, uploaded_urls, newrepo_id, newrepo_type, is_private, is_info],
|
| 72 |
outputs=[uploaded_urls, urls_md, urls_remain],
|
|
|
|
| 73 |
)
|
| 74 |
gr.on(
|
| 75 |
triggers=[search_civitai_submit.click, search_civitai_query.submit, search_civitai_user.submit],
|
|
@@ -78,7 +79,7 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
|
|
| 78 |
search_civitai_period, search_civitai_tag, search_civitai_user, search_civitai_limit, search_civitai_page, search_civitai_gallery, state],
|
| 79 |
outputs=[search_civitai_result, search_civitai_desc, search_civitai_submit, search_civitai_query, search_civitai_gallery,
|
| 80 |
search_civitai_result_checkbox, search_civitai_result_info, state],
|
| 81 |
-
queue=
|
| 82 |
show_api=False,
|
| 83 |
)
|
| 84 |
search_civitai_result.change(select_civitai_item, [search_civitai_result, state], [search_civitai_desc, search_civitai_json], queue=False, show_api=False)\
|
|
|
|
| 21 |
with gr.Row():
|
| 22 |
search_civitai_sort = gr.Radio(label="Sort", choices=CIVITAI_SORT, value=CIVITAI_SORT[0])
|
| 23 |
search_civitai_period = gr.Radio(label="Period", choices=CIVITAI_PERIOD, value="Month")
|
| 24 |
+
search_civitai_limit = gr.Number(label="Limit", minimum=1, maximum=100, step=1, value=100)
|
| 25 |
+
search_civitai_page = gr.Number(label="Page", info="If 0, retrieve all pages", minimum=0, maximum=10, step=1, value=1)
|
| 26 |
with gr.Row(equal_height=True):
|
| 27 |
search_civitai_query = gr.Textbox(label="Query", placeholder="flux", lines=1)
|
| 28 |
search_civitai_tag = gr.Dropdown(label="Tag", choices=get_civitai_tag(), value=get_civitai_tag()[0], allow_custom_value=True)
|
|
|
|
| 70 |
fn=download_civitai,
|
| 71 |
inputs=[dl_url, civitai_key, hf_token, uploaded_urls, newrepo_id, newrepo_type, is_private, is_info],
|
| 72 |
outputs=[uploaded_urls, urls_md, urls_remain],
|
| 73 |
+
queue=True,
|
| 74 |
)
|
| 75 |
gr.on(
|
| 76 |
triggers=[search_civitai_submit.click, search_civitai_query.submit, search_civitai_user.submit],
|
|
|
|
| 79 |
search_civitai_period, search_civitai_tag, search_civitai_user, search_civitai_limit, search_civitai_page, search_civitai_gallery, state],
|
| 80 |
outputs=[search_civitai_result, search_civitai_desc, search_civitai_submit, search_civitai_query, search_civitai_gallery,
|
| 81 |
search_civitai_result_checkbox, search_civitai_result_info, state],
|
| 82 |
+
queue=False,
|
| 83 |
show_api=False,
|
| 84 |
)
|
| 85 |
search_civitai_result.change(select_civitai_item, [search_civitai_result, state], [search_civitai_desc, search_civitai_json], queue=False, show_api=False)\
|
civitai_to_hf.py
CHANGED
|
@@ -153,28 +153,54 @@ CIVITAI_PERIOD = ["AllTime", "Year", "Month", "Week", "Day"]
|
|
| 153 |
|
| 154 |
|
| 155 |
def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [], limit: int = 100,
|
| 156 |
-
sort: str = "Highest Rated", period: str = "AllTime", tag: str = "", user: str = "", page: int = 1):
|
| 157 |
user_agent = get_user_agent()
|
| 158 |
headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
|
| 159 |
base_url = 'https://civitai.com/api/v1/models'
|
| 160 |
-
params = {'sort': sort, 'period': period, 'limit': int(limit), '
|
| 161 |
if len(types) != 0: params["types"] = types
|
| 162 |
if query: params["query"] = query
|
| 163 |
if tag: params["tag"] = tag
|
| 164 |
if user: params["username"] = user
|
|
|
|
| 165 |
session = requests.Session()
|
| 166 |
retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
|
| 167 |
session.mount("https://", HTTPAdapter(max_retries=retries))
|
|
|
|
| 168 |
try:
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
except Exception as e:
|
| 171 |
print(e)
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
if not r.ok:
|
| 175 |
json = r.json()
|
| 176 |
-
if 'items' not in json:
|
| 177 |
-
items = []
|
| 178 |
for j in json['items']:
|
| 179 |
for model in j['modelVersions']:
|
| 180 |
item = {}
|
|
@@ -194,10 +220,10 @@ def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [],
|
|
| 194 |
item['md'] += f'''Model URL: [https://civitai.com/models/{j["id"]}](https://civitai.com/models/{j["id"]})<br>Model Name: {item["name"]}<br>
|
| 195 |
Creator: {item["creator"]}<br>Tags: {", ".join(item["tags"])}<br>Base Model: {item["base_model"]}<br>Description: {item["description"]}'''
|
| 196 |
items.append(item)
|
| 197 |
-
|
| 198 |
|
| 199 |
|
| 200 |
-
def search_civitai(query, types, base_model=[], sort=CIVITAI_SORT[0], period=CIVITAI_PERIOD[0], tag="", user="", limit=100, page=1, gallery=[], state={}):
|
| 201 |
civitai_last_results = {}
|
| 202 |
set_state(state, "civitai_last_choices", [("", "")])
|
| 203 |
set_state(state, "civitai_last_gallery", [])
|
|
|
|
| 153 |
|
| 154 |
|
| 155 |
def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [], limit: int = 100,
|
| 156 |
+
sort: str = "Highest Rated", period: str = "AllTime", tag: str = "", user: str = "", page: int = 1, progress=gr.Progress(track_tqdm=True)):
|
| 157 |
user_agent = get_user_agent()
|
| 158 |
headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
|
| 159 |
base_url = 'https://civitai.com/api/v1/models'
|
| 160 |
+
params = {'sort': sort, 'period': period, 'limit': int(limit), 'nsfw': 'true'}
|
| 161 |
if len(types) != 0: params["types"] = types
|
| 162 |
if query: params["query"] = query
|
| 163 |
if tag: params["tag"] = tag
|
| 164 |
if user: params["username"] = user
|
| 165 |
+
if page != 0: params["page"] = int(page)
|
| 166 |
session = requests.Session()
|
| 167 |
retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
|
| 168 |
session.mount("https://", HTTPAdapter(max_retries=retries))
|
| 169 |
+
rs = []
|
| 170 |
try:
|
| 171 |
+
if page == 0:
|
| 172 |
+
progress(0, desc="Searching page 1...")
|
| 173 |
+
print("Searching page 1...")
|
| 174 |
+
r = session.get(base_url, params=params | {'page': 1}, headers=headers, stream=True, timeout=(7.0, 30))
|
| 175 |
+
rs.append(r)
|
| 176 |
+
if r.ok:
|
| 177 |
+
json = r.json()
|
| 178 |
+
next_url = json['metadata']['nextPage'] if 'metadata' in json and 'nextPage' in json['metadata'] else None
|
| 179 |
+
i = 2
|
| 180 |
+
while(next_url is not None):
|
| 181 |
+
progress(0, desc=f"Searching page {i}...")
|
| 182 |
+
print(f"Searching page {i}...")
|
| 183 |
+
r = session.get(next_url, headers=headers, stream=True, timeout=(7.0, 30))
|
| 184 |
+
rs.append(r)
|
| 185 |
+
if r.ok:
|
| 186 |
+
json = r.json()
|
| 187 |
+
next_url = json['metadata']['nextPage'] if 'metadata' in json and 'nextPage' in json['metadata'] else None
|
| 188 |
+
else: next_url = None
|
| 189 |
+
i += 1
|
| 190 |
+
else:
|
| 191 |
+
progress(0, desc="Searching page 1...")
|
| 192 |
+
print("Searching page 1...")
|
| 193 |
+
r = session.get(base_url, params=params, headers=headers, stream=True, timeout=(7.0, 30))
|
| 194 |
+
rs.append(r)
|
| 195 |
+
except requests.exceptions.ConnectTimeout:
|
| 196 |
+
print("Request timed out.")
|
| 197 |
except Exception as e:
|
| 198 |
print(e)
|
| 199 |
+
items = []
|
| 200 |
+
for r in rs:
|
| 201 |
+
if not r.ok: continue
|
| 202 |
json = r.json()
|
| 203 |
+
if 'items' not in json: continue
|
|
|
|
| 204 |
for j in json['items']:
|
| 205 |
for model in j['modelVersions']:
|
| 206 |
item = {}
|
|
|
|
| 220 |
item['md'] += f'''Model URL: [https://civitai.com/models/{j["id"]}](https://civitai.com/models/{j["id"]})<br>Model Name: {item["name"]}<br>
|
| 221 |
Creator: {item["creator"]}<br>Tags: {", ".join(item["tags"])}<br>Base Model: {item["base_model"]}<br>Description: {item["description"]}'''
|
| 222 |
items.append(item)
|
| 223 |
+
return items if len(items) > 0 else None
|
| 224 |
|
| 225 |
|
| 226 |
+
def search_civitai(query, types, base_model=[], sort=CIVITAI_SORT[0], period=CIVITAI_PERIOD[0], tag="", user="", limit=100, page=1, gallery=[], state={}, progress=gr.Progress(track_tqdm=True)):
|
| 227 |
civitai_last_results = {}
|
| 228 |
set_state(state, "civitai_last_choices", [("", "")])
|
| 229 |
set_state(state, "civitai_last_gallery", [])
|