File size: 1,418 Bytes
2b07811
 
 
 
 
1c5c1ab
2b07811
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0e9888
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
import gradio as gr
import pandas as pd
import joblib

# Load your model
model = joblib.load('best_gradient_boosting_model_v2.pkl')

# Define the prediction function
def predict_tip(total_bill, sex, smoker, day, time, size):
    # Encode like in training
    data = pd.DataFrame({
        'total_bill': [total_bill],
        'sex': [1 if sex == 'Male' else 0],
        'smoker': [1 if smoker == 'Yes' else 0],
        'day': [day],
        'time': [time],
        'size': [size]
    })
    data = pd.get_dummies(data)
    
    # Handle any missing columns (to match training)
    expected_cols = model.feature_names_in_
    for col in expected_cols:
        if col not in data.columns:
            data[col] = 0
    data = data[expected_cols]
    
    pred = model.predict(data)[0]
    return f"💰 Predicted Tip: ${pred:.2f}"

# Build Gradio interface
app = gr.Interface(
    fn=predict_tip,
    inputs=[
        gr.Number(label="Total Bill ($)"),
        gr.Radio(["Male", "Female"], label="Customer Gender"),
        gr.Radio(["Yes", "No"], label="Smoker"),
        gr.Radio(["Thur", "Fri", "Sat", "Sun"], label="Day of Week"),
        gr.Radio(["Lunch", "Dinner"], label="Meal Time"),
        gr.Slider(1, 10, step=1, label="Group Size")
    ],
    outputs="text",
    title="🍽️ Restaurant Tip Prediction App",
    description="Predict tip amount based on restaurant bill details."
)

app.launch(share=True)