Spaces:
Running
on
Zero
Running
on
Zero
Delete flux.py
Browse files
flux.py
DELETED
@@ -1,211 +0,0 @@
|
|
1 |
-
import spaces
|
2 |
-
import os
|
3 |
-
import gradio as gr
|
4 |
-
import json
|
5 |
-
import logging
|
6 |
-
logging.getLogger("diffusers").setLevel(logging.ERROR)
|
7 |
-
import diffusers
|
8 |
-
diffusers.utils.logging.set_verbosity(40)
|
9 |
-
import warnings
|
10 |
-
warnings.filterwarnings(action="ignore", category=FutureWarning, module="diffusers")
|
11 |
-
warnings.filterwarnings(action="ignore", category=UserWarning, module="diffusers")
|
12 |
-
warnings.filterwarnings(action="ignore", category=FutureWarning, module="transformers")
|
13 |
-
from pathlib import Path
|
14 |
-
from huggingface_hub import HfApi
|
15 |
-
from env import (HF_TOKEN, hf_read_token, # to use only for private repos
|
16 |
-
CIVITAI_API_KEY, HF_LORA_PRIVATE_REPOS1, HF_LORA_PRIVATE_REPOS2, HF_LORA_ESSENTIAL_PRIVATE_REPO,
|
17 |
-
HF_VAE_PRIVATE_REPO, directory_models, directory_loras, directory_vaes,
|
18 |
-
download_model_list, download_lora_list, download_vae_list)
|
19 |
-
from modutils import (to_list, list_uniq, list_sub, get_lora_model_list, download_private_repo,
|
20 |
-
safe_float, escape_lora_basename, to_lora_key, to_lora_path, get_local_model_list, download_things,
|
21 |
-
get_private_lora_model_lists, get_valid_lora_name, get_valid_lora_path, get_valid_lora_wt,
|
22 |
-
get_lora_info, normalize_prompt_list, get_civitai_info, search_lora_on_civitai, MODEL_TYPE_DICT)
|
23 |
-
|
24 |
-
|
25 |
-
# - **Download Models**
|
26 |
-
download_model = ", ".join(download_model_list)
|
27 |
-
# - **Download VAEs**
|
28 |
-
download_vae = ", ".join(download_vae_list)
|
29 |
-
# - **Download LoRAs**
|
30 |
-
download_lora = ", ".join(download_lora_list)
|
31 |
-
|
32 |
-
#download_private_repo(HF_LORA_ESSENTIAL_PRIVATE_REPO, directory_loras, True)
|
33 |
-
#download_private_repo(HF_VAE_PRIVATE_REPO, directory_vaes, False)
|
34 |
-
|
35 |
-
CIVITAI_API_KEY = os.environ.get("CIVITAI_API_KEY")
|
36 |
-
hf_token = os.environ.get("HF_TOKEN")
|
37 |
-
|
38 |
-
# Download stuffs
|
39 |
-
for url in [url.strip() for url in download_model.split(',')]:
|
40 |
-
if not os.path.exists(f"./models/{url.split('/')[-1]}"):
|
41 |
-
download_things(directory_models, url, hf_token, CIVITAI_API_KEY)
|
42 |
-
for url in [url.strip() for url in download_vae.split(',')]:
|
43 |
-
if not os.path.exists(f"./vaes/{url.split('/')[-1]}"):
|
44 |
-
download_things(directory_vaes, url, hf_token, CIVITAI_API_KEY)
|
45 |
-
for url in [url.strip() for url in download_lora.split(',')]:
|
46 |
-
if not os.path.exists(f"./loras/{url.split('/')[-1]}"):
|
47 |
-
download_things(directory_loras, url, hf_token, CIVITAI_API_KEY)
|
48 |
-
|
49 |
-
lora_model_list = get_lora_model_list()
|
50 |
-
vae_model_list = get_local_model_list(directory_vaes)
|
51 |
-
vae_model_list.insert(0, "None")
|
52 |
-
|
53 |
-
|
54 |
-
private_lora_dict = {"": ["", "", "", "", ""]}
|
55 |
-
try:
|
56 |
-
with open('lora_dict.json', encoding='utf-8') as f:
|
57 |
-
d = json.load(f)
|
58 |
-
for k, v in d.items():
|
59 |
-
private_lora_dict[escape_lora_basename(k)] = v
|
60 |
-
except Exception:
|
61 |
-
pass
|
62 |
-
|
63 |
-
|
64 |
-
private_lora_model_list = get_private_lora_model_lists()
|
65 |
-
loras_dict = {"None": ["", "", "", "", ""], "": ["", "", "", "", ""]} | private_lora_dict.copy()
|
66 |
-
loras_url_to_path_dict = {} # {"URL to download": "local filepath", ...}
|
67 |
-
civitai_lora_last_results = {} # {"URL to download": {search results}, ...}
|
68 |
-
all_lora_list = []
|
69 |
-
|
70 |
-
|
71 |
-
def get_all_lora_list():
|
72 |
-
global all_lora_list
|
73 |
-
loras = get_lora_model_list()
|
74 |
-
all_lora_list = loras.copy()
|
75 |
-
return loras
|
76 |
-
|
77 |
-
|
78 |
-
def get_all_lora_tupled_list():
|
79 |
-
global loras_dict
|
80 |
-
models = get_all_lora_list()
|
81 |
-
if not models: return []
|
82 |
-
tupled_list = []
|
83 |
-
for model in models:
|
84 |
-
#if not model: continue # to avoid GUI-related bug
|
85 |
-
basename = Path(model).stem
|
86 |
-
key = to_lora_key(model)
|
87 |
-
items = None
|
88 |
-
if key in loras_dict.keys():
|
89 |
-
items = loras_dict.get(key, None)
|
90 |
-
else:
|
91 |
-
items = get_civitai_info(model)
|
92 |
-
if items != None:
|
93 |
-
loras_dict[key] = items
|
94 |
-
name = basename
|
95 |
-
value = model
|
96 |
-
if items and items[2] != "":
|
97 |
-
if items[1] == "Pony":
|
98 |
-
name = f"{basename} (for {items[1]}🐴, {items[2]})"
|
99 |
-
else:
|
100 |
-
name = f"{basename} (for {items[1]}, {items[2]})"
|
101 |
-
tupled_list.append((name, value))
|
102 |
-
return tupled_list
|
103 |
-
|
104 |
-
|
105 |
-
def update_lora_dict(path: str):
|
106 |
-
global loras_dict
|
107 |
-
key = to_lora_key(path)
|
108 |
-
if key in loras_dict.keys(): return
|
109 |
-
items = get_civitai_info(path)
|
110 |
-
if items == None: return
|
111 |
-
loras_dict[key] = items
|
112 |
-
|
113 |
-
|
114 |
-
def download_lora(dl_urls: str):
|
115 |
-
global loras_url_to_path_dict
|
116 |
-
dl_path = ""
|
117 |
-
before = get_local_model_list(directory_loras)
|
118 |
-
urls = []
|
119 |
-
for url in [url.strip() for url in dl_urls.split(',')]:
|
120 |
-
local_path = f"{directory_loras}/{url.split('/')[-1]}"
|
121 |
-
if not Path(local_path).exists():
|
122 |
-
download_things(directory_loras, url, hf_token, CIVITAI_API_KEY)
|
123 |
-
urls.append(url)
|
124 |
-
after = get_local_model_list(directory_loras)
|
125 |
-
new_files = list_sub(after, before)
|
126 |
-
for i, file in enumerate(new_files):
|
127 |
-
path = Path(file)
|
128 |
-
if path.exists():
|
129 |
-
new_path = Path(f'{path.parent.name}/{escape_lora_basename(path.stem)}{path.suffix}')
|
130 |
-
path.resolve().rename(new_path.resolve())
|
131 |
-
loras_url_to_path_dict[urls[i]] = str(new_path)
|
132 |
-
update_lora_dict(str(new_path))
|
133 |
-
dl_path = str(new_path)
|
134 |
-
return dl_path
|
135 |
-
|
136 |
-
|
137 |
-
def copy_lora(path: str, new_path: str):
|
138 |
-
import shutil
|
139 |
-
if path == new_path: return new_path
|
140 |
-
cpath = Path(path)
|
141 |
-
npath = Path(new_path)
|
142 |
-
if cpath.exists():
|
143 |
-
try:
|
144 |
-
shutil.copy(str(cpath.resolve()), str(npath.resolve()))
|
145 |
-
except Exception:
|
146 |
-
return None
|
147 |
-
update_lora_dict(str(npath))
|
148 |
-
return new_path
|
149 |
-
else:
|
150 |
-
return None
|
151 |
-
|
152 |
-
|
153 |
-
def download_my_lora(dl_urls: str, lora):
|
154 |
-
path = download_lora(dl_urls)
|
155 |
-
if path: lora = path
|
156 |
-
choices = get_all_lora_tupled_list()
|
157 |
-
return gr.update(value=lora, choices=choices)
|
158 |
-
|
159 |
-
|
160 |
-
def apply_lora_prompt(lora_info: str):
|
161 |
-
if lora_info == "None": return ""
|
162 |
-
lora_tag = lora_info.replace("/",",")
|
163 |
-
lora_tags = lora_tag.split(",") if str(lora_info) != "None" else []
|
164 |
-
lora_prompts = normalize_prompt_list(lora_tags)
|
165 |
-
prompt = ", ".join(list_uniq(lora_prompts))
|
166 |
-
return prompt
|
167 |
-
|
168 |
-
|
169 |
-
def update_loras(prompt, lora, lora_wt):
|
170 |
-
on, label, tag, md = get_lora_info(lora)
|
171 |
-
choices = get_all_lora_tupled_list()
|
172 |
-
return gr.update(value=prompt), gr.update(value=lora, choices=choices), gr.update(value=lora_wt),\
|
173 |
-
gr.update(value=tag, label=label, visible=on), gr.update(value=md, visible=on)
|
174 |
-
|
175 |
-
|
176 |
-
def search_civitai_lora(query, base_model, sort="Highest Rated", period="AllTime", tag=""):
|
177 |
-
global civitai_lora_last_results
|
178 |
-
items = search_lora_on_civitai(query, base_model, 100, sort, period, tag)
|
179 |
-
if not items: return gr.update(choices=[("", "")], value="", visible=False),\
|
180 |
-
gr.update(value="", visible=False), gr.update(visible=True), gr.update(visible=True)
|
181 |
-
civitai_lora_last_results = {}
|
182 |
-
choices = []
|
183 |
-
for item in items:
|
184 |
-
base_model_name = "Pony🐴" if item['base_model'] == "Pony" else item['base_model']
|
185 |
-
name = f"{item['name']} (for {base_model_name} / By: {item['creator']} / Tags: {', '.join(item['tags'])})"
|
186 |
-
value = item['dl_url']
|
187 |
-
choices.append((name, value))
|
188 |
-
civitai_lora_last_results[value] = item
|
189 |
-
if not choices: return gr.update(choices=[("", "")], value="", visible=False),\
|
190 |
-
gr.update(value="", visible=False), gr.update(visible=True), gr.update(visible=True)
|
191 |
-
result = civitai_lora_last_results.get(choices[0][1], "None")
|
192 |
-
md = result['md'] if result else ""
|
193 |
-
return gr.update(choices=choices, value=choices[0][1], visible=True), gr.update(value=md, visible=True),\
|
194 |
-
gr.update(visible=True), gr.update(visible=True)
|
195 |
-
|
196 |
-
|
197 |
-
def select_civitai_lora(search_result):
|
198 |
-
if not "http" in search_result: return gr.update(value=""), gr.update(value="None", visible=True)
|
199 |
-
result = civitai_lora_last_results.get(search_result, "None")
|
200 |
-
md = result['md'] if result else ""
|
201 |
-
return gr.update(value=search_result), gr.update(value=md, visible=True)
|
202 |
-
|
203 |
-
|
204 |
-
def search_civitai_lora_json(query, base_model):
|
205 |
-
results = {}
|
206 |
-
items = search_lora_on_civitai(query, base_model)
|
207 |
-
if not items: return gr.update(value=results)
|
208 |
-
for item in items:
|
209 |
-
results[item['dl_url']] = item
|
210 |
-
return gr.update(value=results)
|
211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|