msn-enginenova21 commited on
Commit
54de379
·
1 Parent(s): 5c69a6f

Update components/model_prediction.py

Browse files
Files changed (1) hide show
  1. components/model_prediction.py +57 -0
components/model_prediction.py CHANGED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from Support_module_dir.support_function_predict import predict_function
2
+ from Variable_artifects.artifact import CSV_FILE
3
+ from Variable_artifects.artifact import CSV_DIR
4
+ from datetime import datetime
5
+ import pandas as pd
6
+ import logging
7
+ import joblib
8
+ import os
9
+
10
+
11
+ class Prediction:
12
+ def __init__(self, saved_model_path):
13
+ self.saved_model_path = saved_model_path
14
+
15
+ @staticmethod
16
+ def convert_timestamp(timestamp):
17
+ """
18
+ Function return timestamps
19
+
20
+ during prediction date column print into datestamps into millisecond started from 1970 till date.
21
+ to convert that back to today's time function is required
22
+ """
23
+ try:
24
+ if isinstance(timestamp, pd.Timestamp):
25
+ timestamp = timestamp.value // 10 ** 6 # Convert nanoseconds to milliseconds
26
+ return datetime.utcfromtimestamp(timestamp / 1000).strftime('%Y-%m-%d')
27
+
28
+ except Exception as e:
29
+ raise e
30
+
31
+ def model_prediction(self, forecast_days):
32
+ """
33
+ Function created for Data Ingestion
34
+
35
+ Returns: Pandas processed Dataframe and predicted values
36
+ """
37
+ try:
38
+ CSV_PATH = os.path.join(CSV_DIR, CSV_FILE) # called from artifact module
39
+ logging.info(f"Model Prediction Module : Initiating module prediction")
40
+ dataset_frame = pd.read_csv(CSV_PATH)
41
+ dataset_frame.set_index('Date', inplace=True) # setting date columns as index
42
+ logging.info(f"Model Prediction Module : Loading model")
43
+ model = joblib.load(self.saved_model_path) # Load the trained model
44
+
45
+ # prediction function called from support_function material
46
+ logging.info(f"Model Prediction Module : prediction function called")
47
+ predictions, execution_time = predict_function(trained_model=model,
48
+ dataset=dataset_frame.Price,
49
+ forecast_days=forecast_days)
50
+
51
+ # Convert timestamps to a more readable date format
52
+ predictions['Date'] = predictions['Date'].apply(Prediction.convert_timestamp)
53
+ logging.info(f"Model Prediction Module : Exiting module STATUS OK")
54
+ return predictions, dataset_frame
55
+
56
+ except Exception as e:
57
+ logging.info(f"Model Prediction module: model prediction failed {e}")