RICHERGIRL commited on
Commit
22963a9
·
verified ·
1 Parent(s): 154712e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -33
app.py CHANGED
@@ -1,34 +1,50 @@
1
- import gradio as gr
2
- import tensorflow as tf
3
- import cv2
4
- import numpy as np
5
  import pandas as pd
6
- from sklearn.preprocessing import LabelEncoder
7
-
8
- # Load trained model
9
- model = tf.keras.models.load_model("cnn_mask_model.h5")
10
-
11
- # Load dataset & encode mask types
12
- df = pd.read_excel("mask_dataset.xlsx")
13
- label_encoder = LabelEncoder()
14
- df["mask_type"] = label_encoder.fit_transform(df["mask_type"])
15
-
16
- # Define function for prediction
17
- def predict_mask(image):
18
- img_resized = cv2.resize(image, (128, 128)).reshape(1, 128, 128, 3) / 255.0
19
- mask_pred = model.predict(img_resized).argmax()
20
-
21
- mask_name = label_encoder.inverse_transform([mask_pred])[0] # Convert label back
22
-
23
- return f"Recommended Mask: {mask_name}"
24
-
25
- # Gradio interface for Hugging Face Spaces
26
- iface = gr.Interface(
27
- fn=predict_mask,
28
- inputs=gr.Image(type="numpy", label="Upload Your Face Image"),
29
- outputs="text",
30
- title="🎭 Party Mask Recommendation App",
31
- description="Upload a face image and get the best party mask recommendation!"
32
- )
33
-
34
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import pandas as pd
2
+ from sklearn.preprocessing import LabelEncoder
3
+ from sklearn.ensemble import RandomForestClassifier
4
+ from sklearn.model_selection import train_test_split
5
+ import gradio as gr
6
+
7
+ # Function to load/generate data (replace with your data loading logic)
8
+ def load_data():
9
+ data = {
10
+ 'face_shape': ['round', 'oval', 'square', 'heart', 'oval', 'round', 'square', 'heart', 'oval', 'square'],
11
+ 'skin_tone': ['fair', 'medium', 'deep', 'cool', 'warm', 'fair', 'medium', 'deep', 'cool', 'warm'],
12
+ 'face_size': ['small', 'medium', 'large', 'medium', 'small', 'large', 'small', 'large', 'medium', 'small'],
13
+ 'mask_style': ['glitter_cat', 'gold_venetian', 'black_minimal', 'floral_masquerade', 'gold_venetian',
14
+ 'glitter_cat', 'black_minimal', 'floral_masquerade', 'gold_venetian', 'black_minimal']
15
+ }
16
+ return pd.DataFrame(data)
17
+
18
+ # Rest of your code (preprocessing, model training, recommend_mask function)
19
+ # Split features and target
20
+ X = df[['face_shape', 'skin_tone', 'face_size']]
21
+ y = df['mask_style']
22
+
23
+ # Split into train and test sets
24
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42, stratify=y)
25
+
26
+ # Train Random Forest
27
+ rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
28
+
29
+ rf_model.fit(X_train, y_train)
30
+ df = load_data()
31
+ # ... (preprocessing, model training)
32
+
33
+ # ... (recommend_mask function)
34
+
35
+ # ... (Get unique values for dropdown choices)
36
+
37
+ # Define Gradio interface
38
+ iface = gr.Interface(
39
+ fn=recommend_mask,
40
+ inputs=[
41
+ gr.Dropdown(choices=face_shapes_labels, label="Face Shape"),
42
+ gr.Dropdown(choices=skin_tones_labels, label="Skin Tone"),
43
+ gr.Dropdown(choices=face_sizes_labels, label="Face Size"),
44
+ ],
45
+ outputs="text",
46
+ title="🎭 Party Face Mask Recommender",
47
+ description="Get personalized party face mask recommendations based on your facial features."
48
+ )
49
+
50
+ iface.launch(share=True, server_name="0.0.0.0", server_port=7860) # Updated launch for Hugging Face