Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading MlForecast Model in Docker. #463

Closed
IfenaikeElias opened this issue Dec 6, 2024 · 3 comments
Closed

Loading MlForecast Model in Docker. #463

IfenaikeElias opened this issue Dec 6, 2024 · 3 comments
Labels

Comments

@IfenaikeElias
Copy link

What happened + What you expected to happen

REST API works fine locally, but when i load my model in docker it gives an error:


docker run -p 8000:8000 ensom-forecast-power-api
ERROR:main:An error occurred while loading the model: code expected at most 16 arguments, got 18
Traceback (most recent call last):
File "/usr/local/bin/uvicorn", line 8, in
sys.exit(main())
File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1157, in call
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1078, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.10/site-packages/click/core.py", line 783, in invoke
return __callback(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/uvicorn/main.py", line 412, in main
run(
File "/usr/local/lib/python3.10/site-packages/uvicorn/main.py", line 579, in run
server.run()
File "/usr/local/lib/python3.10/site-packages/uvicorn/server.py", line 65, in run
return asyncio.run(self.serve(sockets=sockets))
File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
return future.result()
File "/usr/local/lib/python3.10/site-packages/uvicorn/server.py", line 69, in serve
await self._serve(sockets)
File "/usr/local/lib/python3.10/site-packages/uvicorn/server.py", line 76, in _serve
config.load()
File "/usr/local/lib/python3.10/site-packages/uvicorn/config.py", line 434, in load
self.loaded_app = import_from_string(self.app)
File "/usr/local/lib/python3.10/site-packages/uvicorn/importer.py", line 19, in import_from_string
module = importlib.import_module(module_str)
File "/usr/local/lib/python3.10/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1050, in _gcd_import
File "", line 1027, in _find_and_load
File "", line 1006, in _find_and_load_unlocked
File "", line 688, in _load_unlocked
File "", line 883, in exec_module
File "", line 241, in _call_with_frames_removed
File "/app/main.py", line 33, in
Power_model = cloudpickle.load(f)
File "/usr/local/lib/python3.10/site-packages/numba/core/serialize.py", line 110, in _unpickle__CustomPickled
ctor, states = loads(serialized)
TypeError: code expected at most 16 arguments, got 18


Versions / Dependencies

pandas==2.2.3
numpy==2.0.2
fastapi==0.115.6
plotly==5.24.1
pydantic==2.10.3
joblib==1.4.2
uvicorn==0.32.1
window_ops==0.0.15
MlForecast==0.15.1
xgboost==2.1.3

Reproduction script

import logging
import pandas as pd
import cloudpickle
import os
from mlforecast import MLForecast
from pydantic import BaseModel
from datetime import datetime

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(name)

model_path = r'C:\Users\user\Desktop\Power_forecast\power_model_mlforecast.pkl'

try:
with open(model_path, 'rb') as f:
Power_model = cloudpickle.load(f)
logger.info("Power consumption model loaded successfully.")
except FileNotFoundError:
logger.error(f"Model file not found at {model_path}. Please check the path.")
raise
except Exception as e:
logger.error(f"Error loading model: {e}")
raise

Example Data to forecast

historical_data = [
{"timestamp": "2024-01-01 00:00:00", "site1": 100.5},
{"timestamp": "2024-01-01 01:00:00", "site2": 102.3},
...
]

historical_data_df = pd.DataFrame(historical_data)
historical_data_df['timestamp'] = pd.to_datetime(historical_data_df['timestamp'])

def convert_to_long_format(df, timestamp_col='timestamp', var_name='SiteName', value_name='AC Consumption'):
value_vars = [col for col in df.columns if col != timestamp_col]
long_df = pd.melt(df, id_vars=[timestamp_col], value_vars=value_vars, var_name=var_name, value_name=value_name)
long_df = long_df.sort_values(by=[timestamp_col, var_name]).reset_index(drop=True)
return long_df

long_data = convert_to_long_format(historical_data_df)

try:
forecast = Power_model.forecast(long_data)
logger.info(f"Forecast result: {forecast}")
except Exception as e:
logger.error(f"Error during forecasting: {e}")

Issue Severity

High: It blocks me from completing my task.

@jmoralez
Copy link
Member

jmoralez commented Dec 6, 2024

Are you using the same versions of the libraries and python in both environments? Seems like you saved it with a different python version.

@IfenaikeElias
Copy link
Author

Thank you very much @jmoralez It worked.

@IfenaikeElias
Copy link
Author

I'll close this for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants