File size: 2,914 Bytes
4cfee44
 
f59b209
dad3dcf
 
64157b7
dad3dcf
64157b7
dad3dcf
 
4cfee44
1166e7c
 
 
 
dad3dcf
 
 
 
1166e7c
 
 
 
dad3dcf
 
 
 
 
a6a319a
dad3dcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aebd697
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import gradio as gr
from fastai.vision.all import *
import pandas as pd
import platform
import pathlib

# Correction des chemins pour Windows sur Linux
plt = platform.system()
if plt == 'Linux':
    pathlib.WindowsPath = pathlib.PosixPath

# Charger le modèle
learn = load_learner('export.pkl')
labels = learn.dls.vocab

# Charger les recommandations depuis le fichier Excel
df = pd.read_excel("recommendation.xlsx")
classes = df['class'].unique()

# Fonction de prédiction
def predict(img):
    img = PILImage.create(img)
    pred, pred_idx, probs = learn.predict(img)
    result = {labels[i]: float(probs[i]) for i in range(len(labels))}
    return result

# Interface Gradio en mode Blocks
with gr.Blocks(title="Face condition Analyzer") as demo:
    gr.Markdown("## 🧠 Siham application Analysateur de la peau ")
    gr.Markdown(
        "Ce modèle détecte les problèmes de peau à partir d'une image faciale, entraîné sur un dataset personnalisé avec FastAI.\n\n"
        "📸 Téléversez une photo de votre visage ci-dessous pour voir la prédiction et les recommandations adaptées."
    )

    # Section prédiction
    with gr.Row():
        with gr.Column():
            image_input = gr.Image(type="pil", label="Image de votre visage", shape=(512, 512))
            predict_button = gr.Button("Analyser la peau")
        with gr.Column():
            label_output = gr.Label(num_top_classes=3, label="Résultat de l'analyse")

    predict_button.click(fn=predict, inputs=image_input, outputs=label_output)

    gr.Markdown("---")
    gr.Markdown("## 💡 Recommandations Produits par type de peau")

    # Section recommandations dynamiques
    with gr.Accordion("Afficher les recommandations par type de peau", open=True):
        for c in classes:
            with gr.Accordion(f"{c}", open=False):
                df_temp = df[df['class'] == c]
                with gr.Row():
                    for i, current_row in df_temp.iterrows():
                        with gr.Column():
                            gr.HTML(
                                f"<a href='{current_row['profit_link']}' target='_blank'>"
                                f"<img src='{current_row['product_image']}' "
                                f"alt='{current_row['title']}' style='width:100%; border-radius: 10px; margin-bottom:5px;'/></a>"
                                f"<div style='text-align:center; font-weight:bold;'>{current_row['title']}</div>"
                            )

# Lancer l'application
demo.launch()
@demo.api()  # pour exposer une API REST utilisable dans Flutter
def predict_api(file: gr.File):
    img = PILImage.create(file.name)
    pred, pred_idx, probs = learn.predict(img)
    return {
        "predicted_class": labels[pred_idx],
        "all_predictions": [
            {"class": labels[i], "probability": float(probs[i])}
            for i in range(len(labels))
        ]
    }