Spaces:
Build error
Build error
| 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() | |
| # 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)) | |
| ] | |
| } | |