File size: 2,805 Bytes
a66d894
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gradio as gr
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import LabelEncoder

# Sample dataset (you would 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'],
    'fit': ['regular', 'tight', 'loose', 'regular', 'tight'],  # Fit type (advanced feature)
    'style': ['casual', 'formal', 'casual', 'casual', 'formal']  # Style preference (advanced feature)
}

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

# Encode fit and style as well
fit_encoder = LabelEncoder()
data['fit_encoded'] = fit_encoder.fit_transform(data['fit'])

style_encoder = LabelEncoder()
data['style_encoded'] = style_encoder.fit_transform(data['style'])

# Prepare features and target
X = np.array([data['chest'], data['waist'], data['hip'], data['fit_encoded'], data['style_encoded']]).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, fit, and style preferences
def predict_size(chest, waist, hip, fit, style):
    # Encode the fit and style preferences
    fit_encoded = fit_encoder.transform([fit])[0]
    style_encoded = style_encoder.transform([style])[0]
    
    # Create the input feature array
    input_features = np.array([[chest, waist, hip, fit_encoded, style_encoded]])
    
    # Predict the size
    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)"),
        gr.Dropdown(choices=['regular', 'tight', 'loose'], label="Fit Type"),
        gr.Dropdown(choices=['casual', 'formal'], label="Style Preference")
    ],
    outputs="text",
    live=True,
    title="Advanced AI Size Advisor",
    description="Enter your body measurements and preferences to get an accurate clothing size recommendation. The model takes into account fit and style preferences."
)

# Launch the interface
interface.launch()