File size: 1,080 Bytes
fc1a53a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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)