Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import warnings
|
| 3 |
+
import pandas as pd;
|
| 4 |
+
import numpy as np;
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import seaborn as sns
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
from sklearn.metrics import mean_absolute_error, mean_squared_error
|
| 9 |
+
from statsmodels.tsa.statespace.sarimax import SARIMAX
|
| 10 |
+
from statsmodels.tsa.holtwinters import ExponentialSmoothing
|
| 11 |
+
|
| 12 |
+
from openpyxl import Workbook
|
| 13 |
+
from openpyxl.drawing.image import Image
|
| 14 |
+
from pandas import DataFrame
|
| 15 |
+
|
| 16 |
+
from datetime import datetime
|
| 17 |
+
|
| 18 |
+
def forecast(name,duration):
|
| 19 |
+
df = pd.read_csv(name)
|
| 20 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
| 21 |
+
df.set_index('Date', inplace=True)
|
| 22 |
+
model = ExponentialSmoothing(df, trend='add', seasonal='add', seasonal_periods=48)
|
| 23 |
+
model_fit = model.fit()
|
| 24 |
+
freq = 'W'
|
| 25 |
+
steps = 7
|
| 26 |
+
if duration == 'Month':
|
| 27 |
+
steps = 30
|
| 28 |
+
freq = 'M'
|
| 29 |
+
|
| 30 |
+
forecast = model_fit.forecast(steps=steps)
|
| 31 |
+
last_date = df.index[-1]
|
| 32 |
+
future_dates = pd.date_range(start=last_date + pd.DateOffset(months=1), periods=steps, freq=freq)
|
| 33 |
+
forecast_df = pd.DataFrame({
|
| 34 |
+
'Date': future_dates,
|
| 35 |
+
'No. of Vehicles': forecast
|
| 36 |
+
})
|
| 37 |
+
return forecast_df
|
| 38 |
+
|
| 39 |
+
warnings.filterwarnings('ignore')
|
| 40 |
+
def predict(operation,file):
|
| 41 |
+
if file == None:
|
| 42 |
+
return None,"No file inputted"
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
algos = dict()
|
| 46 |
+
predicted = forecast(file.name,operation)
|
| 47 |
+
algos['SARIMA'] = predicted
|
| 48 |
+
algos['Exponential Smoothing'] = predicted
|
| 49 |
+
algos['XGBoost'] = predicted
|
| 50 |
+
|
| 51 |
+
# output_name = "Forecasted.csv"
|
| 52 |
+
# predicted.to_csv(output_name,index=False)
|
| 53 |
+
c = 0
|
| 54 |
+
workbook = Workbook()
|
| 55 |
+
# Remove the default sheet
|
| 56 |
+
default_sheet = workbook.active
|
| 57 |
+
workbook.remove(default_sheet)
|
| 58 |
+
|
| 59 |
+
for aname in algos:
|
| 60 |
+
plt.figure(figsize=(10, 5))
|
| 61 |
+
plt.plot(algos[aname]['Date'], algos[aname]['No. of Vehicles'])
|
| 62 |
+
plt.xlabel('Date')
|
| 63 |
+
plt.ylabel('No. of Vehicles')
|
| 64 |
+
plt.title('Vehicle Count over Time')
|
| 65 |
+
# Save the plot as an image file (e.g., PNG)
|
| 66 |
+
plot_filename = 'line_plot.png'
|
| 67 |
+
plt.savefig(plot_filename)
|
| 68 |
+
# Create a new sheet for the current DataFrame
|
| 69 |
+
worksheet = workbook.create_sheet(title=aname)
|
| 70 |
+
# Write the DataFrame to the Excel file
|
| 71 |
+
for index, row in algos[aname].iterrows():
|
| 72 |
+
worksheet.append(row.tolist())
|
| 73 |
+
# Insert the plot image into the Excel file
|
| 74 |
+
img = Image(plot_filename)
|
| 75 |
+
worksheet.add_image(img, 'D1')
|
| 76 |
+
# Save the Excel file
|
| 77 |
+
now = datetime.now()
|
| 78 |
+
formatted_datetime = now.strftime("%H:%M %d-%m-%Y")
|
| 79 |
+
output_filename = 'forecast' + formatted_datetime + '.xlsx'
|
| 80 |
+
workbook.save(output_filename)
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
return None,str(e)
|
| 84 |
+
|
| 85 |
+
return output_filename,"Successfully predicted "+operation+" ahead"
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
iface = gr.Interface(fn=predict,
|
| 89 |
+
|
| 90 |
+
inputs=[gr.Radio(label='Predict ahead:',choices=['Month','Week'],value='Month'),gr.File(label="CSV file")],
|
| 91 |
+
|
| 92 |
+
outputs=[gr.File(label="CSV file"),gr.Textbox(label='Log',interactive=False)])
|
| 93 |
+
iface.launch()
|