File size: 5,515 Bytes
1b9a2bc
10caf6a
 
 
 
b0b5df0
10caf6a
b0b5df0
 
10caf6a
b0b5df0
 
5c9676b
10caf6a
b0b5df0
5c9676b
4d2a56e
10caf6a
5c9676b
1b9a2bc
10caf6a
5c9676b
9f65c7c
10caf6a
5c9676b
ddde3e7
10caf6a
5c9676b
b0b5df0
 
5c9676b
b0b5df0
 
5c9676b
ddde3e7
10caf6a
5c9676b
b0b5df0
 
5c9676b
b0b5df0
 
5c9676b
b0b5df0
 
5c9676b
b0b5df0
 
5c9676b
b0b5df0
 
5c9676b
b0b5df0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c9676b
b0b5df0
 
5c9676b
b0b5df0
0ead373
b0b5df0
 
0ead373
 
b0b5df0
 
10caf6a
 
b0b5df0
10caf6a
 
b0b5df0
 
10caf6a
b0b5df0
 
 
 
 
10caf6a
b0b5df0
 
 
 
b90f6cc
b0b5df0
 
 
 
 
ddde3e7
b0b5df0
 
 
 
 
10caf6a
b0b5df0
 
 
 
0ead373
b0b5df0
 
 
 
0ead373
b0b5df0
 
 
1b9a2bc
5c9676b
b0b5df0
 
10caf6a
0ead373
 
10caf6a
 
0ead373
 
 
10caf6a
b0b5df0
bcf9a43
5c9676b
 
 
 
10caf6a
0005cf0
b0b5df0
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import gradio as gr
import pandas as pd
import numpy as np
from datetime import datetime
import plotly.graph_objects as go
import torch
from transformers import pipeline, TapasTokenizer, TapasForQuestionAnswering
import pmdarima as pm
from pmdarima import auto_arima

# Preprocessing functions (same as before)
def merge(B, C, A):
    pass  # Implement merge function here...

def merge_sort(dataframe):
    pass  # Implement merge_sort function here...

def drop(dataframe):
    pass  # Implement drop function here...

def date_format(dataframe):
    pass  # Implement date_format function here...

def group_to_three(dataframe):
    pass  # Implement group_to_three function here...

def series_to_df_exogenous(series):
    pass  # Implement series_to_df_exogenous function here...

def dates_df(dataframe):
    pass  # Implement dates_df function here...

def get_forecast_period(period):
    pass  # Implement get_forecast_period function here...

def train_test(dataframe, n):
    pass  # Implement train_test function here...

def test_fitting(dataframe, Exo, trainY):
    pass  # Implement test_fitting function here...

def forecast_accuracy(forecast, actual):
    pass  # Implement forecast_accuracy function here...

def sales_growth(dataframe, fittedValues):
    pass  # Implement sales_growth function here...

def merge_forecast_data(actual, predicted, future):
    pass  # Implement merge_forecast_data function here...

def interpret_mape(mape_score):
    pass  # Implement interpret_mape function here...

def load_tapas_model():
    model_name = "google/tapas-large-finetuned-wtq"
    tokenizer = TapasTokenizer.from_pretrained(model_name)
    model = TapasForQuestionAnswering.from_pretrained(model_name, local_files_only=False)
    pipe = pipeline("table-question-answering", model=model, tokenizer=tokenizer)
    return pipe

pipe = load_tapas_model()

def get_answer(table, query):
    answers = pipe(table=table, query=query)
    return answers

def convert_answer(answer):
    pass  # Implement convert_answer function here...

def get_converted_answer(table, query):
    pass  # Implement get_converted_answer function here...

# Gradio Interface with emojis and colors
def upload_and_forecast(uploaded_file, period):
    if uploaded_file is None:
        return "โš ๏ธ Please upload a file to proceed."

    # Load the data
    df = pd.read_csv(uploaded_file)
    df = drop(df)
    df = date_format(df)
    merge_sort(df)
    series = group_to_three(df)
    
    forecast_period = get_forecast_period(period)
    df = series_to_df_exogenous(series)
    
    # Train the model
    n_periods = round(len(df) * 0.2)
    train = train_test(df, n_periods)
    training_y, test_y, test_y_series, training_X, test_X, future_X = train
    train_test_model = test_fitting(df, training_X, training_y)
    
    fitted, confint = train_test_model.predict(X=test_X, n_periods=n_periods, return_conf_int=True)
    index_of_fc = test_y_series.index
    fitted_series = pd.Series(fitted)
    fitted_series.index = index_of_fc

    future_n_periods = forecast_period
    future_fitted, confint = train_test_model.predict(X=df.iloc[-future_n_periods:, 1:], n_periods=future_n_periods, return_conf_int=True, freq='3D')
    future_index_of_fc = pd.date_range(df['Sales'].index[-1], periods=future_n_periods, freq='3D')
    future_fitted_series = pd.Series(future_fitted)
    future_fitted_series.index = future_index_of_fc

    # Calculate sales growth
    future_sales_growth = sales_growth(df, future_fitted_series)
    
    # Prepare merged data for chart plotting
    merged_data = merge_forecast_data(df['Sales'], fitted_series, future_fitted_series)
    
    # Plot the charts
    fig_compare = go.Figure()
    fig_compare.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Actual Sales'], mode='lines', name='Actual Sales'))
    fig_compare.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Predicted Sales'], mode='lines', name='Predicted Sales', line=dict(color='#006400')))
    fig_compare.update_layout(title='๐Ÿ“Š Historical Sales Data', xaxis_title='Date', yaxis_title='Sales')

    fig_forecast = go.Figure()
    fig_forecast.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Actual Sales'], mode='lines', name='Actual Sales'))
    fig_forecast.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Forecasted Future Sales'], mode='lines', name='Future Forecasted Sales'))
    fig_forecast.update_layout(title='๐Ÿ”ฎ Forecasted Sales Data', xaxis_title='Date', yaxis_title='Sales')

    # Return the figures and growth data
    return fig_compare, fig_forecast, future_sales_growth

# Gradio Interface setup with external CSS file
iface = gr.Interface(
    fn=upload_and_forecast,
    inputs=[
        gr.File(label="๐Ÿ“‚ Upload your sales data (CSV)", elem_id="file-uploader"),
        gr.Slider(minimum=30, maximum=90, step=1, label="โณ Forecast Period (Days)", elem_id="forecast-period-slider")
    ],
    outputs=[
        gr.Plot(label="๐Ÿ“ˆ Historical vs Predicted Sales"),
        gr.Plot(label="๐Ÿ”ฎ Forecasted Sales Data"),
        gr.DataFrame(label="๐Ÿ“Š Sales Growth")
    ],
    live=True,
    theme="compact",
    title="Sales Forecasting System โœจ",
    description="Upload your sales data to start forecasting ๐Ÿš€",
    layout="horizontal",  # Add a horizontal layout for better alignment
    css=open("styles.css", "r").read()  # Load the external CSS file here
)

iface.launch()