IvanStudent's picture
Guardar mis cambios locales
0ead373
raw
history blame
5.42 kB
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()