Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import os
|
| 4 |
+
from utils import extract_features
|
| 5 |
+
|
| 6 |
+
# Auto-train if model doesn't exist
|
| 7 |
+
if not os.path.exists('model/random_forest.pkl'):
|
| 8 |
+
print("Training model...")
|
| 9 |
+
os.system('python train_model.py')
|
| 10 |
+
|
| 11 |
+
# Load model
|
| 12 |
+
model = joblib.load('model/random_forest.pkl')
|
| 13 |
+
encoders = joblib.load('model/label_encoders.pkl')
|
| 14 |
+
|
| 15 |
+
def recommend_mask(image):
|
| 16 |
+
try:
|
| 17 |
+
face_shape, skin_tone, face_size = extract_features(image)
|
| 18 |
+
prediction = model.predict([[
|
| 19 |
+
encoders['face_shape'].transform([face_shape])[0],
|
| 20 |
+
encoders['skin_tone'].transform([skin_tone])[0],
|
| 21 |
+
encoders['face_size'].transform([face_size])[0]
|
| 22 |
+
]])[0]
|
| 23 |
+
return (
|
| 24 |
+
encoders['mask_style'].classes_[prediction],
|
| 25 |
+
encoders['mask_images'][prediction]
|
| 26 |
+
)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return "Error", "masks/default.png"
|
| 29 |
+
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=recommend_mask,
|
| 32 |
+
inputs=gr.Image(type="filepath"),
|
| 33 |
+
outputs=[
|
| 34 |
+
gr.Textbox(label="Recommended Style"),
|
| 35 |
+
gr.Image(label="Mask Preview")
|
| 36 |
+
],
|
| 37 |
+
title="🎭 Mask Recommender"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch()
|