# %%
from dotenv import load_dotenv
load_dotenv(override=True)
# %%
import os
api_key = os.environ.get("CLOUD_API_KEY")
print(api_key)
project_id = os.environ.get("PROJECT_ID")
print(project_id)
# %%
from ibm_watsonx_ai import Credentials
credentials = Credentials(
url="https://us-south.ml.cloud.ibm.com",
api_key=api_key,
)
# %%
from ibm_watsonx_ai import APIClient
client = APIClient(credentials)
# %%
client.set.default_project(project_id)
# %%
for model in client.foundation_models.get_time_series_model_specs()["resources"]:
print('--------------------------------------------------')
print(f'model_id: {model["model_id"]}')
print(f'functions: {model["functions"]}')
print(f'long_description: {model["long_description"]}')
print(f'label: {model["label"]}')
# %%
ts_model_id = client.foundation_models.TimeSeriesModels.GRANITE_TTM_512_96_R2
# %%
from ibm_watsonx_ai.foundation_models import TSModelInference
ts_model = TSModelInference(
model_id=ts_model_id,
api_client=client
)
# %%
from ibm_watsonx_ai.foundation_models.schema import TSForecastParameters
forecasting_params = TSForecastParameters(
timestamp_column=timestamp_column,
freq="1h",
target_columns=[target_column],
)
# %%
results = ts_model.forecast(data=data, params=forecasting_params)['results'][0]
print(results)
# %%
plt.figure(figsize=(10,2))
plt.plot(np.asarray(data[timestamp_column], dtype='datetime64[s]'), data[target_column], label="Historical data")
plt.plot(np.asarray(results[timestamp_column], dtype='datetime64[s]'), results[target_column], label="Predicted")
plt.plot(np.asarray(future_data[timestamp_column], dtype='datetime64[s]'), future_data[target_column], label="True", linestyle='dashed')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()