Efreak commited on
Commit
865ef6f
1 Parent(s): ab6c524

i hope this works

Browse files
Files changed (1) hide show
  1. app.py +33 -24
app.py CHANGED
@@ -9,9 +9,9 @@ import random
9
  import time
10
 
11
  api=HfApi()
12
- REPO_TYPES = ["model", "dataset", "space"]
13
 
14
- def duplicate(source_url, dst_repo, token, new_name, dst_repo_path, repo_type):
15
  try:
16
  _ = whoami(token)
17
  # ^ this will throw if token is invalid
@@ -29,32 +29,40 @@ def duplicate(source_url, dst_repo, token, new_name, dst_repo_path, repo_type):
29
  dir="/home/user/apps/downloads/"+str(int(time.time()))+str(random.getrandbits(8))+"/"
30
  subprocess.check_call([r"mkdir","-p",dir])
31
  subprocess.check_call([r"aria2c","-x16","--split=16",source_url,"--dir="+dir])
32
- files=os.listdir(dir)
33
 
34
- if new_name:
35
- dst_repo_path=dst_repo_path+new_name
36
- else:
37
- dst_repo_path=dst_repo_path+files[0]
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- api.upload_file(
40
- path_or_fileobj=dir+files[0],
41
- path_in_repo=dst_repo_path,
42
  repo_id=dst_repo,
43
  repo_type=repo_type
44
  )
45
 
46
  # now clean up
 
47
  os.remove(dir+files[0])
 
 
48
  os.rmdir(dir)
49
- match repo_type:
50
- case "space":
51
- repo_url=f"https://hf.co/spaces/{dst_repo}"
52
- case "dataset":
53
- repo_url=f"https://hf.co/datasets/{dst_repo}"
54
- case "model":
55
- repo_url=f"https://hf.co/{dst_repo}"
56
  return (
57
- f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
58
  "sp.jpg",
59
  )
60
 
@@ -80,19 +88,20 @@ def duplicate(source_url, dst_repo, token, new_name, dst_repo_path, repo_type):
80
  interface = gr.Interface(
81
  fn=duplicate,
82
  inputs=[
83
- gr.Textbox(placeholder="Source URL (e.g. civitai.com/api/download/models/4324322534)"),
84
- gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
 
 
85
  gr.Textbox(placeholder="Write access token", type="password"),
86
- gr.Textbox(placeholder="Post-download name of your file, if you want it changed (e.g. stupidmodel.safetensors)"),
87
  gr.Textbox(placeholder="Destination for your file within your repo. Don't include the filename, end path with a / (e.g. /models/Stable-diffusion/)"),
88
- gr.Dropdown(choices=REPO_TYPES, value="model"),
89
  ],
90
  outputs=[
91
  gr.Markdown(label="output"),
92
  gr.Image(show_label=False),
93
  ],
94
- title="Download a file to your repo!",
95
- description="Download a file to your Hugging Face repository! You need to specify a write token obtained in https://hf.co/settings/tokens. This Space is a an experimental demo.",
96
  article="<p>Find your write token at <a href='https://huggingface.co/settings/tokens' target='_blank'>token settings</a></p>",
97
  allow_flagging="never",
98
  live=False, # since i keep wondering, this prevents it from running again automatically when an input changes
 
9
  import time
10
 
11
  api=HfApi()
12
+ FILE_TYPES = ["Embedding", "Model", "Lora", "Lycoris"]
13
 
14
+ def duplicate(source_url, preview_url, model_id, dst_repo, token, dst_repo_path, repo_type):
15
  try:
16
  _ = whoami(token)
17
  # ^ this will throw if token is invalid
 
29
  dir="/home/user/apps/downloads/"+str(int(time.time()))+str(random.getrandbits(8))+"/"
30
  subprocess.check_call([r"mkdir","-p",dir])
31
  subprocess.check_call([r"aria2c","-x16","--split=16",source_url,"--dir="+dir])
 
32
 
33
+ filename=os.listdir(dir)[0].rsplit('.',1)[0]
34
+ subprocess.check_call([r"curl","https://civitai.com/api/v1/model-versions/"+model_id,source_url,"-O",dir+filename+".civitai.info"])
35
+ subprocess.check_call([r"curl",preview_url,"-O",filename+".preview.png"])
36
+
37
+ match repo_type:
38
+ case "Embedding":
39
+ repopath="/embeddings"
40
+ case "Model":
41
+ repopath="/models/Stable-diffusion"
42
+ case "Lora":
43
+ repopath="/models/Lora/Lora"
44
+ case "Lycoris":
45
+ repopath="/models/Lora/Lycoris"
46
+
47
+ if nsfw==True:
48
+ repopath=repopath+"/nsfw"
49
 
50
+ api.upload_folder(
51
+ path_or_fileobj=dir,
52
+ path_in_repo=repopath,
53
  repo_id=dst_repo,
54
  repo_type=repo_type
55
  )
56
 
57
  # now clean up
58
+ files=os.listdir(dir)
59
  os.remove(dir+files[0])
60
+ os.remove(dir+files[1])
61
+ os.remove(dir+files[2])
62
  os.rmdir(dir)
63
+
 
 
 
 
 
 
64
  return (
65
+ f'Find your commit at the top of <a href=\'https://hf.co/spaces/{dst_repo}/commits/main{repopath}\' target="_blank" style="text-decoration:underline">your commits</a>',
66
  "sp.jpg",
67
  )
68
 
 
88
  interface = gr.Interface(
89
  fn=duplicate,
90
  inputs=[
91
+ gr.Textbox(placeholder="Source URL for model (e.g. civitai.com/api/download/models/4324322534)"),
92
+ gr.Textbox(placeholder="Preview URL for model (e.g. https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/df8e24d7-4094-42cf-0d8f-a50afd28f800/width=450,optimized=true/00143-3805918203.jpeg)"),
93
+ gr.Textbox(placeholder="model ID (e.g. 4324322534)"),
94
+ gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)",value="spaces/mirroring/civitai_mirror/"),
95
  gr.Textbox(placeholder="Write access token", type="password"),
 
96
  gr.Textbox(placeholder="Destination for your file within your repo. Don't include the filename, end path with a / (e.g. /models/Stable-diffusion/)"),
97
+ gr.Dropdown(choices=FILE_TYPES, value="Model"),
98
  ],
99
  outputs=[
100
  gr.Markdown(label="output"),
101
  gr.Image(show_label=False),
102
  ],
103
+ title="Download a civitai model to your repo!",
104
+ description="Download a model from Civitai to your repo on HF. Hopefully will soon parse the modelid from the download url, then get the preview automatically from the api response (lets be realistic, this will never happen); in the meantime you need to provide the model id number from the download url , and the location of the preview file. This Space is a an experimental demo.",
105
  article="<p>Find your write token at <a href='https://huggingface.co/settings/tokens' target='_blank'>token settings</a></p>",
106
  allow_flagging="never",
107
  live=False, # since i keep wondering, this prevents it from running again automatically when an input changes