Mohssinibra commited on
Commit
aba50e6
·
verified ·
1 Parent(s): e1f82ea
Files changed (1) hide show
  1. app.py +24 -25
app.py CHANGED
@@ -1,43 +1,42 @@
1
  # Install necessary packages (only for local environment)
2
- # !pip install pandas granite-tsfm gradio
3
 
4
  import pandas as pd
5
  import gradio as gr
6
- from granite-tsfm import TimeSeriesPreprocessor, TinyTimeMixerForPrediction, TimeSeriesForecastingPipeline
 
 
 
 
 
 
 
 
 
 
7
 
8
- # Function to load model and make predictions
9
  def forecast(csv_file):
10
  data = pd.read_csv(csv_file.name, parse_dates=['timestamp_column'])
11
 
12
- tsp = TimeSeriesPreprocessor(
13
- id_columns=[],
14
- timestamp_column='timestamp_column',
15
- target_columns=['value1', 'value2'], # Replace with your target column names
16
- prediction_length=96,
17
- context_length=512,
18
- scaling=True
19
- )
20
- processed_data = tsp.fit_transform(data)
21
 
22
- model = TinyTimeMixerForPrediction.from_pretrained(
23
- 'ibm-granite/granite-timeseries-ttm-r2',
24
- num_input_channels=tsp.num_input_channels
25
- )
26
 
27
- pipeline = TimeSeriesForecastingPipeline(
28
- model=model,
29
- feature_extractor=tsp
30
- )
31
- forecasts = pipeline(data)
32
- return forecasts.to_csv(index=False)
33
 
34
- # Create Gradio interface
35
  iface = gr.Interface(
36
  fn=forecast,
37
  inputs=gr.File(label="Upload CSV File"),
38
  outputs=gr.File(label="Download Forecasts"),
39
- title="Time Series Forecasting with Granite-TimeSeries-TTM-R2",
40
- description="Upload a CSV file with a timestamp column to generate forecasts."
41
  )
42
 
43
  iface.launch()
 
1
  # Install necessary packages (only for local environment)
2
+ # !pip install pandas gradio transformers torch
3
 
4
  import pandas as pd
5
  import gradio as gr
6
+ import torch
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer
8
+
9
+ # Load the Hugging Face forecasting model
10
+ def load_model():
11
+ model_name = "Ankur87/Llama2_Time_series_forecasting_7.0" # Using the specified model
12
+ model = AutoModelForCausalLM.from_pretrained(model_name)
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
14
+ return model, tokenizer
15
+
16
+ model, tokenizer = load_model()
17
 
 
18
  def forecast(csv_file):
19
  data = pd.read_csv(csv_file.name, parse_dates=['timestamp_column'])
20
 
21
+ # Convert data to a format suitable for the model
22
+ input_text = data.to_json()
23
+ inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
 
 
 
 
 
 
24
 
25
+ with torch.no_grad():
26
+ predictions = model.generate(**inputs, max_length=200)
 
 
27
 
28
+ forecast_text = tokenizer.decode(predictions[0], skip_special_tokens=True)
29
+ forecasts = pd.DataFrame({'forecast': [forecast_text]})
30
+ forecasts.to_csv("forecasts.csv", index=False)
31
+ return "forecasts.csv"
 
 
32
 
33
+ # Gradio Interface
34
  iface = gr.Interface(
35
  fn=forecast,
36
  inputs=gr.File(label="Upload CSV File"),
37
  outputs=gr.File(label="Download Forecasts"),
38
+ title="Time Series Forecasting with Llama2",
39
+ description="Upload a CSV file with a timestamp column to generate forecasts using Llama2."
40
  )
41
 
42
  iface.launch()