Q-bert commited on
Commit
e81e578
1 Parent(s): 3ac2401

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -24
app.py CHANGED
@@ -17,6 +17,26 @@ import spaces
17
  HF_TOKEN = os.getenv('HF_TOKEN')
18
  WANDB_TOKEN = os.getenv('WANDB_TOKEN')
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @spaces.GPU
22
  def train_stock_model(stock_symbol, start_date, end_date, feature_range=(10, 100), data_seq_length=256, epochs=10, batch_size=16, learning_rate=2e-4):
@@ -28,27 +48,6 @@ def train_stock_model(stock_symbol, start_date, end_date, feature_range=(10, 100
28
 
29
  data = stock_data["Close"]
30
 
31
- class Scaler:
32
- def __init__(self, feature_range):
33
- self.feature_range = feature_range
34
- self.min_df = None
35
- self.max_df = None
36
-
37
- def fit(self, df: pd.Series):
38
- self.min_df = df.min()
39
- self.max_df = df.max()
40
-
41
- def transform(self, df: pd.Series) -> pd.Series:
42
- min_val, max_val = self.feature_range
43
- scaled_df = (df - self.min_df) / (self.max_df - self.min_df)
44
- scaled_df = scaled_df * (max_val - min_val) + min_val
45
- return scaled_df
46
-
47
- def inverse_transform(self, X: np.ndarray) -> np.ndarray:
48
- min_val, max_val = self.feature_range
49
- min_x, max_x = np.min(X), np.max(X)
50
- return (X - min_x) / (max_x - min_x) * (max_val - min_val) + min_val
51
-
52
  scaler = Scaler(feature_range)
53
  scaler.fit(data)
54
  scaled_data = scaler.transform(data)
@@ -56,12 +55,11 @@ def train_stock_model(stock_symbol, start_date, end_date, feature_range=(10, 100
56
  seq = [np.array(scaled_data[i:i + data_seq_length]) for i in range(len(scaled_data) - data_seq_length)]
57
  target = [np.array(scaled_data[i + data_seq_length:i + data_seq_length + 1]) for i in range(len(scaled_data) - data_seq_length)]
58
 
59
- seq_tensors = [torch.tensor(s, dtype=torch.float32) for s in seq]
60
  target_tensors = [t[0] for t in target]
61
 
62
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
63
  model = StockLlamaForForecasting.from_pretrained("Q-bert/StockLlama").to(device)
64
- print("Model Installed.")
65
  config = LoraConfig(
66
  r=64,
67
  lora_alpha=32,
@@ -71,7 +69,7 @@ def train_stock_model(stock_symbol, start_date, end_date, feature_range=(10, 100
71
  task_type="CAUSAL_LM",
72
  )
73
  model = get_peft_model(model, config)
74
- print("Model pefted.")
75
  login(token=HF_TOKEN)
76
  wandb.login(key=WANDB_TOKEN)
77
  dct = {"input_ids": seq_tensors, "label": target_tensors}
@@ -110,6 +108,7 @@ def train_stock_model(stock_symbol, start_date, end_date, feature_range=(10, 100
110
  path_in_repo=f"scalers/{scaler_path}",
111
  repo_id=f"Q-bert/StockLlama-tuned-{stock_symbol}"
112
  )
 
113
  @spaces.GPU
114
  def gradio_train_stock_model(stock_symbol, start_date, end_date, feature_range_min, feature_range_max, data_seq_length, epochs, batch_size, learning_rate):
115
  feature_range = (feature_range_min, feature_range_max)
 
17
  HF_TOKEN = os.getenv('HF_TOKEN')
18
  WANDB_TOKEN = os.getenv('WANDB_TOKEN')
19
 
20
+ class Scaler:
21
+ def __init__(self, feature_range):
22
+ self.feature_range = feature_range
23
+ self.min_df = None
24
+ self.max_df = None
25
+
26
+ def fit(self, df: pd.Series):
27
+ self.min_df = df.min()
28
+ self.max_df = df.max()
29
+
30
+ def transform(self, df: pd.Series) -> pd.Series:
31
+ min_val, max_val = self.feature_range
32
+ scaled_df = (df - self.min_df) / (self.max_df - self.min_df)
33
+ scaled_df = scaled_df * (max_val - min_val) + min_val
34
+ return scaled_df
35
+
36
+ def inverse_transform(self, X: np.ndarray) -> np.ndarray:
37
+ min_val, max_val = self.feature_range
38
+ min_x, max_x = np.min(X), np.max(X)
39
+ return (X - min_x) / (max_x - min_x) * (max_val - min_val) + min_val
40
 
41
  @spaces.GPU
42
  def train_stock_model(stock_symbol, start_date, end_date, feature_range=(10, 100), data_seq_length=256, epochs=10, batch_size=16, learning_rate=2e-4):
 
48
 
49
  data = stock_data["Close"]
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  scaler = Scaler(feature_range)
52
  scaler.fit(data)
53
  scaled_data = scaler.transform(data)
 
55
  seq = [np.array(scaled_data[i:i + data_seq_length]) for i in range(len(scaled_data) - data_seq_length)]
56
  target = [np.array(scaled_data[i + data_seq_length:i + data_seq_length + 1]) for i in range(len(scaled_data) - data_seq_length)]
57
 
58
+ seq_tensors = [torch.tensor(s, dtype=torch.float32).unsqueeze(0) for s in seq]
59
  target_tensors = [t[0] for t in target]
60
 
61
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
62
  model = StockLlamaForForecasting.from_pretrained("Q-bert/StockLlama").to(device)
 
63
  config = LoraConfig(
64
  r=64,
65
  lora_alpha=32,
 
69
  task_type="CAUSAL_LM",
70
  )
71
  model = get_peft_model(model, config)
72
+
73
  login(token=HF_TOKEN)
74
  wandb.login(key=WANDB_TOKEN)
75
  dct = {"input_ids": seq_tensors, "label": target_tensors}
 
108
  path_in_repo=f"scalers/{scaler_path}",
109
  repo_id=f"Q-bert/StockLlama-tuned-{stock_symbol}"
110
  )
111
+
112
  @spaces.GPU
113
  def gradio_train_stock_model(stock_symbol, start_date, end_date, feature_range_min, feature_range_max, data_seq_length, epochs, batch_size, learning_rate):
114
  feature_range = (feature_range_min, feature_range_max)