File size: 5,424 Bytes
1b9a2bc
10caf6a
 
 
 
b0b5df0
10caf6a
b0b5df0
 
10caf6a
b0b5df0
 
 
 
10caf6a
b0b5df0
 
 
4d2a56e
10caf6a
b0b5df0
 
1b9a2bc
10caf6a
b0b5df0
 
9f65c7c
10caf6a
b0b5df0
 
ddde3e7
10caf6a
b0b5df0
 
 
 
 
 
 
 
 
 
ddde3e7
10caf6a
b0b5df0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ead373
b0b5df0
 
0ead373
 
b0b5df0
 
10caf6a
 
b0b5df0
10caf6a
 
b0b5df0
 
10caf6a
b0b5df0
 
 
 
 
10caf6a
b0b5df0
 
 
 
b90f6cc
b0b5df0
 
 
 
 
ddde3e7
b0b5df0
 
 
 
 
10caf6a
b0b5df0
 
 
 
0ead373
b0b5df0
 
 
 
0ead373
b0b5df0
 
 
1b9a2bc
0ead373
b0b5df0
 
10caf6a
0ead373
 
10caf6a
 
0ead373
 
 
10caf6a
b0b5df0
bcf9a43
0ead373
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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):
    # Implement merge function here...
    pass

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

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

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

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

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

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

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

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

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

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

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

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

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

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):
    # Implement convert_answer function here...
    pass

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

# 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 improved layout and emojis
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 ๐Ÿš€"
)

iface.launch()