File size: 1,890 Bytes
99cdc31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
import gradio as gr
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import LabelEncoder

# Sample dataset (body measurements and corresponding size)
# You will replace this with your actual dataset
data = {
    'chest': [34, 36, 38, 40, 42],
    'waist': [28, 30, 32, 34, 36],
    'hip': [36, 38, 40, 42, 44],
    'size': ['S', 'M', 'L', 'XL', 'XXL']
}

# Train a LabelEncoder for sizes
label_encoder = LabelEncoder()
data['size_encoded'] = label_encoder.fit_transform(data['size'])

# Prepare features and target
X = np.array([data['chest'], data['waist'], data['hip']]).T
y = data['size_encoded']  # Using encoded sizes as the target

# Initialize and train the model
model = LinearRegression()
model.fit(X, y)

# Function to predict size based on measurements
def predict_size(chest, waist, hip):
    input_features = np.array([[chest, waist, hip]])
    predicted_size_encoded = model.predict(input_features)

    # Clamp the predicted size to ensure it's within the valid range of labels
    predicted_size_encoded_clamped = np.clip(predicted_size_encoded, 0, len(label_encoder.classes_) - 1)

    # Convert the numeric prediction back to the original size
    predicted_size = label_encoder.inverse_transform(predicted_size_encoded_clamped.astype(int))

    return predicted_size[0]

# Create the Gradio interface
interface = gr.Interface(
    fn=predict_size, 
    inputs=[
        gr.Slider(minimum=30, maximum=50, step=1, label="Chest (inches)"),
        gr.Slider(minimum=20, maximum=40, step=1, label="Waist (inches)"),
        gr.Slider(minimum=30, maximum=50, step=1, label="Hip (inches)")
    ],
    outputs="text",
    live=True,
    title="AI Size Advisor",
    description="Enter your body measurements to get an accurate clothing size recommendation based on past purchase data."
)

# Launch the interface
interface.launch()