Spaces:
Runtime error
Runtime error
OfirMatzlawi
commited on
Commit
•
1d2c83a
1
Parent(s):
e547a1d
Update main.py
Browse files
main.py
CHANGED
@@ -27,12 +27,53 @@ def read_root():
|
|
27 |
|
28 |
}
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
|
32 |
@app.get("/ticker/{ticker}")
|
33 |
def read_item(ticker: str):
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
37 |
|
38 |
|
|
|
27 |
|
28 |
}
|
29 |
|
30 |
+
## TimeGPT
|
31 |
+
|
32 |
+
|
33 |
+
def get_data_from_yahoo(ticker: str):
|
34 |
+
yf.pdr_override()
|
35 |
+
data = pdr.get_data_yahoo(ticker, start="2023-01-01", end=None)
|
36 |
+
df = pd.DataFrame(data).reset_index()
|
37 |
+
return df
|
38 |
+
|
39 |
+
##TimeGPT APIkey validation
|
40 |
+
|
41 |
+
api_key = 'nixt-7nixiWJtV1TwD3vp9K8WqLrgkIZmsXZ0gxBqrOSI1E3XPpkVSakyPYgMtWdtNKeBgStnPncgzpGqQzoG'
|
42 |
+
|
43 |
+
def apikey(api_key):
|
44 |
+
nixtla_client = NixtlaClient(
|
45 |
+
api_key = api_key
|
46 |
+
)
|
47 |
+
key_valid = nixtla_client.validate_api_key()
|
48 |
+
return key_valid
|
49 |
+
|
50 |
+
def forecasting(df):
|
51 |
+
## if key_valid == True:
|
52 |
+
## forecasting 3 months
|
53 |
+
timegpt_fcst_df = nixtla_client.forecast(df=df, h=3, freq='MS', time_col='Date', target_col='Adj Close')
|
54 |
+
# else:
|
55 |
+
# timegpt_fcst_df= 'API key is not Valid'
|
56 |
+
|
57 |
+
return timegpt_fcst_df, nixtla_client.plot(df, timegpt_fcst_df, time_col='Date', target_col='Adj Close')
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
## Anomaly Detection
|
62 |
+
|
63 |
+
def anomaly_detect(df):
|
64 |
+
timegpt_anomalies_df = nixtla_client.detect_anomalies(df, time_col='Date', target_col='Adj Close', freq='D', level = 95, date_features=True)
|
65 |
+
return nixtla_client.plot(df,
|
66 |
+
timegpt_anomalies_df,
|
67 |
+
time_col='Date',
|
68 |
+
target_col='Adj Close')
|
69 |
|
70 |
|
71 |
@app.get("/ticker/{ticker}")
|
72 |
def read_item(ticker: str):
|
73 |
+
data = get_data_from_yahoo(ticker)
|
74 |
+
key = apikey(api_key)
|
75 |
+
stock_forecast = forecasting(df)
|
76 |
+
result = stock_forecast.to_json()
|
77 |
+
return result
|
78 |
|
79 |
|