File size: 1,979 Bytes
c1368fb
c4d99de
c1368fb
 
 
c4d99de
c1368fb
f3b050e
c1368fb
c4d99de
 
987cb98
 
c1368fb
c4d99de
 
 
 
 
 
 
 
 
c1368fb
c4d99de
 
c1368fb
f9469ab
 
c1368fb
c4d99de
 
f3b050e
f9469ab
c4d99de
 
 
 
 
 
 
 
 
 
 
 
e3ea4ef
c1368fb
 
c4d99de
c1368fb
c4d99de
 
c1368fb
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import zipfile
import tensorflow as tf
import numpy as np
import gradio as gr
import gdown
from tensorflow.keras.preprocessing import image as keras_image
from PIL import Image

# === Google Drive ZIP file ID ===
ZIP_FILE_ID = "1KYs5K2cIKp6C5VlIkATjoAJnUyDUhaFx"  # from your shared link
ZIP_PATH = "best_model (3).keras.zip"
MODEL_PATH = "model.weights.h5"  # expected inside ZIP after extraction

def download_and_extract():
    if not os.path.exists(MODEL_PATH):
        print("Downloading model ZIP from Google Drive...")
        url = f"https://drive.google.com/uc?id={ZIP_FILE_ID}"
        gdown.download(url, ZIP_PATH, quiet=False)
        print("Extracting ZIP...")
        with zipfile.ZipFile(ZIP_PATH, 'r') as zip_ref:
            zip_ref.extractall()
        print("Extraction complete.")

# === Download and extract the model on app startup ===
download_and_extract()

# === Load the model ===
model = tf.keras.models.load_model(MODEL_PATH)

# === Class names β€” adjust based on your model's labels ===
class_names = ['Dry', 'Normal', 'Oily', 'Acne', 'Blackheads', 'Dark Spots', 'Wrinkles', 'Skin Redness', 'Pores', 'Eye Bags']

def preprocess_image(img):
    img = img.convert("RGB")
    img = img.resize((224, 224))  # adjust size if your model expects 225Γ—225
    arr = keras_image.img_to_array(img) / 255.0
    return np.expand_dims(arr, axis=0)

def predict(img):
    inp = preprocess_image(img)
    preds = model.predict(inp, verbose=0)[0]
    top_idx = np.argmax(preds)
    top_pred = f"{class_names[top_idx]} ({preds[top_idx]*100:.2f}%)"
    all_probs = "\n".join(f"{class_names[i]}: {v*100:.2f}%" for i, v in enumerate(preds))
    return top_pred, all_probs

# === Gradio Interface ===
iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs=[gr.Textbox(label="Top Prediction"), gr.Textbox(label="All Class Probabilities")],
    title="Skin Type & Condition Predictor"
)

if __name__ == "__main__":
    iface.launch()