Spaces:
Runtime error
Runtime error
| import requests | |
| import json | |
| import pickle | |
| import os | |
| class fft(): | |
| def __init__(self, apikey) -> None: | |
| self.apikey = apikey | |
| def parse_res(self, res): | |
| if str(res.status_code)[0] != '2': | |
| raise ValueError('fft API Call Failed!\n', res.text) | |
| else: | |
| parsed_res = res.json() | |
| return parsed_res | |
| def forecast( | |
| self, | |
| ts_list, | |
| n_predict: int, | |
| num_harmonics=None): | |
| endpoint = 'https://idsc.com.sg/foretell/prediction/time-series/continuous/fourier-prediction' | |
| payloads = { | |
| 'time_series_data': ts_list, | |
| 'num_predict': n_predict} | |
| if num_harmonics is not None: | |
| payloads['num_harmonics'] = num_harmonics | |
| headers = {'api-key': self.apikey} | |
| res = requests.post(endpoint, json=payloads, headers=headers) | |
| # self.__save_res(res) | |
| # print('fft__________') | |
| # print(res) | |
| return self.parse_res(res) | |
| def __save_res(self, res): | |
| # The successful res is based on "data/test.csv" | |
| script_dir = os.path.dirname(__file__) | |
| rel_path = "./tests/fft_success_res.dict" | |
| abs_file_path = os.path.join(script_dir, rel_path) | |
| with open(abs_file_path, 'wb') as f: | |
| pickle.dump(res, f) | |
| def __load_res(self): | |
| # The successful res is based on "data/test.csv" | |
| script_dir = os.path.dirname(__file__) | |
| rel_path = "./tests/fft_success_res.dict" | |
| abs_file_path = os.path.join(script_dir, rel_path) | |
| with open(abs_file_path, 'rb') as f: | |
| res = pickle.load(f) | |
| return res | |