Mohammad Javad Darvishi
commited on
Commit
·
686c1e1
1
Parent(s):
32d870a
'first working example of the app'
Browse files- app.py +76 -2
- requirements.txt +3 -0
app.py
CHANGED
|
@@ -1,5 +1,79 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import torch
|
| 4 |
+
from chronos import ChronosPipeline
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import numpy as np
|
| 7 |
|
| 8 |
+
# Load the Chronos Pipeline model
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_pipeline():
|
| 11 |
+
pipeline = ChronosPipeline.from_pretrained(
|
| 12 |
+
"amazon/chronos-t5-small",
|
| 13 |
+
device_map="cpu", # Change to CPU
|
| 14 |
+
torch_dtype=torch.float32, # Use float32 for CPU
|
| 15 |
+
)
|
| 16 |
+
return pipeline
|
| 17 |
|
| 18 |
+
pipeline = load_pipeline()
|
| 19 |
+
|
| 20 |
+
# Streamlit app interface
|
| 21 |
+
st.title("Time Series Forecasting Demo with Deep Learning models")
|
| 22 |
+
st.write("This demo uses the ChronosPipeline model for time series forecasting.")
|
| 23 |
+
|
| 24 |
+
# Default time series data (comma-separated)
|
| 25 |
+
default_data = """
|
| 26 |
+
112, 118, 132, 129, 121, 135, 148, 148, 136, 119, 104, 118, 115, 126, 141, 135, 125, 149, 170, 170, 158,
|
| 27 |
+
133, 114, 140, 145, 150, 178, 163, 172, 178, 199, 199, 184, 162, 146, 166, 171, 180, 193, 181, 183, 218,
|
| 28 |
+
230, 242, 209, 191, 172, 194, 196, 196, 236, 235, 229, 243, 264, 272, 237, 211, 180, 201, 204, 188, 235,
|
| 29 |
+
227, 234, 264, 302, 293, 259, 229, 203, 229, 242, 233, 267, 269, 270, 315, 364, 347, 312, 274, 237, 278,
|
| 30 |
+
284, 277, 317, 313, 318, 374, 413, 405, 355, 306, 271, 306, 315, 301, 356, 348, 355, 422, 465, 467, 404,
|
| 31 |
+
347, 305, 336, 340, 318, 362, 348, 363, 435, 491, 505, 404, 359, 310, 337, 360, 342, 406, 396, 420, 472,
|
| 32 |
+
548, 559, 463, 407, 362, 405, 417, 391, 419, 461, 472, 535, 622, 606, 508, 461, 390, 432
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
# Input field for user-provided data
|
| 36 |
+
user_input = st.text_area(
|
| 37 |
+
"Enter time series data (comma-separated values):",
|
| 38 |
+
default_data.strip()
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Convert user input into a list of numbers
|
| 42 |
+
def process_input(input_str):
|
| 43 |
+
return [float(x.strip()) for x in input_str.split(",")]
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
time_series_data = process_input(user_input)
|
| 47 |
+
except ValueError:
|
| 48 |
+
st.error("Please make sure all values are numbers, separated by commas.")
|
| 49 |
+
time_series_data = [] # Set empty data on error to prevent further processing
|
| 50 |
+
|
| 51 |
+
# Select the number of months for forecasting
|
| 52 |
+
prediction_length = st.slider("Select Forecast Horizon (Months)", min_value=1, max_value=64, value=12)
|
| 53 |
+
|
| 54 |
+
# If data is valid, perform the forecast
|
| 55 |
+
if time_series_data:
|
| 56 |
+
# Convert the data to a tensor
|
| 57 |
+
context = torch.tensor(time_series_data, dtype=torch.float32)
|
| 58 |
+
|
| 59 |
+
# Make the forecast
|
| 60 |
+
forecast = pipeline.predict(
|
| 61 |
+
context=context,
|
| 62 |
+
prediction_length=prediction_length,
|
| 63 |
+
num_samples=20,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Prepare forecast data for plotting
|
| 67 |
+
forecast_index = range(len(time_series_data), len(time_series_data) + prediction_length)
|
| 68 |
+
low, median, high = np.quantile(forecast[0].numpy(), [0.1, 0.5, 0.9], axis=0)
|
| 69 |
+
|
| 70 |
+
# Plot the historical and forecasted data
|
| 71 |
+
plt.figure(figsize=(8, 4))
|
| 72 |
+
plt.plot(time_series_data, color="royalblue", label="Historical data")
|
| 73 |
+
plt.plot(forecast_index, median, color="tomato", label="Median forecast")
|
| 74 |
+
plt.fill_between(forecast_index, low, high, color="tomato", alpha=0.3, label="80% prediction interval")
|
| 75 |
+
plt.legend()
|
| 76 |
+
plt.grid()
|
| 77 |
+
|
| 78 |
+
# Show the plot in the Streamlit app
|
| 79 |
+
st.pyplot(plt)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
transformers
|
| 3 |
+
torch
|