Spaces:
Running
Running
Update finetuned_model.py
Browse files- finetuned_model.py +39 -15
finetuned_model.py
CHANGED
|
@@ -68,21 +68,46 @@ for _, row in df.iterrows():
|
|
| 68 |
"summary": f"On {date}, the S&P 500 closed at {sp500:.2f} with a {return_val:.1f}% annual return and a {real_return:.1f}% real return."
|
| 69 |
})
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
periods = [(2000, 2010), (2011, 2016), (2010, 2020)]
|
| 74 |
for start_year, end_year in periods:
|
| 75 |
df_period = df[(df['Date'].dt.year >= start_year) & (df['Date'].dt.year <= end_year)]
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Add general S&P 500 growth rate question
|
| 88 |
qa_pairs.append({
|
|
@@ -153,8 +178,7 @@ trainer.save_model("./finetuned_model")
|
|
| 153 |
tokenizer.save_pretrained("./finetuned_model")
|
| 154 |
|
| 155 |
# Test the model
|
| 156 |
-
input_text = "What is the
|
| 157 |
inputs = tokenizer(input_text, return_tensors="pt")
|
| 158 |
outputs = model.generate(**inputs, max_new_tokens=50)
|
| 159 |
-
|
| 160 |
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
|
|
|
| 68 |
"summary": f"On {date}, the S&P 500 closed at {sp500:.2f} with a {return_val:.1f}% annual return and a {real_return:.1f}% real return."
|
| 69 |
})
|
| 70 |
|
| 71 |
+
# Period-specific questions
|
| 72 |
+
periods = [(2000, 2010), (2011, 2016), (2010, 2020), (2018, 2020), (2020, 2023)]
|
|
|
|
| 73 |
for start_year, end_year in periods:
|
| 74 |
df_period = df[(df['Date'].dt.year >= start_year) & (df['Date'].dt.year <= end_year)]
|
| 75 |
+
if not df_period.empty:
|
| 76 |
+
avg_return = df_period['Return'].mean()
|
| 77 |
+
avg_real_return = df_period['Real Return'].mean()
|
| 78 |
+
qa_pairs.append({
|
| 79 |
+
"question": f"What is the average annual growth rate of the S&P 500 from {start_year} to {end_year}?",
|
| 80 |
+
"answer": f"The S&P 500’s average annual growth rate from {start_year} to {end_year} was approximately {avg_return:.1f}%, including dividends."
|
| 81 |
+
})
|
| 82 |
+
qa_pairs.append({
|
| 83 |
+
"question": f"What was the S&P 500’s real return from {start_year} to {end_year}?",
|
| 84 |
+
"answer": f"The S&P 500’s average annual inflation-adjusted return from {start_year} to {end_year} was approximately {avg_real_return:.1f}%."
|
| 85 |
+
})
|
| 86 |
+
|
| 87 |
+
# Relative period questions (e.g., last 1, 3, 5 years)
|
| 88 |
+
current_year = 2025
|
| 89 |
+
relative_periods = [1, 3, 5]
|
| 90 |
+
for years in relative_periods:
|
| 91 |
+
start_year = current_year - years
|
| 92 |
+
df_period = df[(df['Date'].dt.year >= start_year) & (df['Date'].dt.year <= current_year)]
|
| 93 |
+
if not df_period.empty:
|
| 94 |
+
avg_return = df_period['Return'].mean()
|
| 95 |
+
qa_pairs.append({
|
| 96 |
+
"question": f"What is the S&P 500 growth rate for the last {years} years?",
|
| 97 |
+
"answer": f"The S&P 500’s average annual growth rate for the last {years} years (from {start_year} to {current_year}) was approximately {avg_return:.1f}%, including dividends."
|
| 98 |
+
})
|
| 99 |
+
|
| 100 |
+
# Investment return questions
|
| 101 |
+
amounts = [1000, 5000, 10000]
|
| 102 |
+
years = [1, 3, 5, 10, 20]
|
| 103 |
+
avg_annual_return = 10.0 # Historical S&P 500 average
|
| 104 |
+
for amount in amounts:
|
| 105 |
+
for n in years:
|
| 106 |
+
future_value = amount * (1 + avg_annual_return / 100) ** n
|
| 107 |
+
qa_pairs.append({
|
| 108 |
+
"question": f"What will ${amount} be worth in {n} years if invested in the S&P 500?",
|
| 109 |
+
"answer": f"Assuming a 10% average annual return, ${amount:,.0f} invested in the S&P 500 would grow to approximately ${future_value:,.0f} in {n} years with annual compounding."
|
| 110 |
+
})
|
| 111 |
|
| 112 |
# Add general S&P 500 growth rate question
|
| 113 |
qa_pairs.append({
|
|
|
|
| 178 |
tokenizer.save_pretrained("./finetuned_model")
|
| 179 |
|
| 180 |
# Test the model
|
| 181 |
+
input_text = "What is the S&P 500 growth rate for the last 3 years?"
|
| 182 |
inputs = tokenizer(input_text, return_tensors="pt")
|
| 183 |
outputs = model.generate(**inputs, max_new_tokens=50)
|
|
|
|
| 184 |
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|