Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -10,12 +10,18 @@ from peft import LoraConfig, get_peft_model
|
|
10 |
from datasets import Dataset
|
11 |
import os
|
12 |
from transformers import Trainer, TrainingArguments
|
13 |
-
from huggingface_hub import login, upload_file
|
14 |
import wandb
|
15 |
import gradio as gr
|
16 |
import spaces
|
|
|
|
|
|
|
|
|
17 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
18 |
WANDB_TOKEN = os.getenv('WANDB_TOKEN')
|
|
|
|
|
19 |
|
20 |
class Scaler:
|
21 |
def __init__(self, feature_range):
|
@@ -38,14 +44,24 @@ class Scaler:
|
|
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):
|
|
|
|
|
|
|
|
|
|
|
43 |
try:
|
44 |
stock_data = yf.download(stock_symbol, start=start_date, end=end_date, progress=False)
|
45 |
except Exception as e:
|
46 |
print(f"Error downloading data for {stock_symbol}: {e}")
|
47 |
return
|
48 |
-
|
49 |
data = stock_data["Close"]
|
50 |
|
51 |
scaler = Scaler(feature_range)
|
@@ -55,7 +71,7 @@ def train_stock_model(stock_symbol, start_date, end_date, feature_range=(10, 100
|
|
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)
|
59 |
target_tensors = [t[0] for t in target]
|
60 |
|
61 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
@@ -70,11 +86,9 @@ def train_stock_model(stock_symbol, start_date, end_date, feature_range=(10, 100
|
|
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}
|
76 |
dataset = Dataset.from_dict(dct)
|
77 |
-
dataset.push_to_hub(
|
78 |
trainer = Trainer(
|
79 |
model=model,
|
80 |
train_dataset=dataset,
|
@@ -108,11 +122,12 @@ def train_stock_model(stock_symbol, start_date, end_date, feature_range=(10, 100
|
|
108 |
path_in_repo=f"scalers/{scaler_path}",
|
109 |
repo_id=f"Q-bert/StockLlama-tuned-{stock_symbol}-{stock_symbol}-{start_date}_{end_date}"
|
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)
|
115 |
-
train_stock_model(
|
116 |
stock_symbol=stock_symbol,
|
117 |
start_date=start_date,
|
118 |
end_date=end_date,
|
@@ -122,7 +137,7 @@ def gradio_train_stock_model(stock_symbol, start_date, end_date, feature_range_m
|
|
122 |
batch_size=batch_size,
|
123 |
learning_rate=learning_rate
|
124 |
)
|
125 |
-
return
|
126 |
|
127 |
iface = gr.Interface(
|
128 |
fn=gradio_train_stock_model,
|
@@ -140,4 +155,4 @@ iface = gr.Interface(
|
|
140 |
outputs="text",
|
141 |
)
|
142 |
|
143 |
-
iface.launch()
|
|
|
10 |
from datasets import Dataset
|
11 |
import os
|
12 |
from transformers import Trainer, TrainingArguments
|
13 |
+
from huggingface_hub import login, upload_file, hf_hub_download
|
14 |
import wandb
|
15 |
import gradio as gr
|
16 |
import spaces
|
17 |
+
from huggingface_hub import HfApi
|
18 |
+
|
19 |
+
hf_api = HfApi()
|
20 |
+
|
21 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
22 |
WANDB_TOKEN = os.getenv('WANDB_TOKEN')
|
23 |
+
login(token=HF_TOKEN)
|
24 |
+
wandb.login(key=WANDB_TOKEN)
|
25 |
|
26 |
class Scaler:
|
27 |
def __init__(self, feature_range):
|
|
|
44 |
min_x, max_x = np.min(X), np.max(X)
|
45 |
return (X - min_x) / (max_x - min_x) * (max_val - min_val) + min_val
|
46 |
|
47 |
+
def check_existing_model(stock_symbol, start_date, end_date):
|
48 |
+
repo_id = f"Q-bert/StockLlama-tuned-{stock_symbol}-{stock_symbol}-{start_date}_{end_date}"
|
49 |
+
state = repo_id in [model.modelId for model in hf_api.list_models()]
|
50 |
+
return state
|
51 |
+
|
52 |
@spaces.GPU
|
53 |
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):
|
54 |
+
repo_id = f"Q-bert/StockLlama-tuned-{stock_symbol}-{stock_symbol}-{start_date}_{end_date}"
|
55 |
+
|
56 |
+
if check_existing_model(stock_symbol, start_date, end_date):
|
57 |
+
return f"Model for {stock_symbol} from {start_date} to {end_date} already exists."
|
58 |
+
|
59 |
try:
|
60 |
stock_data = yf.download(stock_symbol, start=start_date, end=end_date, progress=False)
|
61 |
except Exception as e:
|
62 |
print(f"Error downloading data for {stock_symbol}: {e}")
|
63 |
return
|
64 |
+
|
65 |
data = stock_data["Close"]
|
66 |
|
67 |
scaler = Scaler(feature_range)
|
|
|
71 |
seq = [np.array(scaled_data[i:i + data_seq_length]) for i in range(len(scaled_data) - data_seq_length)]
|
72 |
target = [np.array(scaled_data[i + data_seq_length:i + data_seq_length + 1]) for i in range(len(scaled_data) - data_seq_length)]
|
73 |
|
74 |
+
seq_tensors = [torch.tensor(s, dtype=torch.float32) for s in seq]
|
75 |
target_tensors = [t[0] for t in target]
|
76 |
|
77 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
|
86 |
)
|
87 |
model = get_peft_model(model, config)
|
88 |
|
|
|
|
|
89 |
dct = {"input_ids": seq_tensors, "label": target_tensors}
|
90 |
dataset = Dataset.from_dict(dct)
|
91 |
+
dataset.push_to_hub(repo_id)
|
92 |
trainer = Trainer(
|
93 |
model=model,
|
94 |
train_dataset=dataset,
|
|
|
122 |
path_in_repo=f"scalers/{scaler_path}",
|
123 |
repo_id=f"Q-bert/StockLlama-tuned-{stock_symbol}-{stock_symbol}-{start_date}_{end_date}"
|
124 |
)
|
125 |
+
return f"Training completed and model saved for {stock_symbol} from {start_date} to {end_date}."
|
126 |
|
127 |
@spaces.GPU
|
128 |
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):
|
129 |
feature_range = (feature_range_min, feature_range_max)
|
130 |
+
result = train_stock_model(
|
131 |
stock_symbol=stock_symbol,
|
132 |
start_date=start_date,
|
133 |
end_date=end_date,
|
|
|
137 |
batch_size=batch_size,
|
138 |
learning_rate=learning_rate
|
139 |
)
|
140 |
+
return result
|
141 |
|
142 |
iface = gr.Interface(
|
143 |
fn=gradio_train_stock_model,
|
|
|
155 |
outputs="text",
|
156 |
)
|
157 |
|
158 |
+
iface.launch()
|