-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
62 lines (49 loc) · 1.53 KB
/
utils.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
import json
import logging
import mimetypes
import os
import requests
from urllib.parse import urlparse
from config import BASE_URL
def read_file_or_url(path):
if path.startswith("http://") or path.startswith("https://"):
response = requests.get(path)
return response.content
else:
return open(path, "rb")
# used by "/forecastingapicall2"
def call_api(token: str,
apiroute: str,
filename: str,
method: str = 'Ridge',
n_hidden_features: int = 5,
lags: int = 20,
type_pi: str = 'scp2-block-bootstrap',
replications: int = 50,
h: int = 10):
headers = {
'Authorization': 'Bearer ' + token,
}
params = {'base_model': str(method),
'n_hidden_features': str(n_hidden_features),
'lags': str(lags),
'type_pi': str(type_pi),
'replications': str(replications),
'h': str(h)}
# Prepare the file for uploading (file content, filename, and content_type)
files = {
'file': (filename, read_file_or_url(filename), 'text/csv')
}
try:
response = requests.post(BASE_URL + apiroute,
params=params, headers=headers,
files=files)
res = response.json()
mean = res["mean"]
lower = res["lower"]
upper = res["upper"]
sims = res["sims"]
return mean, lower, upper, sims
except Exception as e:
print(f"Error: {e}")
return None, None, None, None