OfirMatzlawi's picture
Update main.py
10249b3 verified
from fastapi import FastAPI
import requests
# from telegram import ChatAction
import os
from urllib.request import urlopen, Request
from bs4 import BeautifulSoup
import pandas as pd
import json # for graph plotting in website
import datetime
import yfinance as yf
from pandas_datareader import data as pdr
from nixtlats import TimeGPT
from nixtlats import NixtlaClient
app = FastAPI()
@app.get("/")
def read_root():
return {
"message": "Hello, Please type a ticker at the end of the URL to get the stock sentiment."
}
## TimeGPT
def get_data_from_yahoo(ticker: str):
yf.pdr_override()
data = pdr.get_data_yahoo(ticker, start="2019-01-01", end=None)
df = pd.DataFrame(data).reset_index()
return df
def forecasting(df):
api_key = 'nixt-7nixiWJtV1TwD3vp9K8WqLrgkIZmsXZ0gxBqrOSI1E3XPpkVSakyPYgMtWdtNKeBgStnPncgzpGqQzoG'
nixtla_client = NixtlaClient(api_key = api_key)
## forecasting 3 months
timegpt_fcst_df = nixtla_client.forecast(df=df, h=3, freq='MS', finetune_steps=10, time_col='Date', target_col='Adj Close')
return timegpt_fcst_df
@app.get("/ticker/{ticker}")
def read_item(ticker: str):
data = get_data_from_yahoo(ticker)
pred = forecasting(data)
result = pred.to_json(orient="split")
return result