Spaces:
Running
Running
Upload 2 files
Browse files- app.py +8 -5
- civitai_to_hf.py +38 -5
app.py
CHANGED
@@ -1,10 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
-
from civitai_to_hf import search_civitai, download_civitai, select_civitai_item, add_civitai_item,
|
|
|
3 |
|
4 |
css = """
|
5 |
.title { font-size: 3em; align-items: center; text-align: center; }
|
6 |
.info { align-items: center; text-align: center; }
|
7 |
.block.result { margin: 1em 0; padding: 1em; box-shadow: 0 0 3px 3px #664422, 0 0 3px 2px #664422 inset; border-radius: 6px; background: #665544; }
|
|
|
8 |
"""
|
9 |
|
10 |
with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_cache=(60, 3600)) as demo:
|
@@ -19,10 +21,11 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
|
|
19 |
search_civitai_period = gr.Radio(label="Period", choices=CIVITAI_PERIOD, value="Month")
|
20 |
with gr.Row():
|
21 |
search_civitai_query = gr.Textbox(label="Query", placeholder="flux", lines=1)
|
22 |
-
search_civitai_tag = gr.
|
|
|
23 |
search_civitai_submit = gr.Button("Search on Civitai")
|
24 |
with gr.Row():
|
25 |
-
search_civitai_desc = gr.Markdown(value="", visible=False)
|
26 |
search_civitai_json = gr.JSON(value={}, visible=False)
|
27 |
search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value="", allow_custom_value=True, visible=False, multiselect=True)
|
28 |
search_civitai_add = gr.Button("Add to download URLs")
|
@@ -53,9 +56,9 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
|
|
53 |
outputs=[uploaded_urls, urls_md],
|
54 |
)
|
55 |
gr.on(
|
56 |
-
triggers=[search_civitai_submit.click, search_civitai_query.submit,
|
57 |
fn=search_civitai,
|
58 |
-
inputs=[search_civitai_query, search_civitai_type, search_civitai_basemodel, search_civitai_sort, search_civitai_period, search_civitai_tag],
|
59 |
outputs=[search_civitai_result, search_civitai_desc, search_civitai_submit, search_civitai_query],
|
60 |
queue=True,
|
61 |
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,
|
3 |
+
CIVITAI_TYPE, CIVITAI_BASEMODEL, CIVITAI_SORT, CIVITAI_PERIOD)
|
4 |
|
5 |
css = """
|
6 |
.title { font-size: 3em; align-items: center; text-align: center; }
|
7 |
.info { align-items: center; text-align: center; }
|
8 |
.block.result { margin: 1em 0; padding: 1em; box-shadow: 0 0 3px 3px #664422, 0 0 3px 2px #664422 inset; border-radius: 6px; background: #665544; }
|
9 |
+
.desc [src$='#float'] { float: left; margin: 10px; }
|
10 |
"""
|
11 |
|
12 |
with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_cache=(60, 3600)) as demo:
|
|
|
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")
|
|
|
56 |
outputs=[uploaded_urls, urls_md],
|
57 |
)
|
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,
|
civitai_to_hf.py
CHANGED
@@ -10,6 +10,7 @@ from utils import get_token, set_token, is_repo_exists, get_user_agent, get_down
|
|
10 |
import re
|
11 |
from PIL import Image
|
12 |
import json
|
|
|
13 |
|
14 |
|
15 |
TEMP_DIR = "."
|
@@ -130,7 +131,7 @@ CIVITAI_PERIOD = ["AllTime", "Year", "Month", "Week", "Day"]
|
|
130 |
|
131 |
|
132 |
def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [], limit: int = 100,
|
133 |
-
sort: str = "Highest Rated", period: str = "AllTime", tag: str = ""):
|
134 |
user_agent = get_user_agent()
|
135 |
headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
|
136 |
base_url = 'https://civitai.com/api/v1/models'
|
@@ -138,6 +139,7 @@ def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [],
|
|
138 |
if len(types) != 0: params["types"] = types
|
139 |
if query: params["query"] = query
|
140 |
if tag: params["tag"] = tag
|
|
|
141 |
session = requests.Session()
|
142 |
retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
|
143 |
session.mount("https://", HTTPAdapter(max_retries=retries))
|
@@ -160,10 +162,13 @@ def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [],
|
|
160 |
item['tags'] = j['tags'] if 'tags' in j.keys() else []
|
161 |
item['model_name'] = model['name'] if 'name' in model.keys() else ""
|
162 |
item['base_model'] = model['baseModel'] if 'baseModel' in model.keys() else ""
|
|
|
163 |
item['dl_url'] = model['downloadUrl']
|
|
|
164 |
if 'images' in model.keys() and len(model["images"]) != 0:
|
165 |
-
item['md']
|
166 |
-
|
|
|
167 |
items.append(item)
|
168 |
return items
|
169 |
|
@@ -171,9 +176,9 @@ def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [],
|
|
171 |
civitai_last_results = {}
|
172 |
|
173 |
|
174 |
-
def search_civitai(query, types, base_model=[], sort=CIVITAI_SORT[0], period=CIVITAI_PERIOD[0], tag=""):
|
175 |
global civitai_last_results
|
176 |
-
items = search_on_civitai(query, types, base_model, 100, sort, period, tag)
|
177 |
if not items: return gr.update(choices=[("", "")], value="", visible=False),\
|
178 |
gr.update(value="", visible=False), gr.update(visible=True), gr.update(visible=True)
|
179 |
civitai_last_results = {}
|
@@ -230,6 +235,34 @@ def get_civitai_json(dl_url: str, is_html: bool=False, image_baseurl: str=""):
|
|
230 |
return default
|
231 |
|
232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
def select_civitai_item(results: list[str]):
|
234 |
if "http" not in "".join(results): return gr.update(value="None", visible=True)
|
235 |
result = civitai_last_results.get(results[-1], "None")
|
|
|
10 |
import re
|
11 |
from PIL import Image
|
12 |
import json
|
13 |
+
import pandas as pd
|
14 |
|
15 |
|
16 |
TEMP_DIR = "."
|
|
|
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'
|
|
|
139 |
if len(types) != 0: params["types"] = types
|
140 |
if query: params["query"] = query
|
141 |
if tag: params["tag"] = tag
|
142 |
+
if user: params["username"] = user
|
143 |
session = requests.Session()
|
144 |
retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
|
145 |
session.mount("https://", HTTPAdapter(max_retries=retries))
|
|
|
162 |
item['tags'] = j['tags'] if 'tags' in j.keys() else []
|
163 |
item['model_name'] = model['name'] if 'name' in model.keys() else ""
|
164 |
item['base_model'] = model['baseModel'] if 'baseModel' in model.keys() else ""
|
165 |
+
item['description'] = model['description'] if 'description' in model.keys() else ""
|
166 |
item['dl_url'] = model['downloadUrl']
|
167 |
+
item['md'] = ""
|
168 |
if 'images' in model.keys() and len(model["images"]) != 0:
|
169 |
+
item['md'] += f'<img src="{model["images"][0]["url"]}#float" alt="thumbnail" width="150" height="240"><br>'
|
170 |
+
item['md'] += f'''Model URL: [https://civitai.com/models/{j["id"]}](https://civitai.com/models/{j["id"]})<br>Model Name: {item["name"]}<br>
|
171 |
+
Creator: {item["creator"]}<br>Tags: {", ".join(item["tags"])}<br>Base Model: {item["base_model"]}<br>Description: {item["description"]}'''
|
172 |
items.append(item)
|
173 |
return items
|
174 |
|
|
|
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 = {}
|
|
|
235 |
return default
|
236 |
|
237 |
|
238 |
+
def get_civitai_tag():
|
239 |
+
default = [""]
|
240 |
+
user_agent = get_user_agent()
|
241 |
+
headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
|
242 |
+
base_url = 'https://civitai.com/api/v1/tags'
|
243 |
+
params = {'limit': 200}
|
244 |
+
session = requests.Session()
|
245 |
+
retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
|
246 |
+
session.mount("https://", HTTPAdapter(max_retries=retries))
|
247 |
+
url = base_url
|
248 |
+
try:
|
249 |
+
r = session.get(url, params=params, headers=headers, stream=True, timeout=(3.0, 15))
|
250 |
+
if not r.ok: return default
|
251 |
+
j = dict(r.json()).copy()
|
252 |
+
if "items" not in j.keys(): return default
|
253 |
+
items = []
|
254 |
+
for item in j["items"]:
|
255 |
+
items.append([str(item.get("name", "")), int(item.get("modelCount", 0))])
|
256 |
+
df = pd.DataFrame(items)
|
257 |
+
df.sort_values(1, ascending=False)
|
258 |
+
tags = df.values.tolist()
|
259 |
+
tags = [""] + [l[0] for l in tags]
|
260 |
+
return tags
|
261 |
+
except Exception as e:
|
262 |
+
print(e)
|
263 |
+
return default
|
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")
|