Spaces:
Build error
Build error
marziehben
commited on
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
from sklearn.ensemble import RandomForestRegressor
|
5 |
+
import gradio as gr
|
6 |
+
import os
|
7 |
+
import plotly.graph_objects as go
|
8 |
+
from huggingface_hub import from_pretrained_keras
|
9 |
+
def predictPPM(df, split):
|
10 |
+
ts= pd.read_csv('datappm.csv')
|
11 |
+
df2 =ts.copy()
|
12 |
+
ttSplit=split/100
|
13 |
+
ts['Date']=pd.to_datetime(ts['Date'])
|
14 |
+
ts.rename(columns={'#PPM':'PPM'},inplace=True)
|
15 |
+
ts=ts.set_index(['Date'])
|
16 |
+
ts['months'] = [x.month for x in ts.index]
|
17 |
+
ts['years'] = [x.year for x in ts.index]
|
18 |
+
ts.reset_index(drop=True, inplace=True)
|
19 |
+
|
20 |
+
# Split Data
|
21 |
+
X=ts.drop("PPM",axis=1)
|
22 |
+
Y= ts["PPM"]
|
23 |
+
X_train=X[:int (len(Y)*ttSplit)]
|
24 |
+
X_test=X[int(len(Y)*ttSplit):]
|
25 |
+
Y_train=Y[:int (len(Y)*ttSplit)]
|
26 |
+
Y_test=Y[int(len(Y)*ttSplit):]
|
27 |
+
|
28 |
+
# fit the model
|
29 |
+
rf = RandomForestRegressor()
|
30 |
+
rf.fit(X_train, Y_train)
|
31 |
+
|
32 |
+
df1=df2.set_index(['Date'])
|
33 |
+
df1.rename(columns={'#PPM':'PPM'},inplace=True)
|
34 |
+
train=df1.PPM[:int (len(ts.PPM)*ttSplit)]
|
35 |
+
test=df1.PPM[int(len(ts.PPM)*ttSplit):]
|
36 |
+
preds=rf.predict(X_test).astype(int)
|
37 |
+
predictions=pd.DataFrame(preds,columns=['PPM'])
|
38 |
+
predictions.index=test.index
|
39 |
+
predictions.reset_index(inplace=True)
|
40 |
+
predictions['Date']=pd.to_datetime(predictions['Date'])
|
41 |
+
print(predictions)
|
42 |
+
|
43 |
+
#combine all into one table
|
44 |
+
ts_df=df.copy()
|
45 |
+
ts_df.rename(columns={'#PPM':'PPM'},inplace=True)
|
46 |
+
train= ts_df[:int (len(ts_df)*ttSplit)]
|
47 |
+
test= ts_df[int(len(ts_df)*ttSplit):]
|
48 |
+
|
49 |
+
df2['Date']=pd.to_datetime(df2['Date'])
|
50 |
+
df2.rename(columns={'#PPM':'PPM'},inplace=True)
|
51 |
+
df3= predictions
|
52 |
+
df2['origin']='ground truth'
|
53 |
+
df3['origin']='prediction'
|
54 |
+
df4=pd.concat([df2, df3])
|
55 |
+
print(df4)
|
56 |
+
return df4
|
57 |
+
|
58 |
+
demo = gr.Interface(
|
59 |
+
fn =predictPPM,
|
60 |
+
inputs = [
|
61 |
+
gr.Timeseries(label="Input for the timeseries", max_rows=1, interactive=False),
|
62 |
+
gr.Slider(1, 100, value=75, step=1, label="Train test split percentage"),
|
63 |
+
],
|
64 |
+
outputs= [gr.LinePlot(x='Date', y='PPM', color='origin')#gr.Timeseries(x='Month')
|
65 |
+
|
66 |
+
]
|
67 |
+
)
|
68 |
+
demo.launch()
|