Ippo987's picture
Upload 2 files
72bdd36 verified
raw
history blame
6.88 kB
import gradio as gr
import numpy as np
import joblib
from sklearn.preprocessing import StandardScaler
model = joblib.load("final_rf_model.pkl")
scaler = StandardScaler()
class_labels = {
0: 'Insufficient Weight',
1: 'Normal Weight',
2: 'Overweight Level I',
3: 'Overweight Level II',
4: 'Obesity Type I',
5: 'Obesity Type II',
6: 'Obesity Type III'
}
# Function to make predictions
def predict_obesity(weight, height, age, fcvc, gender, ncp, ch2o, faf, tue, fhwow,
caec_sometimes, calc_no, calc_sometimes, caec_frequently,
alcohol_choice, mtrans_choice, favc):
# Prepare input data for prediction
input_data = np.array([[weight, height, age, fcvc, 1 if gender == "Male" else 0, ncp, ch2o, faf, tue, fhwow,
1 if caec_sometimes else 0, 1 if calc_no else 0, 1 if calc_sometimes else 0,
1 if caec_frequently else 0, 1 if alcohol_choice == "Yes" else 0,
1 if favc else 0, 1 if mtrans_choice == "Automobile" else 0]])
# Scale the appropriate input values
input_data[:, 0:4] = scaler.fit_transform(input_data[:, 0:4])
input_data[:, 5:10] = scaler.fit_transform(input_data[:, 5:10])
# Make prediction
prediction = model.predict(input_data)
# Map the numeric prediction to the corresponding label
predicted_label = class_labels.get(prediction[0], "Unknown Class")
return predicted_label
# Custom CSS for better styling
custom_css = """
<style>
.gradio-container {
background-color: #0a0a2c;
background-image:
linear-gradient(45deg, #0a0a2c 25%, #12124a 25%, #12124a 50%, #0a0a2c 50%, #0a0a2c 75%, #12124a 75%, #12124a 100%);
background-size: 56.57px 56.57px;
border-radius: 15px;
padding: 30px;
box-shadow: 0 0 20px rgba(66, 220, 219, 0.3),
0 0 40px rgba(233, 30, 99, 0.2);
border: 1px solid rgba(66, 220, 219, 0.3);
}
.title {
font-family: 'Orbitron', sans-serif;
font-size: 36px;
font-weight: bold;
color: #00fff2;
text-align: center;
margin-bottom: 30px;
text-transform: uppercase;
letter-spacing: 3px;
text-shadow: 0 0 10px rgba(0, 255, 242, 0.5),
0 0 20px rgba(0, 255, 242, 0.3),
0 0 30px rgba(0, 255, 242, 0.1);
}
.description {
font-family: 'Rajdhani', sans-serif;
font-size: 18px;
color: #b4f8fc;
text-align: center;
margin-bottom: 30px;
line-height: 1.6;
text-shadow: 0 0 5px rgba(180, 248, 252, 0.3);
}
/* Input fields styling */
input[type="number"] {
background-color: rgba(16, 16, 44, 0.9);
border: 2px solid #00fff2;
border-radius: 8px;
padding: 12px;
color: #fff;
font-family: 'Rajdhani', sans-serif;
transition: all 0.3s ease;
box-shadow: 0 0 10px rgba(0, 255, 242, 0.2);
}
input[type="number"]:focus {
border-color: #ff2e63;
box-shadow: 0 0 15px rgba(255, 46, 99, 0.4);
outline: none;
}
/* Radio and Checkbox styling */
input[type="radio"],
input[type="checkbox"] {
accent-color: #ff2e63;
}
.input-container label {
color: #b4f8fc;
font-family: 'Rajdhani', sans-serif;
font-size: 16px;
margin-bottom: 8px;
display: block;
}
/* Button styling */
button {
background: linear-gradient(45deg, #ff2e63, #00fff2);
color: #fff;
border: none;
padding: 15px 30px;
border-radius: 8px;
cursor: pointer;
font-family: 'Orbitron', sans-serif;
font-size: 18px;
text-transform: uppercase;
letter-spacing: 2px;
transition: all 0.3s ease;
box-shadow: 0 0 15px rgba(255, 46, 99, 0.3),
0 0 30px rgba(0, 255, 242, 0.2);
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 0 20px rgba(255, 46, 99, 0.5),
0 0 40px rgba(0, 255, 242, 0.3);
}
/* Output label styling */
.output-label {
background: rgba(16, 16, 44, 0.9);
border: 2px solid #ff2e63;
border-radius: 8px;
padding: 20px;
color: #00fff2;
font-family: 'Orbitron', sans-serif;
font-size: 24px;
text-align: center;
margin-top: 20px;
box-shadow: 0 0 15px rgba(255, 46, 99, 0.3);
}
/* Add cyberpunk grid lines to the background */
.gradio-container::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background:
linear-gradient(90deg, rgba(66, 220, 219, 0.1) 1px, transparent 1px),
linear-gradient(0deg, rgba(66, 220, 219, 0.1) 1px, transparent 1px);
background-size: 20px 20px;
pointer-events: none;
}
/* Add some hover effects to input containers */
.input-container:hover {
transform: translateX(5px);
transition: transform 0.3s ease;
}
/* Scrollbar styling */
::-webkit-scrollbar {
width: 10px;
background: #0a0a2c;
}
::-webkit-scrollbar-thumb {
background: linear-gradient(45deg, #ff2e63, #00fff2);
border-radius: 5px;
}
</style>
"""
# Gradio interface
iface = gr.Interface(
fn=predict_obesity,
inputs=[
gr.Number(label="Weight (40-160 kg)"),
gr.Number(label="Height (1-2 m)"),
gr.Number(label="Age (10-60 years)"),
gr.Number(label="FCVC (Frequency of Vegetable Consumption 1-4)"),
gr.Radio(choices=["Male", "Female"], label="Gender"),
gr.Number(label="NCP (Number of meals per day 1-3)"),
gr.Number(label="CH2O (Water Consumption 1-3)"),
gr.Number(label="FAF (Physical Activity Frequency 1-4)"),
gr.Number(label="TUE (Time Spent on Exercise 1-4)"),
gr.Number(label="FHWOW (Family History with OverWeight)"),
gr.Radio(choices=["No", "Sometimes", "Frequently"], label="Alcohol Consumption"),
gr.Radio(choices=["Public Transportation", "Automobile"], label="Transportation Method"),
gr.Checkbox(label="FAVC (Frequent Consumption of High-Calorie Foods)"),
],
outputs=gr.Label(label="Predicted Obesity Level"),
title="Obesity Level Estimator",
description="Enter the features related to eating habits and physical condition to estimate obesity levels.",
css=custom_css
)
# Launch the interface
iface.launch(share=True)