Thiwanka01 commited on
Commit
99cdc31
·
verified ·
1 Parent(s): 8d467e2

Create AI Size Advisor.py

Browse files
Files changed (1) hide show
  1. AI Size Advisor.py +55 -0
AI Size Advisor.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from sklearn.linear_model import LinearRegression
4
+ from sklearn.preprocessing import LabelEncoder
5
+
6
+ # Sample dataset (body measurements and corresponding size)
7
+ # You will replace this with your actual dataset
8
+ data = {
9
+ 'chest': [34, 36, 38, 40, 42],
10
+ 'waist': [28, 30, 32, 34, 36],
11
+ 'hip': [36, 38, 40, 42, 44],
12
+ 'size': ['S', 'M', 'L', 'XL', 'XXL']
13
+ }
14
+
15
+ # Train a LabelEncoder for sizes
16
+ label_encoder = LabelEncoder()
17
+ data['size_encoded'] = label_encoder.fit_transform(data['size'])
18
+
19
+ # Prepare features and target
20
+ X = np.array([data['chest'], data['waist'], data['hip']]).T
21
+ y = data['size_encoded'] # Using encoded sizes as the target
22
+
23
+ # Initialize and train the model
24
+ model = LinearRegression()
25
+ model.fit(X, y)
26
+
27
+ # Function to predict size based on measurements
28
+ def predict_size(chest, waist, hip):
29
+ input_features = np.array([[chest, waist, hip]])
30
+ predicted_size_encoded = model.predict(input_features)
31
+
32
+ # Clamp the predicted size to ensure it's within the valid range of labels
33
+ predicted_size_encoded_clamped = np.clip(predicted_size_encoded, 0, len(label_encoder.classes_) - 1)
34
+
35
+ # Convert the numeric prediction back to the original size
36
+ predicted_size = label_encoder.inverse_transform(predicted_size_encoded_clamped.astype(int))
37
+
38
+ return predicted_size[0]
39
+
40
+ # Create the Gradio interface
41
+ interface = gr.Interface(
42
+ fn=predict_size,
43
+ inputs=[
44
+ gr.Slider(minimum=30, maximum=50, step=1, label="Chest (inches)"),
45
+ gr.Slider(minimum=20, maximum=40, step=1, label="Waist (inches)"),
46
+ gr.Slider(minimum=30, maximum=50, step=1, label="Hip (inches)")
47
+ ],
48
+ outputs="text",
49
+ live=True,
50
+ title="AI Size Advisor",
51
+ description="Enter your body measurements to get an accurate clothing size recommendation based on past purchase data."
52
+ )
53
+
54
+ # Launch the interface
55
+ interface.launch()