AI_Size_Advisor / AI_Size_Advisor.py
Thiwanka01's picture
Rename AI Size Advisor.py to AI_Size_Advisor.py
7b3f91e verified
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()