Spaces:
Sleeping
Sleeping
| 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) | |