Thiwanka01 commited on
Commit
a66d894
1 Parent(s): 2807874

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 (you would replace this with your actual dataset)
7
+ data = {
8
+ 'chest': [34, 36, 38, 40, 42],
9
+ 'waist': [28, 30, 32, 34, 36],
10
+ 'hip': [36, 38, 40, 42, 44],
11
+ 'size': ['S', 'M', 'L', 'XL', 'XXL'],
12
+ 'fit': ['regular', 'tight', 'loose', 'regular', 'tight'], # Fit type (advanced feature)
13
+ 'style': ['casual', 'formal', 'casual', 'casual', 'formal'] # Style preference (advanced feature)
14
+ }
15
+
16
+ # Train a LabelEncoder for sizes
17
+ label_encoder = LabelEncoder()
18
+ data['size_encoded'] = label_encoder.fit_transform(data['size'])
19
+
20
+ # Encode fit and style as well
21
+ fit_encoder = LabelEncoder()
22
+ data['fit_encoded'] = fit_encoder.fit_transform(data['fit'])
23
+
24
+ style_encoder = LabelEncoder()
25
+ data['style_encoded'] = style_encoder.fit_transform(data['style'])
26
+
27
+ # Prepare features and target
28
+ X = np.array([data['chest'], data['waist'], data['hip'], data['fit_encoded'], data['style_encoded']]).T
29
+ y = data['size_encoded'] # Using encoded sizes as the target
30
+
31
+ # Initialize and train the model
32
+ model = LinearRegression()
33
+ model.fit(X, y)
34
+
35
+ # Function to predict size based on measurements, fit, and style preferences
36
+ def predict_size(chest, waist, hip, fit, style):
37
+ # Encode the fit and style preferences
38
+ fit_encoded = fit_encoder.transform([fit])[0]
39
+ style_encoded = style_encoder.transform([style])[0]
40
+
41
+ # Create the input feature array
42
+ input_features = np.array([[chest, waist, hip, fit_encoded, style_encoded]])
43
+
44
+ # Predict the size
45
+ predicted_size_encoded = model.predict(input_features)
46
+
47
+ # Clamp the predicted size to ensure it's within the valid range of labels
48
+ predicted_size_encoded_clamped = np.clip(predicted_size_encoded, 0, len(label_encoder.classes_) - 1)
49
+
50
+ # Convert the numeric prediction back to the original size
51
+ predicted_size = label_encoder.inverse_transform(predicted_size_encoded_clamped.astype(int))
52
+
53
+ return predicted_size[0]
54
+
55
+ # Create the Gradio interface
56
+ interface = gr.Interface(
57
+ fn=predict_size,
58
+ inputs=[
59
+ gr.Slider(minimum=30, maximum=50, step=1, label="Chest (inches)"),
60
+ gr.Slider(minimum=20, maximum=40, step=1, label="Waist (inches)"),
61
+ gr.Slider(minimum=30, maximum=50, step=1, label="Hip (inches)"),
62
+ gr.Dropdown(choices=['regular', 'tight', 'loose'], label="Fit Type"),
63
+ gr.Dropdown(choices=['casual', 'formal'], label="Style Preference")
64
+ ],
65
+ outputs="text",
66
+ live=True,
67
+ title="Advanced AI Size Advisor",
68
+ description="Enter your body measurements and preferences to get an accurate clothing size recommendation. The model takes into account fit and style preferences."
69
+ )
70
+
71
+ # Launch the interface
72
+ interface.launch()