Spaces:
Build error
Build error
| # api.py | |
| import gradio as gr | |
| from fastai.vision.all import * | |
| import platform | |
| import pathlib | |
| # Corriger WindowsPath si nécessaire | |
| plt = platform.system() | |
| if plt == 'Linux': pathlib.WindowsPath = pathlib.PosixPath | |
| # Charger le modèle | |
| learn = load_learner('export.pkl') | |
| labels = learn.dls.vocab | |
| # Fonction principale de prédiction | |
| def predict(img): | |
| img = PILImage.create(img) | |
| pred, pred_idx, probs = learn.predict(img) | |
| # Résultat principal | |
| predicted_class = labels[pred_idx] | |
| # Préparer réponse JSON | |
| response = { | |
| "predicted_class": predicted_class, | |
| "all_predictions": [ | |
| { | |
| "class": labels[i], | |
| "probability": float(probs[i]) | |
| } for i in range(len(labels)) | |
| ] | |
| } | |
| return response | |
| # Interface API (sans HTML, juste pour Flutter) | |
| demo = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil", shape=(512, 512)), | |
| outputs=gr.JSON(label="Résultat"), | |
| title="Face Analyzer API", | |
| description="Interface API pour analyseur de peau" | |
| ) | |
| demo.launch(share=True) | |