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
|
import os
from os.path import join
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from app.settings import app_cfg
def api_url(path):
return "https://lens.neural.garden/api/{}/".format(path)
def fetch_cortex_folder(opt_folder_id):
rows = fetch_json(api_url('file'), folder_id=opt_folder_id)
fp_out_dir = join(app_cfg.DIR_INPUTS, "cortex", str(opt_folder_id))
os.makedirs(fp_out_dir, exist_ok=True)
for row in rows:
if row['generated'] == 0 and row['processed'] != 1:
fn, ext = os.path.splitext(row['name'])
fp_out_image = join(fp_out_dir, "{}{}".format(row['id'], ext))
if not os.path.exists(fp_out_image):
fetch_file(row['url'], fp_out_image)
def fetch_json(url, **kwargs):
resp = requests.get(url, params=kwargs, verify=False, timeout=10)
return None if resp.status_code != 200 else resp.json()
def fetch_file(url, fn, **kwargs):
print("Fetch {} => {}".format(url, fn))
try:
resp = requests.get(url, params=kwargs, verify=False, timeout=10)
if resp.status_code != 200:
return None
except:
return None
size = 0
with open(fn, 'wb') as f:
for chunk in resp.iter_content(chunk_size=1024):
if chunk:
size += len(chunk)
f.write(chunk)
return size
def upload_fp_to_cortex(opt_folder_id, fp):
files = {
'file': fp
}
data = {
'folder_id': opt_folder_id,
'generated': 'true',
'module': 'biggan',
'activity': 'invert',
'datatype': 'image',
}
url = os.path.join(api_url('folder'), opt_folder_id, 'upload/')
print(url)
r = requests.post(url, files=files, data=data)
print(r.json())
def upload_bytes_to_cortex(opt_folder_id, fn, fp, mimetype):
upload_fp_to_cortex(opt_folder_id, (fn, fp.getvalue(), mimetype,))
def upload_file_to_cortex(opt_folder_id, fn):
with open(fn, 'rb') as fp:
upload_fp_to_cortex(opt_folder_id, fp)
|