angiebow commited on
Commit
a1cb511
·
verified ·
1 Parent(s): 75b1348

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py CHANGED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from catboost import CatBoostRegressor
4
+ import os
5
+
6
+ # 1. Load the model
7
+ # Make sure 'catboost_skincare_model.cbm' is uploaded to the same Space
8
+ model = CatBoostRegressor()
9
+ if os.path.exists("catboost_skincare_model.cbm"):
10
+ model.load_model("catboost_skincare_model.cbm")
11
+ else:
12
+ print("Model file not found!")
13
+
14
+ def predict_rating(brand, product, cat1, cat2, origin, age_range, skin_type, concern, ing1, ing2, ing3):
15
+ # 2. Feature Engineering: Age Mapping
16
+ age_map = {
17
+ '18 and Under': 'Teens',
18
+ '19 - 24': 'Young Adult',
19
+ '25 - 29': 'Young Adult',
20
+ '30 - 34': 'Adult',
21
+ '35 - 39': 'Adult',
22
+ '40 - 44': 'Mature',
23
+ '45 and Above': 'Mature'
24
+ }
25
+ age_segment = age_map.get(age_range, 'Young Adult')
26
+
27
+ # 3. Feature Engineering: Ingredient Flags
28
+ all_ings = f"{ing1} {ing2} {ing3}".lower()
29
+
30
+ def check_ing(term):
31
+ return "1" if term in all_ings else "0"
32
+
33
+ # 4. Create DataFrame for prediction (Must match training column order)
34
+ input_df = pd.DataFrame([{
35
+ 'brand_name': brand,
36
+ 'product_name': product,
37
+ '1st_category': cat1,
38
+ '2nd_category': cat2,
39
+ 'brand_country_origin': origin,
40
+ 'age': age_range,
41
+ 'skin_type': skin_type,
42
+ 'skin_concern': concern,
43
+ 'ingredient_1': ing1,
44
+ 'ingredient_2': ing2,
45
+ 'ingredient_3': ing3,
46
+ # Engineered features the model expects
47
+ 'age_segment': age_segment,
48
+ 'has_niacinamide': check_ing('niacinamide'),
49
+ 'has_retinol': check_ing('retinol'),
50
+ 'has_salicylic_acid': check_ing('salicylic'),
51
+ 'has_hyaluronic_acid': check_ing('hyaluronic'),
52
+ 'has_vitamin_c': check_ing('vitamin c'),
53
+ 'skin_profile': f"{skin_type}_{concern}",
54
+ 'product_skin_fit': f"{cat2}_{skin_type}"
55
+ }])
56
+
57
+ # 5. Predict
58
+ prediction = model.predict(input_df)
59
+
60
+ # Return rounded rating
61
+ return round(float(prediction[0]), 2)
62
+
63
+ # 6. Define the Gradio Interface
64
+ # We define the labels so the "View API" button on HF shows clear names
65
+ inputs = [
66
+ gr.Textbox(label="Brand Name"),
67
+ gr.Textbox(label="Product Name"),
68
+ gr.Textbox(label="1st Category"),
69
+ gr.Textbox(label="2nd Category"),
70
+ gr.Textbox(label="Country of Origin"),
71
+ gr.Dropdown(choices=['18 and Under', '19 - 24', '25 - 29', '30 - 34', '35 - 39', '40 - 44', '45 and Above'], label="Age Range"),
72
+ gr.Textbox(label="Skin Type"),
73
+ gr.Textbox(label="Skin Concern"),
74
+ gr.Textbox(label="Ingredient 1"),
75
+ gr.Textbox(label="Ingredient 2"),
76
+ gr.Textbox(label="Ingredient 3")
77
+ ]
78
+
79
+ demo = gr.Interface(
80
+ fn=predict_rating,
81
+ inputs=inputs,
82
+ outputs=gr.Number(label="Predicted Rating"),
83
+ title="Skincare Rating Predictor API"
84
+ )
85
+
86
+ if __name__ == "__main__":
87
+ demo.launch()