Spaces:
Running
Running
Upload 5 files
Browse files- app.py +5 -3
- civitai_to_hf.py +22 -9
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, CIVITAI_TYPE, CIVITAI_BASEMODEL, CIVITAI_SORT, CIVITAI_PERIOD
|
| 3 |
|
| 4 |
css = """
|
| 5 |
.title { text-align: center; !important; }
|
|
@@ -23,7 +23,8 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
|
|
| 23 |
search_civitai_json = gr.JSON(value={}, visible=False)
|
| 24 |
search_civitai_desc = gr.Markdown(value="", visible=False)
|
| 25 |
search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value="", allow_custom_value=True, visible=False)
|
| 26 |
-
|
|
|
|
| 27 |
civitai_key = gr.Textbox(label="Your Civitai Key", value="", max_lines=1)
|
| 28 |
with gr.Row():
|
| 29 |
hf_token = gr.Textbox(label="Your HF write token", placeholder="hf_...", value="", max_lines=1)
|
|
@@ -51,7 +52,8 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
|
|
| 51 |
queue=True,
|
| 52 |
show_api=False,
|
| 53 |
)
|
| 54 |
-
search_civitai_result.change(select_civitai_item, [search_civitai_result], [
|
|
|
|
| 55 |
|
| 56 |
demo.queue()
|
| 57 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from civitai_to_hf import search_civitai, download_civitai, select_civitai_item, add_civitai_item, CIVITAI_TYPE, CIVITAI_BASEMODEL, CIVITAI_SORT, CIVITAI_PERIOD
|
| 3 |
|
| 4 |
css = """
|
| 5 |
.title { text-align: center; !important; }
|
|
|
|
| 23 |
search_civitai_json = gr.JSON(value={}, visible=False)
|
| 24 |
search_civitai_desc = gr.Markdown(value="", visible=False)
|
| 25 |
search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value="", allow_custom_value=True, visible=False)
|
| 26 |
+
search_civitai_add = gr.Button("Add to download URLs")
|
| 27 |
+
dl_url = gr.Textbox(label="Download URL(s)", placeholder="https://civitai.com/api/download/models/28907\n...", value="", lines=2, max_lines=255)
|
| 28 |
civitai_key = gr.Textbox(label="Your Civitai Key", value="", max_lines=1)
|
| 29 |
with gr.Row():
|
| 30 |
hf_token = gr.Textbox(label="Your HF write token", placeholder="hf_...", value="", max_lines=1)
|
|
|
|
| 52 |
queue=True,
|
| 53 |
show_api=False,
|
| 54 |
)
|
| 55 |
+
search_civitai_result.change(select_civitai_item, [search_civitai_result], [search_civitai_desc], queue=False, show_api=False)
|
| 56 |
+
search_civitai_add.click(add_civitai_item, [search_civitai_result, dl_url], [dl_url], queue=False, show_api=False)
|
| 57 |
|
| 58 |
demo.queue()
|
| 59 |
demo.launch()
|
civitai_to_hf.py
CHANGED
|
@@ -9,13 +9,17 @@ from urllib3.util import Retry
|
|
| 9 |
from utils import get_token, set_token, is_repo_exists, get_user_agent, get_download_file
|
| 10 |
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def upload_safetensors_to_repo(filename, repo_id, repo_type, is_private, progress=gr.Progress(track_tqdm=True)):
|
| 13 |
output_filename = Path(filename).name
|
| 14 |
hf_token = get_token()
|
| 15 |
api = HfApi(token=hf_token)
|
| 16 |
try:
|
| 17 |
if not is_repo_exists(repo_id, repo_type): api.create_repo(repo_id=repo_id, repo_type=repo_type, token=hf_token, private=is_private)
|
| 18 |
-
progress(0, desc="Start uploading...")
|
| 19 |
api.upload_file(path_or_fileobj=filename, path_in_repo=output_filename, repo_type=repo_type, revision="main", token=hf_token, repo_id=repo_id)
|
| 20 |
progress(1, desc="Uploaded.")
|
| 21 |
url = hf_hub_url(repo_id=repo_id, repo_type=repo_type, filename=output_filename)
|
|
@@ -28,7 +32,7 @@ def upload_safetensors_to_repo(filename, repo_id, repo_type, is_private, progres
|
|
| 28 |
|
| 29 |
def download_file(dl_url, civitai_key, progress=gr.Progress(track_tqdm=True)):
|
| 30 |
download_dir = "."
|
| 31 |
-
progress(0, desc="Start downloading...")
|
| 32 |
output_filename = get_download_file(download_dir, dl_url, civitai_key)
|
| 33 |
return output_filename
|
| 34 |
|
|
@@ -38,13 +42,16 @@ def download_civitai(dl_url, civitai_key, hf_token, urls,
|
|
| 38 |
if hf_token: set_token(hf_token)
|
| 39 |
else: set_token(os.environ.get("HF_TOKEN"))
|
| 40 |
if not civitai_key: civitai_key = os.environ.get("CIVITAI_API_KEY")
|
| 41 |
-
if not
|
| 42 |
-
|
| 43 |
if not urls: urls = []
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
progress(1, desc="Processing...")
|
| 46 |
-
if url: urls.append(url)
|
| 47 |
-
Path(file).unlink()
|
| 48 |
md = ""
|
| 49 |
for u in urls:
|
| 50 |
md += f"[Uploaded {str(u)}]({str(u)})<br>"
|
|
@@ -123,7 +130,13 @@ def search_civitai(query, types, base_model=[], sort=CIVITAI_SORT[0], period=CIV
|
|
| 123 |
|
| 124 |
|
| 125 |
def select_civitai_item(search_result):
|
| 126 |
-
if not "http" in search_result: return gr.update(value="
|
| 127 |
result = civitai_last_results.get(search_result, "None")
|
| 128 |
md = result['md'] if result else ""
|
| 129 |
-
return gr.update(value=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
from utils import get_token, set_token, is_repo_exists, get_user_agent, get_download_file
|
| 10 |
|
| 11 |
|
| 12 |
+
def parse_urls(s):
|
| 13 |
+
return [x.strip() for x in s.split() if s and "http" in s]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
def upload_safetensors_to_repo(filename, repo_id, repo_type, is_private, progress=gr.Progress(track_tqdm=True)):
|
| 17 |
output_filename = Path(filename).name
|
| 18 |
hf_token = get_token()
|
| 19 |
api = HfApi(token=hf_token)
|
| 20 |
try:
|
| 21 |
if not is_repo_exists(repo_id, repo_type): api.create_repo(repo_id=repo_id, repo_type=repo_type, token=hf_token, private=is_private)
|
| 22 |
+
progress(0, desc=f"Start uploading... {filename} to {repo_id}")
|
| 23 |
api.upload_file(path_or_fileobj=filename, path_in_repo=output_filename, repo_type=repo_type, revision="main", token=hf_token, repo_id=repo_id)
|
| 24 |
progress(1, desc="Uploaded.")
|
| 25 |
url = hf_hub_url(repo_id=repo_id, repo_type=repo_type, filename=output_filename)
|
|
|
|
| 32 |
|
| 33 |
def download_file(dl_url, civitai_key, progress=gr.Progress(track_tqdm=True)):
|
| 34 |
download_dir = "."
|
| 35 |
+
progress(0, desc=f"Start downloading... {dl_url}")
|
| 36 |
output_filename = get_download_file(download_dir, dl_url, civitai_key)
|
| 37 |
return output_filename
|
| 38 |
|
|
|
|
| 42 |
if hf_token: set_token(hf_token)
|
| 43 |
else: set_token(os.environ.get("HF_TOKEN"))
|
| 44 |
if not civitai_key: civitai_key = os.environ.get("CIVITAI_API_KEY")
|
| 45 |
+
if not get_token() or not civitai_key: raise gr.Error("HF write token and Civitai API key is required.")
|
| 46 |
+
dl_urls = parse_urls(dl_url)
|
| 47 |
if not urls: urls = []
|
| 48 |
+
for u in dl_urls:
|
| 49 |
+
file = download_file(u, civitai_key)
|
| 50 |
+
if not Path(file).exists() or not Path(file).is_file(): continue
|
| 51 |
+
url = upload_safetensors_to_repo(file, newrepo_id, repo_type, is_private)
|
| 52 |
+
if url: urls.append(url)
|
| 53 |
+
Path(file).unlink()
|
| 54 |
progress(1, desc="Processing...")
|
|
|
|
|
|
|
| 55 |
md = ""
|
| 56 |
for u in urls:
|
| 57 |
md += f"[Uploaded {str(u)}]({str(u)})<br>"
|
|
|
|
| 130 |
|
| 131 |
|
| 132 |
def select_civitai_item(search_result):
|
| 133 |
+
if not "http" in search_result: return gr.update(value="None", visible=True)
|
| 134 |
result = civitai_last_results.get(search_result, "None")
|
| 135 |
md = result['md'] if result else ""
|
| 136 |
+
return gr.update(value=md, visible=True)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def add_civitai_item(search_result, dl_url):
|
| 140 |
+
if not "http" in search_result: return gr.update(value=dl_url)
|
| 141 |
+
new_url = f"{dl_url}\n{search_result}" if dl_url else search_result
|
| 142 |
+
return gr.update(value=new_url)
|