File size: 2,011 Bytes
ddb698f
 
 
 
 
 
 
 
edfa9ff
ddb698f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fef31c
ddb698f
 
 
 
 
8fef31c
 
ddb698f
 
 
 
8fef31c
 
ddb698f
 
8fef31c
ddb698f
8fef31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
pd.options.plotting.backend = "plotly"
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
    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']='status '
    df3['origin']='prediction'
    df4=pd.concat([df2, df3])
    print(df4)
    return df4
demo = gr.Interface(
    fn =predictPPM,inputs = [gr.UploadButton(label="Input data for PPM TimeSeries"),
        gr.Slider(1, 100, value=75, step=1, label="Train test split percentage"),
    ],
    outputs=gr.LinePlot(x='Date', y='PPM', color='origin')
)
demo.launch(debug=True)