Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os.path
|
2 |
+
import sys
|
3 |
+
import time
|
4 |
+
import urllib.request
|
5 |
+
from pathlib import Path
|
6 |
+
from urllib.parse import urlparse, parse_qs, unquote
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
CHUNK_SIZE = 1638400
|
10 |
+
TOKEN_FILE = Path.home() / '.civitai' / 'config'
|
11 |
+
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
|
12 |
+
|
13 |
+
|
14 |
+
def get_token():
|
15 |
+
try:
|
16 |
+
with open(TOKEN_FILE, 'r') as file:
|
17 |
+
token = file.read()
|
18 |
+
return token
|
19 |
+
except Exception as e:
|
20 |
+
return None
|
21 |
+
|
22 |
+
|
23 |
+
def store_token(token: str):
|
24 |
+
TOKEN_FILE.parent.mkdir(parents=True, exist_ok=True)
|
25 |
+
|
26 |
+
with open(TOKEN_FILE, 'w') as file:
|
27 |
+
file.write(token)
|
28 |
+
|
29 |
+
|
30 |
+
def prompt_for_civitai_token():
|
31 |
+
token = input('Please enter your CivitAI API token: ')
|
32 |
+
store_token(token)
|
33 |
+
return token
|
34 |
+
|
35 |
+
|
36 |
+
def download_file(url: str, output_path: str, token: str):
|
37 |
+
headers = {
|
38 |
+
'Authorization': f'Bearer {token}',
|
39 |
+
'User-Agent': USER_AGENT,
|
40 |
+
}
|
41 |
+
|
42 |
+
# Disable automatic redirect handling
|
43 |
+
class NoRedirection(urllib.request.HTTPErrorProcessor):
|
44 |
+
def http_response(self, request, response):
|
45 |
+
return response
|
46 |
+
https_response = http_response
|
47 |
+
|
48 |
+
request = urllib.request.Request(url, headers=headers)
|
49 |
+
opener = urllib.request.build_opener(NoRedirection)
|
50 |
+
response = opener.open(request)
|
51 |
+
|
52 |
+
if response.status in [301, 302, 303, 307, 308]:
|
53 |
+
redirect_url = response.getheader('Location')
|
54 |
+
|
55 |
+
# Extract filename from the redirect URL
|
56 |
+
parsed_url = urlparse(redirect_url)
|
57 |
+
query_params = parse_qs(parsed_url.query)
|
58 |
+
content_disposition = query_params.get('response-content-disposition', [None])[0]
|
59 |
+
|
60 |
+
if content_disposition:
|
61 |
+
filename = unquote(content_disposition.split('filename=')[1].strip('"'))
|
62 |
+
else:
|
63 |
+
raise Exception('Unable to determine filename')
|
64 |
+
|
65 |
+
response = urllib.request.urlopen(redirect_url)
|
66 |
+
elif response.status == 404:
|
67 |
+
raise Exception('File not found')
|
68 |
+
else:
|
69 |
+
raise Exception('No redirect found, something went wrong')
|
70 |
+
|
71 |
+
total_size = response.getheader('Content-Length')
|
72 |
+
|
73 |
+
if total_size is not None:
|
74 |
+
total_size = int(total_size)
|
75 |
+
|
76 |
+
output_file = os.path.join(output_path, filename)
|
77 |
+
|
78 |
+
with open(output_file, 'wb') as f:
|
79 |
+
downloaded = 0
|
80 |
+
start_time = time.time()
|
81 |
+
|
82 |
+
while True:
|
83 |
+
chunk_start_time = time.time()
|
84 |
+
buffer = response.read(CHUNK_SIZE)
|
85 |
+
chunk_end_time = time.time()
|
86 |
+
|
87 |
+
if not buffer:
|
88 |
+
break
|
89 |
+
|
90 |
+
downloaded += len(buffer)
|
91 |
+
f.write(buffer)
|
92 |
+
chunk_time = chunk_end_time - chunk_start_time
|
93 |
+
|
94 |
+
if chunk_time > 0:
|
95 |
+
speed = len(buffer) / chunk_time / (1024 ** 2) # Speed in MB/s
|
96 |
+
|
97 |
+
if total_size is not None:
|
98 |
+
progress = downloaded / total_size
|
99 |
+
sys.stdout.write(f'\rDownloading: {filename} [{progress*100:.2f}%] - {speed:.2f} MB/s')
|
100 |
+
sys.stdout.flush()
|
101 |
+
|
102 |
+
end_time = time.time()
|
103 |
+
time_taken = end_time - start_time
|
104 |
+
hours, remainder = divmod(time_taken, 3600)
|
105 |
+
minutes, seconds = divmod(remainder, 60)
|
106 |
+
|
107 |
+
if hours > 0:
|
108 |
+
time_str = f'{int(hours)}h {int(minutes)}m {int(seconds)}s'
|
109 |
+
elif minutes > 0:
|
110 |
+
time_str = f'{int(minutes)}m {int(seconds)}s'
|
111 |
+
else:
|
112 |
+
time_str = f'{int(seconds)}s'
|
113 |
+
|
114 |
+
sys.stdout.write('\n')
|
115 |
+
print(f'Download completed. File saved as: {filename}')
|
116 |
+
print(f'Downloaded in {time_str}')
|
117 |
+
|
118 |
+
|
119 |
+
def download_civitai_file(url, output_path, token):
|
120 |
+
try:
|
121 |
+
download_file(url, output_path, token)
|
122 |
+
return f'Download completed. File saved in: {output_path}'
|
123 |
+
except Exception as e:
|
124 |
+
return f'ERROR: {e}'
|
125 |
+
|
126 |
+
|
127 |
+
def main(url, output_path, token=None):
|
128 |
+
if not token:
|
129 |
+
token = prompt_for_civitai_token()
|
130 |
+
|
131 |
+
result = download_civitai_file(url, output_path, token)
|
132 |
+
return result
|
133 |
+
|
134 |
+
|
135 |
+
with gr.Blocks() as demo:
|
136 |
+
gr.Markdown("## CivitAI Downloader")
|
137 |
+
url = gr.Textbox(label="CivitAI Download URL")
|
138 |
+
output_path = gr.Textbox(label="Output Path")
|
139 |
+
token = gr.Textbox(label="API Token (Optional)")
|
140 |
+
download_button = gr.Button("Download")
|
141 |
+
output = gr.File(label="Output")
|
142 |
+
|
143 |
+
download_button.click(fn=main, inputs=[url, output_path, token], outputs=output)
|
144 |
+
|
145 |
+
demo.launch()
|