Spaces:
Running
Running
feat: rewrite ML utilities to Cython
Browse files- Makefile +2 -0
- diagram/cryptocurrency_prediction.ai +0 -0
- diagram/cryptocurrency_prediction.jpg +0 -0
- restful/cutils/build/lib.linux-x86_64-3.10/utilities.cpython-310-x86_64-linux-gnu.so +0 -0
- restful/cutils/build/temp.linux-x86_64-3.10/utilities.o +0 -0
- restful/cutils/setup.py +9 -0
- restful/cutils/utilities.c +0 -0
- restful/cutils/utilities.cpython-310-x86_64-linux-gnu.so +0 -0
- restful/cutils/utilities.pyx +56 -0
- restful/services.py +1 -1
Makefile
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cutils:
|
| 2 |
+
cd restful/cutils && python setup.py build_ext --inplace && cd ../..
|
diagram/cryptocurrency_prediction.ai
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
diagram/cryptocurrency_prediction.jpg
CHANGED
|
|
restful/cutils/build/lib.linux-x86_64-3.10/utilities.cpython-310-x86_64-linux-gnu.so
ADDED
|
Binary file (360 kB). View file
|
|
|
restful/cutils/build/temp.linux-x86_64-3.10/utilities.o
ADDED
|
Binary file (491 kB). View file
|
|
|
restful/cutils/setup.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from setuptools import setup
|
| 2 |
+
from Cython.Build import cythonize
|
| 3 |
+
import numpy
|
| 4 |
+
|
| 5 |
+
setup(
|
| 6 |
+
ext_modules=cythonize("utilities.pyx"),
|
| 7 |
+
include_dirs=[numpy.get_include()]
|
| 8 |
+
)
|
| 9 |
+
|
restful/cutils/utilities.c
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
restful/cutils/utilities.cpython-310-x86_64-linux-gnu.so
ADDED
|
Binary file (360 kB). View file
|
|
|
restful/cutils/utilities.pyx
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from joblib import load
|
| 3 |
+
from numpy import append, expand_dims
|
| 4 |
+
from pandas import read_json, to_datetime, Timedelta
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
import cython
|
| 7 |
+
|
| 8 |
+
cdef class Utilities:
|
| 9 |
+
async def cryptocurrency_prediction_utils(self,
|
| 10 |
+
int days, int sequence_length, str model_name) -> tuple:
|
| 11 |
+
cdef str model_path = os.path.join('./models', f'{model_name}.keras')
|
| 12 |
+
model = load_model(model_path)
|
| 13 |
+
|
| 14 |
+
cdef str dataframe_path = os.path.join('./posttrained', f'{model_name}-posttrained.json')
|
| 15 |
+
dataframe = read_json(dataframe_path)
|
| 16 |
+
dataframe.set_index('Date', inplace=True)
|
| 17 |
+
|
| 18 |
+
minmax_scaler = load(os.path.join('./pickles', f'{model_name}_minmax_scaler.pickle'))
|
| 19 |
+
standard_scaler = load(os.path.join('./pickles', f'{model_name}_standard_scaler.pickle'))
|
| 20 |
+
|
| 21 |
+
# Prediction
|
| 22 |
+
lst_seq = dataframe[-sequence_length:].values
|
| 23 |
+
lst_seq = expand_dims(lst_seq, axis=0)
|
| 24 |
+
|
| 25 |
+
cdef dict predicted_prices = {}
|
| 26 |
+
last_date = to_datetime(dataframe.index[-1])
|
| 27 |
+
|
| 28 |
+
for _ in range(days):
|
| 29 |
+
predicted_price = model.predict(lst_seq)
|
| 30 |
+
last_date = last_date + Timedelta(days=1)
|
| 31 |
+
|
| 32 |
+
predicted_prices[last_date] = minmax_scaler.inverse_transform(predicted_price)
|
| 33 |
+
predicted_prices[last_date] = standard_scaler.inverse_transform(predicted_prices[last_date])
|
| 34 |
+
|
| 35 |
+
lst_seq = append(lst_seq[:, 1:, :], [predicted_price], axis=1)
|
| 36 |
+
|
| 37 |
+
predictions = [
|
| 38 |
+
{'date': date.strftime('%Y-%m-%d'), 'price': float(price)} \
|
| 39 |
+
for date, price in predicted_prices.items()
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
# Actual
|
| 43 |
+
df_date = dataframe.index[-sequence_length:].values
|
| 44 |
+
df_date = [to_datetime(date) for date in df_date]
|
| 45 |
+
|
| 46 |
+
dataframe[['Close']] = minmax_scaler.inverse_transform(dataframe)
|
| 47 |
+
dataframe[['Close']] = standard_scaler.inverse_transform(dataframe)
|
| 48 |
+
df_close = dataframe.iloc[-sequence_length:]['Close'].values
|
| 49 |
+
|
| 50 |
+
actuals = [
|
| 51 |
+
{'date': date.strftime('%Y-%m-%d'), 'price': close} \
|
| 52 |
+
for date, close in zip(df_date, df_close)
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
return actuals, predictions
|
| 56 |
+
|
restful/services.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from restful.utilities import Utilities
|
| 2 |
from restful.schemas import CryptocurrencyPredictionSchema
|
| 3 |
|
| 4 |
class cryptocurrency_svc:
|
|
|
|
| 1 |
+
from restful.cutils.utilities import Utilities
|
| 2 |
from restful.schemas import CryptocurrencyPredictionSchema
|
| 3 |
|
| 4 |
class cryptocurrency_svc:
|