multimodalart HF staff commited on
Commit
2aeb649
1 Parent(s): d20e495

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +193 -0
app.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import os
3
+ import gradio as gr
4
+ from huggingface_hub import HfApi
5
+ from slugify import slugify
6
+ import gradio as gr
7
+ import uuid
8
+ from typing import Optional
9
+
10
+ def get_json_data(url):
11
+ api_url = f"https://civitai.com/api/v1/models/{url.split('/')[4]}"
12
+ try:
13
+ response = requests.get(api_url)
14
+ response.raise_for_status()
15
+ return response.json()
16
+ except requests.exceptions.RequestException as e:
17
+ print(f"Error fetching JSON data: {e}")
18
+ return None
19
+
20
+ def check_nsfw(json_data):
21
+ if json_data["nsfw"]:
22
+ return False
23
+ for model_version in json_data["modelVersions"]:
24
+ for image in model_version["images"]:
25
+ if image["nsfw"] != "None":
26
+ return False
27
+ return True
28
+
29
+ def extract_info(json_data):
30
+ if json_data["type"] == "LORA":
31
+ for model_version in json_data["modelVersions"]:
32
+ if model_version["baseModel"] in ["SDXL 1.0", "SDXL 0.9"]:
33
+ for file in model_version["files"]:
34
+ if file["primary"]:
35
+ info = {
36
+ "urls_to_download": [
37
+ {"url": file["downloadUrl"], "filename": file["name"], "type": "weightName"},
38
+ {"url": model_version["images"][0]["url"], "filename": os.path.basename(model_version["images"][0]["url"]), "type": "imageName"}
39
+ ],
40
+ "id": model_version["id"],
41
+ "modelId": model_version["modelId"],
42
+ "name": json_data["name"],
43
+ "description": json_data["description"],
44
+ "trainedWords": model_version["trainedWords"],
45
+ "creator": json_data["creator"]["username"]
46
+ }
47
+ return info
48
+ return None
49
+
50
+ def download_files(info, folder="."):
51
+ downloaded_files = {
52
+ "imageName": [],
53
+ "weightName": []
54
+ }
55
+ for item in info["urls_to_download"]:
56
+ download_file(item["url"], item["filename"], folder)
57
+ downloaded_files[item["type"]].append(item["filename"])
58
+ return downloaded_files
59
+
60
+ def download_file(url, filename, folder="."):
61
+ try:
62
+ response = requests.get(url)
63
+ response.raise_for_status()
64
+ with open(f"{folder}/{filename}", 'wb') as f:
65
+ f.write(response.content)
66
+ print(f"{filename} downloaded.")
67
+ except requests.exceptions.RequestException as e:
68
+ print(f"Error downloading file: {e}")
69
+
70
+ def process_url(url, folder="."):
71
+ json_data = get_json_data(url)
72
+ if json_data:
73
+ if check_nsfw(json_data):
74
+ info = extract_info(json_data)
75
+ if info:
76
+ downloaded_files = download_files(info, folder)
77
+ return info, downloaded_files
78
+ else:
79
+ print("No model met the criteria.")
80
+ else:
81
+ print("NSFW content found.")
82
+ else:
83
+ print("Failed to get JSON data.")
84
+
85
+ def create_readme(info, downloaded_files, is_author, folder="."):
86
+ readme_content = ""
87
+ original_url = f"https://civitai.com/models/{info['id']}"
88
+ non_author_disclaimer = f'This model was originally uploaded on [CivitAI]({original_url}), by [{info["creator"]}](https://civitai.com/user/{info["creator"]}/models). The information below was provided by the author on CivitAI:'
89
+ content = f"""---
90
+ license: other
91
+ tags:
92
+ - text-to-image
93
+ - stable-diffusion
94
+ - lora
95
+ - diffusers
96
+ base_model: stabilityai/stable-diffusion-xl-base-1.0
97
+ instance_prompt: {info["trainedWords"][0]}
98
+ widget:
99
+ - text: {info["trainedWords"][0]}
100
+ ---
101
+
102
+ # {info["name"]}
103
+
104
+ {non_author_disclaimer if not is_author else ''}
105
+
106
+ ![Image]({downloaded_files["imageName"][0]})
107
+
108
+ {info["description"]}
109
+ """
110
+ readme_content += content + "\n"
111
+
112
+ with open(f"{folder}/README.md", "w") as file:
113
+ file.write(readme_content)
114
+
115
+
116
+ def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], url, is_author, progress=gr.Progress(track_tqdm=True)):
117
+ if not profile.name:
118
+ return gr.Error("Are you sure you are logged in?")
119
+
120
+ folder = str(uuid.uuid4())
121
+ os.makedirs(folder, exist_ok=False)
122
+ info, downloaded_files = process_url(url, folder)
123
+ create_readme(info, downloaded_files, False, folder)
124
+ try:
125
+ api = HfApi(token=hf_token)
126
+ username = api.whoami()["name"]
127
+ slug_name = slugify(info["name"])
128
+ repo_id = f"{username}/{slug_name}"
129
+ api.create_repo(repo_id=repo_id, private=True, exist_ok=True)
130
+ api.upload_folder(
131
+ folder_path=folder,
132
+ repo_id=repo_id,
133
+ repo_type="model"
134
+ )
135
+ except:
136
+ raise gr.Error("something went wrong")
137
+ return "Model uploaded!"
138
+
139
+ def swap_fill(profile: Optional[gr.OAuthProfile]):
140
+ if profile is None:
141
+ return gr.update(visible=True), gr.update(visible=False)
142
+ else:
143
+ return gr.update(visible=False), gr.update(visible=True)
144
+
145
+ css = '''
146
+ #login {
147
+ font-size: 0px;
148
+ width: 100% !important;
149
+ margin: 0 auto;
150
+ }
151
+ #login:after {
152
+ content: 'Authorize this app before uploading your model';
153
+ visibility: visible;
154
+ display: block;
155
+ font-size: var(--button-large-text-size);
156
+ }
157
+ #login:disabled{
158
+ font-size: var(--button-large-text-size);
159
+ }
160
+ #login:disabled:after{
161
+ content:''
162
+ }
163
+ #disabled_upload{
164
+ opacity: 0.5;
165
+ pointer-events:none;
166
+ }
167
+ '''
168
+
169
+ with gr.Blocks(css=css) as demo:
170
+ gr.LoginButton(elem_id="login")
171
+ with gr.Column(elem_id="disabled_upload") as disabled_area:
172
+ with gr.Row():
173
+ submit_source_civit = gr.Textbox(
174
+ label="CivitAI model URL",
175
+ info="URL of the CivitAI model, make sure it is a SDXL LoRA",
176
+ )
177
+ is_author = gr.Checkbox(label="Are you the model author?", info="If you are not the author, a disclaimer with information about the author and the CivitAI source will be added", value=False)
178
+ submit_button_civit = gr.Button("Upload model to Hugging Face and submit")
179
+ output = gr.Textbox(label="Output progress")
180
+ with gr.Column(visible=False) as enabled_area:
181
+ with gr.Row():
182
+ submit_source_civit = gr.Textbox(
183
+ label="CivitAI model URL",
184
+ info="URL of the CivitAI model, make sure it is a SDXL LoRA",
185
+ )
186
+ is_author = gr.Checkbox(label="Are you the model author?", info="If you are not the author, a disclaimer with information about the author and the CivitAI source will be added", value=False)
187
+ submit_button_civit = gr.Button("Upload model to Hugging Face")
188
+ output = gr.Textbox(label="Output progress")
189
+ demo.load(fn=swap_fill, outputs=[disabled_area, enabled_area])
190
+ submit_button_civit.click(fn=upload_civit_to_hf, inputs=[submit_source_civit, is_author], outputs=[output])
191
+
192
+ demo.queue()
193
+ demo.launch(share=True)