Spaces:
Running
Running
# Install necessary packages (only for local environment) | |
# !pip install pandas granite-tsfm gradio | |
import pandas as pd | |
import gradio as gr | |
from granite_tsfm import TimeSeriesPreprocessor, TinyTimeMixerForPrediction, TimeSeriesForecastingPipeline | |
# Function to load model and make predictions | |
def forecast(csv_file): | |
data = pd.read_csv(csv_file.name, parse_dates=['timestamp_column']) | |
tsp = TimeSeriesPreprocessor( | |
id_columns=[], | |
timestamp_column='timestamp_column', | |
target_columns=['value1', 'value2'], # Replace with your target column names | |
prediction_length=96, | |
context_length=512, | |
scaling=True | |
) | |
processed_data = tsp.fit_transform(data) | |
model = TinyTimeMixerForPrediction.from_pretrained( | |
'ibm-granite/granite-timeseries-ttm-r2', | |
num_input_channels=tsp.num_input_channels | |
) | |
pipeline = TimeSeriesForecastingPipeline( | |
model=model, | |
feature_extractor=tsp | |
) | |
forecasts = pipeline(data) | |
return forecasts.to_csv(index=False) | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=forecast, | |
inputs=gr.File(label="Upload CSV File"), | |
outputs=gr.File(label="Download Forecasts"), | |
title="Time Series Forecasting with Granite-TimeSeries-TTM-R2", | |
description="Upload a CSV file with a timestamp column to generate forecasts." | |
) | |
iface.launch() | |