John6666 commited on
Commit
07a3a3d
1 Parent(s): 1069789

Upload 6 files

Browse files
Files changed (4) hide show
  1. README.md +2 -2
  2. app.py +5 -4
  3. civitai_to_hf.py +12 -6
  4. requirements.txt +1 -1
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: CivitAI to HF Downloader Alpha
3
  emoji: 🤗
4
  colorFrom: yellow
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
1
  ---
2
+ title: CivitAI to HF🤗 Downloader & Uploader with Search
3
  emoji: 🤗
4
  colorFrom: yellow
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 5.0.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py CHANGED
@@ -2,15 +2,16 @@ 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; }
 
6
  """
7
 
8
  with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_cache=(60, 3600)) as demo:
9
  with gr.Column():
10
- gr.Markdown("# Civitai to HF Downloader Alpha", elem_classes="title")
11
  with gr.Accordion("Search Civitai", open=False):
12
  with gr.Row():
13
- search_civitai_type = gr.CheckboxGroup(label="Type", choices=CIVITAI_TYPE, value=[])
14
  search_civitai_basemodel = gr.CheckboxGroup(label="Base model", choices=CIVITAI_BASEMODEL, value=[])
15
  with gr.Row():
16
  search_civitai_sort = gr.Radio(label="Sort", choices=CIVITAI_SORT, value=CIVITAI_SORT[0])
@@ -35,7 +36,7 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
35
  is_private = gr.Checkbox(label="Create private repo", value=True)
36
  uploaded_urls = gr.CheckboxGroup(visible=False, choices=[], value=None) # hidden
37
  run_button = gr.Button(value="Download and Upload")
38
- urls_md = gr.Markdown()
39
  gr.DuplicateButton(value="Duplicate Space")
40
 
41
  gr.on(
 
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 { font-size: 3em; align-items: center; text-align: center; }
6
+ .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; }
7
  """
8
 
9
  with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_cache=(60, 3600)) as demo:
10
  with gr.Column():
11
+ gr.Markdown("# CivitAI to HF🤗 Downloader & Uploader", elem_classes="title")
12
  with gr.Accordion("Search Civitai", open=False):
13
  with gr.Row():
14
+ search_civitai_type = gr.CheckboxGroup(label="Type", choices=CIVITAI_TYPE, value=["Checkpoint", "LORA"])
15
  search_civitai_basemodel = gr.CheckboxGroup(label="Base model", choices=CIVITAI_BASEMODEL, value=[])
16
  with gr.Row():
17
  search_civitai_sort = gr.Radio(label="Sort", choices=CIVITAI_SORT, value=CIVITAI_SORT[0])
 
36
  is_private = gr.Checkbox(label="Create private repo", value=True)
37
  uploaded_urls = gr.CheckboxGroup(visible=False, choices=[], value=None) # hidden
38
  run_button = gr.Button(value="Download and Upload")
39
+ urls_md = gr.Markdown("<br><br>", elem_classes="result")
40
  gr.DuplicateButton(value="Duplicate Space")
41
 
42
  gr.on(
civitai_to_hf.py CHANGED
@@ -7,10 +7,16 @@ import requests
7
  from requests.adapters import HTTPAdapter
8
  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 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)):
@@ -40,8 +46,9 @@ def download_file(dl_url, civitai_key, progress=gr.Progress(track_tqdm=True)):
40
  def download_civitai(dl_url, civitai_key, hf_token, urls,
41
  newrepo_id, repo_type="model", is_private=True, progress=gr.Progress(track_tqdm=True)):
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 = []
@@ -52,9 +59,9 @@ def download_civitai(dl_url, civitai_key, hf_token, urls,
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>"
58
  gc.collect()
59
  return gr.update(value=urls, choices=urls), gr.update(value=md)
60
 
@@ -67,7 +74,6 @@ CIVITAI_PERIOD = ["AllTime", "Year", "Month", "Week", "Day"]
67
 
68
  def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [], limit: int = 100,
69
  sort: str = "Highest Rated", period: str = "AllTime", tag: str = ""):
70
-
71
  user_agent = get_user_agent()
72
  headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
73
  base_url = 'https://civitai.com/api/v1/models'
 
7
  from requests.adapters import HTTPAdapter
8
  from urllib3.util import Retry
9
  from utils import get_token, set_token, is_repo_exists, get_user_agent, get_download_file
10
+ import re
11
 
12
 
13
  def parse_urls(s):
14
+ url_pattern = "https?://[\\w/:%#\\$&\\?\\(\\)~\\.=\\+\\-]+"
15
+ try:
16
+ urls = re.findall(url_pattern, s)
17
+ return list(urls)
18
+ except Exception:
19
+ return []
20
 
21
 
22
  def upload_safetensors_to_repo(filename, repo_id, repo_type, is_private, progress=gr.Progress(track_tqdm=True)):
 
46
  def download_civitai(dl_url, civitai_key, hf_token, urls,
47
  newrepo_id, repo_type="model", is_private=True, progress=gr.Progress(track_tqdm=True)):
48
  if hf_token: set_token(hf_token)
49
+ else: set_token(os.environ.get("HF_TOKEN")) # default huggingface write token
50
+ if not civitai_key: civitai_key = os.environ.get("CIVITAI_API_KEY") # default Civitai API key
51
+ if not newrepo_id: newrepo_id = os.environ.get("HF_REPO") # default repo to upload
52
  if not get_token() or not civitai_key: raise gr.Error("HF write token and Civitai API key is required.")
53
  dl_urls = parse_urls(dl_url)
54
  if not urls: urls = []
 
59
  if url: urls.append(url)
60
  Path(file).unlink()
61
  progress(1, desc="Processing...")
62
+ md = f'### Your repo: [{newrepo_id}]({"https://huggingface.co/datasets/" if repo_type == "dataset" else "https://huggingface.co/"}{newrepo_id})\n'
63
  for u in urls:
64
+ md += f"- Uploaded [{str(u)}]({str(u)})\n"
65
  gc.collect()
66
  return gr.update(value=urls, choices=urls), gr.update(value=md)
67
 
 
74
 
75
  def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [], limit: int = 100,
76
  sort: str = "Highest Rated", period: str = "AllTime", tag: str = ""):
 
77
  user_agent = get_user_agent()
78
  headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
79
  base_url = 'https://civitai.com/api/v1/models'
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
  huggingface-hub
2
- gdown
 
1
  huggingface-hub
2
+ gdown