from datetime import datetime import numpy as np import pandas as pd from sklearn.ensemble import RandomForestRegressor import gradio as gr import os import plotly.graph_objects as go from huggingface_hub import from_pretrained_keras def predictPPM(df, split): ts= pd.read_csv('datappm.csv') df2 =ts.copy() ttSplit=split/100 ts['Date']=pd.to_datetime(ts['Date']) ts.rename(columns={'#PPM':'PPM'},inplace=True) ts=ts.set_index(['Date']) ts['months'] = [x.month for x in ts.index] ts['years'] = [x.year for x in ts.index] ts.reset_index(drop=True, inplace=True) # Split Data X=ts.drop("PPM",axis=1) Y= ts["PPM"] X_train=X[:int (len(Y)*ttSplit)] X_test=X[int(len(Y)*ttSplit):] Y_train=Y[:int (len(Y)*ttSplit)] Y_test=Y[int(len(Y)*ttSplit):] # fit the model rf = RandomForestRegressor() rf.fit(X_train, Y_train) df1=df2.set_index(['Date']) df1.rename(columns={'#PPM':'PPM'},inplace=True) train=df1.PPM[:int (len(ts.PPM)*ttSplit)] test=df1.PPM[int(len(ts.PPM)*ttSplit):] preds=rf.predict(X_test).astype(int) predictions=pd.DataFrame(preds,columns=['PPM']) predictions.index=test.index predictions.reset_index(inplace=True) predictions['Date']=pd.to_datetime(predictions['Date']) print(predictions) #combine all into one table ts_df=df.copy() ts_df.rename(columns={'#PPM':'PPM'},inplace=True) train= ts_df[:int (len(ts_df)*ttSplit)] test= ts_df[int(len(ts_df)*ttSplit):] df2['Date']=pd.to_datetime(df2['Date']) df2.rename(columns={'#PPM':'PPM'},inplace=True) df3= predictions df2['origin']='ground truth' df3['origin']='prediction' df4=pd.concat([df2, df3]) print(df4) return df4 demo = gr.Interface( fn =predictPPM, inputs = [ gr.Timeseries(label="Input for the timeseries", max_rows=1, interactive=False), gr.Slider(1, 100, value=75, step=1, label="Train test split percentage"), ], outputs= [gr.LinePlot(x='Date', y='PPM', color='origin')#gr.Timeseries(x='Month') ] ) demo.launch()