ander-machine commited on
Commit
adb15a8
·
1 Parent(s): acf0ef6

update to image calsification.. labels fixed

Browse files
Files changed (1) hide show
  1. app.py +34 -47
app.py CHANGED
@@ -1,52 +1,39 @@
1
- import os
2
- import io
3
- import requests
4
  import gradio as gr
5
- from PIL import Image
6
-
7
- # Configura tu modelo
8
- MODEL_ID = "ander-machine/autotrain-u2mob-eufcd"
9
- HF_API_TOKEN = os.getenv("HF_TOKEN") # 🔒 definido en Hugging Face -> Settings -> Secrets
10
-
11
- headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
12
-
13
- # Clases conocidas (3 etiquetas)
14
- CLASSES = ["black_sigatoka", "fusarium", "healthy"]
15
-
16
- def predict_hf(image: Image.Image):
17
- # Convertir imagen a bytes
18
- buffered = io.BytesIO()
19
- image.save(buffered, format="PNG")
20
- img_bytes = buffered.getvalue()
21
-
22
- # Llamada a la API de inferencia de Hugging Face
23
- response = requests.post(
24
- f"https://api-inference.huggingface.co/models/{MODEL_ID}",
25
- headers=headers,
26
- data=img_bytes # 👈 importante usar "data", no "files"
27
- )
28
-
29
- if response.status_code != 200:
30
- return {"error": f"HTTP {response.status_code}: {response.text}"}
31
-
32
- result = response.json()
33
-
34
- # Esperamos algo como: [{"label":"healthy","score":0.95}, ...]
35
- if isinstance(result, list):
36
- return {item["label"]: float(item["score"]) for item in result}
37
-
38
- # Si algo falla, devolver mensaje de error
39
- return {"error": str(result)}
40
-
41
- # Interfaz de Gradio
42
  demo = gr.Interface(
43
- fn=predict_hf,
44
- inputs=gr.Image(type="pil", label="Sube hoja de banano"),
45
- outputs=gr.Label(num_top_classes=3, label="Clasificación"),
46
- title="Clasificador de Enfermedades del Banano",
47
- description="Clasifica la hoja como 'black_sigatoka', 'fusarium' o 'healthy'."
48
  )
49
 
50
- # Importante: en Spaces NO usar share=True
51
- demo.launch()
52
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load your model
5
+ pipe = pipeline("image-classification", model="ander-machine/autotrain-u2mob-eufcd")
6
+
7
+ def predict_safe(image):
8
+ try:
9
+ # Run the pipeline
10
+ preds = pipe(image)
11
+
12
+ # preds normalmente es algo como:
13
+ # [{'label': 'cat', 'score': 0.97}, {'label': 'dog', 'score': 0.03}]
14
+ if isinstance(preds, list) and len(preds) > 0:
15
+ best = preds[0]
16
+ return {
17
+ "label": best["label"],
18
+ "confidences": [(p["label"], float(p["score"])) for p in preds]
19
+ }
20
+ else:
21
+ # fallback si la lista viene vacía
22
+ return {"label": "unknown", "confidences": [("unknown", 0.0)]}
23
+
24
+ except Exception as e:
25
+ # fallback en caso de error (como el HTTP 404 que viste)
26
+ return {"label": "error", "confidences": [("error", 0.0)]}
27
+
28
+ # Interfaz Gradio
 
 
 
 
 
 
 
 
 
 
29
  demo = gr.Interface(
30
+ fn=predict_safe,
31
+ inputs=gr.Image(type="pil"),
32
+ outputs=gr.Label(num_top_classes=3),
33
+ title="Image Classifier",
34
+ description="Clasificador con fallback seguro"
35
  )
36
 
37
+ if __name__ == "__main__":
38
+ demo.launch()
39