forked from Wikidepia/crawlingathome-worker
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gpu.py
201 lines (165 loc) · 6.46 KB
/
gpu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import multiprocessing as mp
import os
import shutil
import time
import traceback
import warnings
from glob import glob
from pathlib import Path
import pandas
import ujson
from PIL import ImageFile
import crawlingathome_client as cah
ImageFile.LOAD_TRUNCATED_IMAGES = True # https://stackoverflow.com/a/47958486
warnings.filterwarnings('ignore')
def upload(source: str, client_type: str):
client_type = client_type.upper()
target = 'gpujobs' if client_type == 'CPU' else 'CAH'
options = '-rsh' if client_type == 'CPU' else '-zh'
return os.system(f'rsync {options} {source} [email protected]::{target}')
def download(url, name, debug, isnotebook):
while True:
try:
client = cah.init(
url=url, nickname=name, type='gpu'
)
start_dl = time.time()
client.newJob()
client.downloadShard()
end_dl = time.time()
uid = client.shard.split('rsync', 1)[-1].strip()
if len(glob(f'{uid}/*.csv')) == 0:
print(f'[crawling@home] Marking job {uid} as invalid')
client.invalidURL()
for file in glob(f'{uid}/*_parsed.csv') + glob(f'{uid}/*_unfiltered.csv'):
shutil.move(file, 'stats/')
print(f'[crawling@home] Downloaded job {uid} in {end_dl-start_dl}')
with open(f'{uid}/client.json', 'w') as f:
ujson.dump(client.dump(), f)
if isnotebook:
time.sleep(25)
except (cah.errors.InvalidURLError, cah.errors.ZeroJobError):
try:
if client.isAlive():
client.bye()
except:
pass
time.sleep(5)
except KeyboardInterrupt:
if client.isAlive():
client.bye()
except Exception as ex:
print(f'[crawling@home] DLERROR: {ex}')
if debug:
traceback.print_exc()
if client.isAlive():
try:
client.log('Error, restarting job')
except:
print("[crawling@home] Couldn't log to client:")
pool = None
def downloader(name, url, debug, isnotebook, workers):
pool = mp.Pool(workers)
pool.starmap_async(
download, [(url, name, debug, isnotebook) for _ in range(1)])
def old_main(name, url, debug, isnotebook, workers, isdocker):
if not Path('stats').exists():
os.mkdir('stats')
print('[crawling@home] loading clip')
from clip_filter import run_inference
print('\n[crawling@home] clip loaded\n')
def _worker(client_dict):
while True:
try:
client = cah.load(**client_dict)
output_folder = f"./{client.shard.split('rsync', 1)[-1].strip()}/"
start = time.time()
first_sample_id = int(client.start_id)
last_sample_id = int(client.end_id)
shard_of_chunk = client.shard_piece
out_fname = \
f'FIRST_SAMPLE_ID_IN_SHARD_{first_sample_id}_LAST_SAMPLE_ID_IN_SHARD_{last_sample_id}_{shard_of_chunk}'
print(
f'[crawling@home] shard identification {out_fname}'
) # in case test fails, we need to remove bad data
dlparse_df = pandas.read_csv(
f'{output_folder}{out_fname}.csv', sep='|')
dlparse_df['PATH'] = dlparse_df.apply(
lambda x: output_folder + x['PATH'].strip('save/'), axis=1)
client.log('Dropping NSFW keywords')
filtered_df_len = run_inference(
dlparse_df, output_folder, out_fname)
client.log('Uploading Results')
upload(f'{output_folder}/*{out_fname}*', client.type)
client.completeJob(filtered_df_len)
client.bye()
end = time.time()
print(
f'[crawling@home] job completed in {round(end - start)} seconds')
print(
f'[crawling@home] job efficiency {filtered_df_len / (end - start)} pairs/sec')
shutil.rmtree(output_folder)
break
except Exception as ex:
print(f'[crawling@home] ERROR: {ex}')
if debug:
traceback.print_exc()
if client.isAlive():
try:
client.log('Error, restarting job')
except:
print("[crawling@home] Couldn't log to client")
try:
if client.isAlive():
client.bye()
except:
pass
pass
print('start download')
downloader(name, url, debug, isnotebook, workers)
print('download started')
while True:
try:
for client_dump in glob('./*/client.json'):
with open(client_dump, 'r') as f:
print(client_dump)
try:
client_dict = ujson.load(f)
except ValueError:
continue
print(client_dict)
_worker(client_dict)
continue
except KeyboardInterrupt:
print('[crawling@home] stopping worker')
if hasattr(pool, 'terminate'):
print('terminating pool')
pool.terminate()
print('joining pool')
pool.join()
print('[crawling@home] stopped worker, cleaning workspace')
break
except Exception as ex:
try:
print(f'[crawling@home] ERROR: {ex}')
if debug:
traceback.print_exc()
except:
break
time.sleep(10) # wait for in-progress rsync job to complete
for client in glob('./*/client.json'):
with open(client) as f:
client = cah.load(**ujson.load(f))
client.bye()
for folder in glob('./*'):
if 'crawlingathome_client' in folder or 'venv' in folder or 'stats' in folder:
continue
path = Path(folder)
if path.is_dir():
shutil.rmtree(folder)
print('[crawling@home] cleaned workspace')
def main(*_):
import warnings
warnings.filterwarnings('default')
warnings.warn(
'This worker is deprecated, use this repo: https://github.com/rvencu/crawlingathome-gpu-hcloud', DeprecationWarning)