omniverse1 commited on
Commit
a90bc0e
·
verified ·
1 Parent(s): 405965a

Update model_handler.py

Browse files
Files changed (1) hide show
  1. model_handler.py +37 -46
model_handler.py CHANGED
@@ -1,62 +1,44 @@
1
- import torch
2
  import numpy as np
3
- from transformers import AutoTokenizer, AutoConfig
4
- from huggingface_hub import hf_hub_download
5
- import json
6
- import os
7
 
8
  class ModelHandler:
9
  def __init__(self):
10
- self.model_name = "amazon/chronos-t5-small" # Using smaller model for CPU
11
- self.tokenizer = None
12
- self.model = None
13
- self.device = "cpu"
 
14
  self.load_model()
15
 
16
  def load_model(self):
17
- """Load Chronos model optimized for CPU"""
18
  try:
19
- print(f"Loading {self.model_name}...")
20
-
21
- # Download config
22
- config_path = hf_hub_download(
23
- repo_id=self.model_name,
24
- filename="config.json"
25
- )
26
-
27
- with open(config_path, 'r') as f:
28
- config = json.load(f)
29
-
30
- # Initialize tokenizer
31
- self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
32
 
33
- # For CPU optimization, use TorchScript if available
34
- model_path = hf_hub_download(
35
- repo_id=self.model_name,
36
- filename="model.safetensors"
37
  )
38
-
39
- # Load model state dict
40
- from safetensors.torch import load_file
41
- state_dict = load_file(model_path)
42
-
43
- # Create model from config (simplified for CPU)
44
- # In production, would load full model architecture
45
- print("Model loaded successfully (optimized for CPU)")
46
 
47
  except Exception as e:
48
- print(f"Error loading model: {e}")
49
  print("Using fallback prediction method")
50
- self.model = None
51
 
52
  def predict(self, data, horizon=10):
53
- """Generate predictions using Chronos or fallback"""
54
  try:
55
- if data is None or len(data['values']) < 20:
 
56
  return np.array([0] * horizon)
57
 
58
- if self.model is None:
59
- # Fallback: Use simple trend extrapolation for CPU efficiency
 
60
  values = data['original']
61
  recent_trend = np.polyfit(range(len(values[-20:])), values[-20:], 1)[0]
62
 
@@ -64,18 +46,27 @@ class ModelHandler:
64
  last_value = values[-1]
65
 
66
  for i in range(horizon):
67
- # Add trend with some noise
68
  next_value = last_value + recent_trend * (i + 1)
69
- # Add realistic market noise
70
  noise = np.random.normal(0, data['std'] * 0.1)
71
  predictions.append(next_value + noise)
72
 
73
  return np.array(predictions)
74
 
75
- # In production, would implement full Chronos inference
76
- # For now, return fallback
77
- return self.predict(data, horizon) # Recursive call to fallback
 
 
 
 
 
 
 
 
 
 
78
 
79
  except Exception as e:
80
  print(f"Prediction error: {e}")
 
81
  return np.array([0] * horizon)
 
 
1
  import numpy as np
2
+ import torch
3
+ # Menggunakan ChronosPipeline untuk pemuatan dan inferensi yang efisien
4
+ from chronos import ChronosPipeline
 
5
 
6
  class ModelHandler:
7
  def __init__(self):
8
+ # Mengganti model lama dengan Chronos-2 yang lebih canggih
9
+ self.model_name = "amazon/chronos-2"
10
+ self.pipeline = None
11
+ # Penentuan device: "cuda" jika ada GPU, jika tidak "cpu"
12
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
13
  self.load_model()
14
 
15
  def load_model(self):
16
+ """Load Chronos-2 model optimized for CPU/GPU"""
17
  try:
18
+ print(f"Loading {self.model_name} on {self.device}...")
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ # ChronosPipeline menangani semua proses tokenisasi dan pemuatan arsitektur
21
+ self.pipeline = ChronosPipeline.from_pretrained(
22
+ self.model_name,
23
+ device_map=self.device,
24
  )
25
+ print("Chronos-2 pipeline loaded successfully.")
 
 
 
 
 
 
 
26
 
27
  except Exception as e:
28
+ print(f"Error loading Chronos-2 model: {e}")
29
  print("Using fallback prediction method")
30
+ self.pipeline = None
31
 
32
  def predict(self, data, horizon=10):
33
+ """Generate predictions using Chronos-2 or fallback"""
34
  try:
35
+ # Menggunakan data['original'] yang merupakan harga aktual riil
36
+ if data is None or len(data['original']) < 20:
37
  return np.array([0] * horizon)
38
 
39
+ if self.pipeline is None:
40
+ # --- Fallback Logic ---
41
+ # Logic ekstrapolasi tren lama tetap dipertahankan jika model Deep Learning gagal dimuat
42
  values = data['original']
43
  recent_trend = np.polyfit(range(len(values[-20:])), values[-20:], 1)[0]
44
 
 
46
  last_value = values[-1]
47
 
48
  for i in range(horizon):
 
49
  next_value = last_value + recent_trend * (i + 1)
 
50
  noise = np.random.normal(0, data['std'] * 0.1)
51
  predictions.append(next_value + noise)
52
 
53
  return np.array(predictions)
54
 
55
+ # --- Chronos-2 Inference ---
56
+ # Input: numpy array dari harga Close historis yang riil
57
+ predictions_samples = self.pipeline.predict(
58
+ data['original'],
59
+ prediction_length=horizon,
60
+ # Mengambil 20 sampel prediksi untuk mendapatkan prediksi probablistik
61
+ num_samples=20
62
+ )
63
+
64
+ # Untuk chart (garis tunggal), ambil nilai rata-rata (mean) dari semua sampel.
65
+ mean_predictions = np.mean(predictions_samples, axis=0)
66
+
67
+ return mean_predictions
68
 
69
  except Exception as e:
70
  print(f"Prediction error: {e}")
71
+ # Mengembalikan array nol jika ada error saat inferensi Chronos
72
  return np.array([0] * horizon)