Spaces:
Running
Running
Commit
•
f212c91
1
Parent(s):
d7d7222
Update app.py
Browse files
app.py
CHANGED
@@ -67,22 +67,25 @@ def download_file(url, filename, folder="."):
|
|
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 |
-
|
|
|
|
|
|
|
77 |
return info, downloaded_files
|
78 |
else:
|
79 |
-
|
80 |
else:
|
81 |
-
|
82 |
else:
|
83 |
-
|
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:'
|
@@ -113,14 +116,14 @@ widget:
|
|
113 |
file.write(readme_content)
|
114 |
|
115 |
|
116 |
-
def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], url,
|
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,
|
124 |
try:
|
125 |
api = HfApi(token=hf_token)
|
126 |
username = api.whoami()["name"]
|
@@ -136,6 +139,9 @@ def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], url, is_author, progr
|
|
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)
|
@@ -174,7 +180,7 @@ with gr.Blocks(css=css) as demo:
|
|
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:
|
@@ -183,11 +189,14 @@ with gr.Blocks(css=css) as demo:
|
|
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 |
-
|
|
|
191 |
|
192 |
demo.queue()
|
193 |
demo.launch()
|
|
|
67 |
except requests.exceptions.RequestException as e:
|
68 |
print(f"Error downloading file: {e}")
|
69 |
|
70 |
+
def process_url(url, download_files=True, 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 |
+
if(download_files):
|
77 |
+
downloaded_files = download_files(info, folder)
|
78 |
+
else
|
79 |
+
downloaded_files = []
|
80 |
return info, downloaded_files
|
81 |
else:
|
82 |
+
raise gr.Error("Only SDXL LoRAs are supported for now")
|
83 |
else:
|
84 |
+
raise gr.Error("This model has content tagged as unsafe by CivitAI")
|
85 |
else:
|
86 |
+
raise gr.Error("Something went wrong in fetching CivitAI API")
|
87 |
|
88 |
+
def create_readme(info, downloaded_files, is_author=True, folder="."):
|
89 |
readme_content = ""
|
90 |
original_url = f"https://civitai.com/models/{info['id']}"
|
91 |
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:'
|
|
|
116 |
file.write(readme_content)
|
117 |
|
118 |
|
119 |
+
def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], url, progress=gr.Progress(track_tqdm=True)):
|
120 |
if not profile.name:
|
121 |
return gr.Error("Are you sure you are logged in?")
|
122 |
|
123 |
folder = str(uuid.uuid4())
|
124 |
os.makedirs(folder, exist_ok=False)
|
125 |
info, downloaded_files = process_url(url, folder)
|
126 |
+
create_readme(info, downloaded_files, folder)
|
127 |
try:
|
128 |
api = HfApi(token=hf_token)
|
129 |
username = api.whoami()["name"]
|
|
|
139 |
raise gr.Error("something went wrong")
|
140 |
return "Model uploaded!"
|
141 |
|
142 |
+
def check_civit_link(url):
|
143 |
+
info, _ = process_url(url, download_files=False)
|
144 |
+
return info["creator"]
|
145 |
def swap_fill(profile: Optional[gr.OAuthProfile]):
|
146 |
if profile is None:
|
147 |
return gr.update(visible=True), gr.update(visible=False)
|
|
|
180 |
label="CivitAI model URL",
|
181 |
info="URL of the CivitAI model, make sure it is a SDXL LoRA",
|
182 |
)
|
183 |
+
#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)
|
184 |
submit_button_civit = gr.Button("Upload model to Hugging Face and submit")
|
185 |
output = gr.Textbox(label="Output progress")
|
186 |
with gr.Column(visible=False) as enabled_area:
|
|
|
189 |
label="CivitAI model URL",
|
190 |
info="URL of the CivitAI model, make sure it is a SDXL LoRA",
|
191 |
)
|
192 |
+
#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)
|
193 |
+
instructions = gr.HTML("")
|
194 |
submit_button_civit = gr.Button("Upload model to Hugging Face")
|
195 |
output = gr.Textbox(label="Output progress")
|
196 |
+
|
197 |
demo.load(fn=swap_fill, outputs=[disabled_area, enabled_area])
|
198 |
+
submit_source_civit.change(fn=check_civit_link, inputs=[submit_source_civit], output=[instructions])
|
199 |
+
submit_button_civit.click(fn=upload_civit_to_hf, inputs=[submit_source_civit], outputs=[output])
|
200 |
|
201 |
demo.queue()
|
202 |
demo.launch()
|