Upload 3 files
Browse files- app.py +114 -0
- model.h5 +3 -0
- skincare_products_clean.csv +0 -0
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Untitled3.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/15hJqN0ojQZbWh5t6vlnLXzMAAuUyY84Y
|
8 |
+
"""
|
9 |
+
|
10 |
+
import gradio as gr
|
11 |
+
import pandas as pd
|
12 |
+
import tensorflow as tf
|
13 |
+
import numpy as np
|
14 |
+
import requests
|
15 |
+
from io import BytesIO
|
16 |
+
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input
|
17 |
+
from tensorflow.keras.preprocessing import image
|
18 |
+
|
19 |
+
# Load model and data
|
20 |
+
model = tf.keras.models.load_model("model.h5")
|
21 |
+
df = pd.read_csv("skincare_products_clean.csv")
|
22 |
+
|
23 |
+
# Load MobileNetV2 feature extractor
|
24 |
+
feature_extractor = MobileNetV2(weights='imagenet', include_top=False, pooling='avg')
|
25 |
+
|
26 |
+
# Reduction layer to match model input (32 features)
|
27 |
+
reduce_model = tf.keras.Sequential([
|
28 |
+
tf.keras.layers.InputLayer(input_shape=(1280,)),
|
29 |
+
tf.keras.layers.Dense(32, activation='relu')
|
30 |
+
])
|
31 |
+
|
32 |
+
CLASS_NAMES = [
|
33 |
+
"acne", "aging", "dryness", "sensitivity", "pigmentation",
|
34 |
+
"eczema", "rosacea", "dark circles", "wrinkles", "scars"
|
35 |
+
]
|
36 |
+
|
37 |
+
concern_ingredients = {
|
38 |
+
"acne": ["salicylic acid", "benzoyl peroxide", "niacinamide", "azelaic acid", "tea tree"],
|
39 |
+
"dryness": ["hyaluronic acid", "glycerin", "ceramide", "squalane"],
|
40 |
+
"aging": ["retinol", "peptides", "vitamin c", "niacinamide"],
|
41 |
+
"sensitivity": ["allantoin", "panthenol", "madecassoside", "centella"],
|
42 |
+
"pigmentation": ["vitamin c", "kojic acid", "azelaic acid", "niacinamide"],
|
43 |
+
}
|
44 |
+
|
45 |
+
def recommend_products(concern, num_results=5, max_price=None, sort_by="price"):
|
46 |
+
ingredients = concern_ingredients.get(concern.lower(), [concern.lower()])
|
47 |
+
pattern = '|'.join(ingredients)
|
48 |
+
df_filtered = df[df['clean_ingreds'].str.contains(pattern, case=False, na=False)]
|
49 |
+
|
50 |
+
if max_price:
|
51 |
+
df_filtered = df_filtered[df_filtered['price'] <= float(max_price)]
|
52 |
+
|
53 |
+
if df_filtered.empty:
|
54 |
+
df_filtered = df.copy()
|
55 |
+
|
56 |
+
if sort_by in df_filtered.columns:
|
57 |
+
df_filtered = df_filtered.sort_values(by=sort_by)
|
58 |
+
else:
|
59 |
+
df_filtered = df_filtered.sort_values(by="price")
|
60 |
+
|
61 |
+
df_filtered['clean_ingreds'] = df_filtered['clean_ingreds'].apply(
|
62 |
+
lambda x: ', '.join(eval(x)[:3]) + "..." if isinstance(x, str) and x.startswith('[') else x
|
63 |
+
)
|
64 |
+
df_filtered['price'] = df_filtered['price'].round(2)
|
65 |
+
|
66 |
+
output = ""
|
67 |
+
for _, row in df_filtered.head(num_results).iterrows():
|
68 |
+
output += f"🧴 **Product**: {row['product_name']}\n"
|
69 |
+
output += f"📦 **Type**: {row['product_type']}\n"
|
70 |
+
output += f"🧪 **Key Ingredients**: {row['clean_ingreds']}\n"
|
71 |
+
output += f"💰 **Price**: £{row['price']}\n\n"
|
72 |
+
return output.strip()
|
73 |
+
|
74 |
+
def diagnose_image(image_input):
|
75 |
+
try:
|
76 |
+
img = image_input.resize((224, 224))
|
77 |
+
img_array = image.img_to_array(img)
|
78 |
+
img_array = np.expand_dims(img_array, axis=0)
|
79 |
+
img_array = preprocess_input(img_array)
|
80 |
+
|
81 |
+
features_1280 = feature_extractor.predict(img_array)
|
82 |
+
features_32 = reduce_model.predict(features_1280)
|
83 |
+
preds = model.predict(features_32)
|
84 |
+
|
85 |
+
class_index = np.argmax(preds[0])
|
86 |
+
confidence = preds[0][class_index] * 100
|
87 |
+
return f"✅ Predicted Diagnosis: **{CLASS_NAMES[class_index].capitalize()}**\n🔎 Confidence: {confidence:.2f}%"
|
88 |
+
except Exception as e:
|
89 |
+
return f"❌ Error: {e}"
|
90 |
+
|
91 |
+
# Gradio Interface
|
92 |
+
text_interface = gr.Interface(
|
93 |
+
fn=recommend_products,
|
94 |
+
inputs=[
|
95 |
+
gr.Textbox(label="Skincare Concern (e.g., acne, dryness)"),
|
96 |
+
gr.Number(label="Number of Recommendations", value=5),
|
97 |
+
gr.Number(label="Max Price (Optional)", value=None),
|
98 |
+
gr.Textbox(label="Sort By (price, product_name, etc.)", value="price")
|
99 |
+
],
|
100 |
+
outputs="text",
|
101 |
+
title="🧴 Skincare Product Recommender"
|
102 |
+
)
|
103 |
+
|
104 |
+
image_interface = gr.Interface(
|
105 |
+
fn=diagnose_image,
|
106 |
+
inputs=gr.Image(type="pil", label="Upload Skin Image"),
|
107 |
+
outputs="text",
|
108 |
+
title="🧬 Skin Condition Diagnosis"
|
109 |
+
)
|
110 |
+
|
111 |
+
gr.TabbedInterface(
|
112 |
+
[text_interface, image_interface],
|
113 |
+
tab_names=["🧴 Text-Based Recommender", "🧬 Image-Based Diagnosis"]
|
114 |
+
).launch()
|
model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:039ef1e6f47274afa21df6cd06026b47fc79a288e2bd5e9ec7a168d90df875a7
|
3 |
+
size 110320
|
skincare_products_clean.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|