azulgarza's picture
feat: add chatgpt app
d54d2f1
import pandas as pd
import streamlit as st
from src.utils import ChatGPTForecast
DATASETS = {
"Demand (AirPassengers)": "https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv",
#"Electriciy (ERCOT, multiple markets)": "https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/ercot_multiple_ts.csv",
"Web Traffic (Peyton Manning)": "https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/peyton_manning.csv",
"Finance (Exchange USD-EUR)": "https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/usdeur.csv",
"Electricity (Ercot COAST)": "https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/ercot_COAST.csv",
}
gpt_forecast = ChatGPTForecast()
def st_chatgpt_forecast():
st.set_page_config(
page_title="ChatGPT Forecast",
page_icon="๐Ÿ”ฎ",
layout="wide",
initial_sidebar_state="expanded",
)
st.title(
"ChatGPT Forecast: Revolutionizing Time Series by Nixtla"
)
st.write(
"<style>div.block-container{padding-top:2rem;}</style>", unsafe_allow_html=True
)
intro = """
This application is designed to analyze time series forecasting tasks by leveraging the power of OpenAI's ChatGPT.
Here's how it works:
1. **Upload Your Data**: You can upload your own time series data which will be used to generate forecasts.
2. **Forecast with GPT**: Our application utilizes the advanced language model, ChatGPT, to generate time series forecasts. ChatGPT has been trained on a diverse range of internet text, but it also has the unique ability to generate numerical sequences, making it a fascinating tool for time series forecasting.
3. **Compare with Naive Forecast**: We provide a simple naive forecast as a benchmark for comparison. This forecast is based on the simple assumption that future values will be the same as the most recent observed value.
By comparing the GPT-based forecast against the naive model, you can gain insights into the capabilities and potential advantages of using advanced AI models for time series prediction.
Please note that this application is meant for experimental purposes and the forecasts generated by the AI should not be used for making real-world decisions without proper consideration and additional checks.
"""
st.write(intro)
required_cols = ["ds", "y"]
with st.sidebar.expander("Dataset", expanded=True):
data_selection = st.selectbox("Select example dataset", DATASETS.keys())
data_url = DATASETS[data_selection]
url_json = st.text_input("Data (you can pass your own url here)", data_url)
st.write(
"You can also upload a CSV file like [this one](https://github.com/Nixtla/transfer-learning-time-series/blob/main/datasets/air_passengers.csv)."
)
uploaded_file = st.file_uploader("Upload CSV")
with st.form("Data"):
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
cols = df.columns
timestamp_col = st.selectbox("Timestamp column", options=cols)
value_col = st.selectbox("Value column", options=cols)
else:
timestamp_col = st.text_input("Timestamp column", value="timestamp")
value_col = st.text_input("Value column", value="value")
st.write("You must press Submit each time you want to forecast.")
submitted = st.form_submit_button("Submit")
if submitted:
if uploaded_file is None:
st.write("Please provide a dataframe.")
if url_json.endswith("json"):
df = pd.read_json(url_json)
else:
df = pd.read_csv(url_json)
df = df.rename(
columns=dict(zip([timestamp_col, value_col], required_cols))
)
else:
# df = pd.read_csv(uploaded_file)
df = df.rename(
columns=dict(zip([timestamp_col, value_col], required_cols))
)
else:
if url_json.endswith("json"):
df = pd.read_json(url_json)
else:
df = pd.read_csv(url_json)
cols = df.columns
if "unique_id" in cols:
cols = cols[-2:]
df = df.rename(columns=dict(zip(cols, required_cols)))
if "unique_id" not in df:
df.insert(0, "unique_id", "ts_0")
df["ds"] = pd.to_datetime(df["ds"])
df = df.sort_values(["unique_id", "ds"])
with st.sidebar:
horizon = st.number_input("Forecasting horizon to predict:", value=24)
input_size = st.number_input("Number of values to make inference:", value=12)
st.header("Forecasts generated by ChatGPT against a Naive model")
fig = gpt_forecast.forecast(df, horizon, input_size)
fig.update_layout(height=400)
st.plotly_chart(
fig,
use_container_width=True,
)
if __name__ == "__main__":
st_chatgpt_forecast()