File size: 1,772 Bytes
b0507ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
import os
from huggingface_hub import hf_hub_download
import joblib  # Import joblib directly
import pandas as pd

# Load the pre-trained model from Hugging Face
hf_token = os.getenv("HF_TOKEN")
model_path = hf_hub_download(repo_id="wvsu-dti-aidev-team/advertising_knn_regressor_model", filename="decision_tree_regressor.pkl", use_auth_token=hf_token)
model = joblib.load(model_path)

def predict_sales(tv, radio, newspaper):
    # Create a DataFrame with the same feature names as the training data
    input_data = pd.DataFrame([[tv, radio, newspaper]], columns=['TV', 'Radio', 'Newspaper'])
    
    # Get the predicted sales
    predicted_sales = model.predict(input_data)
    
    # Discussion on the projected sales result
    discussion = f"Based on the advertising spending, the projected sales are approximately {predicted_sales[0] * 10000:.2f} Pesos. " \
                 f"Investing more in TV advertising tends to have a significant impact on sales, followed by Radio and Newspaper. " \
                 f"Optimizing the budget allocation across these channels can help maximize sales."

    return predicted_sales[0], discussion

# Create the Gradio interface
iface = gr.Interface(
    fn=predict_sales,
    inputs=[
        gr.Number(label="TV Advertising Spend (x 10,000 Pesos)"),
        gr.Number(label="Radio Advertising Spend (x 10,000 Pesos)"),
        gr.Number(label="Newspaper Advertising Spend (x 10,000 Pesos)")
    ],
    outputs=[
        gr.Textbox(label="Predicted Sales"),
        gr.Textbox(label="Discussion")
    ],
    title="Advertising Spend to Sales Prediction",
    description="Enter the advertising spending on TV, Radio, and Newspaper to predict the sales."
)

# Launch the app
iface.launch()