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()