Spaces:
Configuration error
Configuration error
Update model/random_forest.pkl
Browse files- model/random_forest.pkl +22 -18
model/random_forest.pkl
CHANGED
|
@@ -1,36 +1,40 @@
|
|
| 1 |
-
#
|
| 2 |
-
import pandas as pd
|
| 3 |
from sklearn.ensemble import RandomForestClassifier
|
| 4 |
from sklearn.preprocessing import LabelEncoder
|
| 5 |
import joblib
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
# Create
|
| 9 |
data = {
|
| 10 |
-
'face_shape': ['Oval', 'Round', 'Square'] *
|
| 11 |
-
'skin_tone': ['Fair', 'Medium', 'Dark'] *
|
| 12 |
-
'face_size': ['Small', 'Medium', 'Large'] *
|
| 13 |
-
'mask_style': ['
|
| 14 |
}
|
| 15 |
df = pd.DataFrame(data)
|
| 16 |
|
| 17 |
-
# Create and
|
| 18 |
-
encoders = {
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# Train
|
| 22 |
-
model = RandomForestClassifier(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
model.fit(
|
| 24 |
pd.DataFrame({col: encoders[col].transform(df[col])
|
| 25 |
for col in ['face_shape', 'skin_tone', 'face_size']}),
|
| 26 |
encoders['mask_style'].transform(df['mask_style'])
|
| 27 |
)
|
| 28 |
|
| 29 |
-
# Save
|
| 30 |
os.makedirs('model', exist_ok=True)
|
| 31 |
-
joblib.dump(model, 'model/random_forest.pkl')
|
| 32 |
-
joblib.dump(encoders, 'model/label_encoders.pkl')
|
| 33 |
|
| 34 |
-
|
| 35 |
-
print("Current directory:", os.listdir())
|
| 36 |
-
print("Model directory:", os.listdir('model'))
|
|
|
|
| 1 |
+
# train_model.py (corrected)
|
|
|
|
| 2 |
from sklearn.ensemble import RandomForestClassifier
|
| 3 |
from sklearn.preprocessing import LabelEncoder
|
| 4 |
import joblib
|
| 5 |
+
import pandas as pd
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# 1. Create sample data
|
| 9 |
data = {
|
| 10 |
+
'face_shape': ['Oval', 'Round', 'Square'] * 100,
|
| 11 |
+
'skin_tone': ['Fair', 'Medium', 'Dark'] * 100,
|
| 12 |
+
'face_size': ['Small', 'Medium', 'Large'] * 100,
|
| 13 |
+
'mask_style': ['StyleA', 'StyleB', 'StyleC'] * 100
|
| 14 |
}
|
| 15 |
df = pd.DataFrame(data)
|
| 16 |
|
| 17 |
+
# 2. Create and fit encoders
|
| 18 |
+
encoders = {
|
| 19 |
+
col: LabelEncoder().fit(df[col])
|
| 20 |
+
for col in ['face_shape', 'skin_tone', 'face_size', 'mask_style']
|
| 21 |
+
}
|
| 22 |
|
| 23 |
+
# 3. Train model
|
| 24 |
+
model = RandomForestClassifier(
|
| 25 |
+
n_estimators=50,
|
| 26 |
+
random_state=42,
|
| 27 |
+
max_depth=5
|
| 28 |
+
)
|
| 29 |
model.fit(
|
| 30 |
pd.DataFrame({col: encoders[col].transform(df[col])
|
| 31 |
for col in ['face_shape', 'skin_tone', 'face_size']}),
|
| 32 |
encoders['mask_style'].transform(df['mask_style'])
|
| 33 |
)
|
| 34 |
|
| 35 |
+
# 4. Save with protocol=4 for compatibility
|
| 36 |
os.makedirs('model', exist_ok=True)
|
| 37 |
+
joblib.dump(model, 'model/random_forest.pkl', protocol=4)
|
| 38 |
+
joblib.dump(encoders, 'model/label_encoders.pkl', protocol=4)
|
| 39 |
|
| 40 |
+
print("Model saved successfully!")
|
|
|
|
|
|