Spaces:
Sleeping
Sleeping
Commit ·
acf0ef6
1
Parent(s): a673410
update to image calsification.. labels added
Browse files
app.py
CHANGED
|
@@ -1,51 +1,52 @@
|
|
| 1 |
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import requests
|
| 4 |
import io
|
|
|
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
HF_API_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
MODEL_ID = "ander-machine/autotrain-u2mob-eufcd"
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
}
|
| 15 |
|
| 16 |
def predict_hf(image: Image.Image):
|
| 17 |
-
#
|
| 18 |
buffered = io.BytesIO()
|
| 19 |
image.save(buffered, format="PNG")
|
| 20 |
img_bytes = buffered.getvalue()
|
| 21 |
|
| 22 |
-
# Llamada a la API de Hugging Face
|
| 23 |
response = requests.post(
|
| 24 |
f"https://api-inference.huggingface.co/models/{MODEL_ID}",
|
| 25 |
headers=headers,
|
| 26 |
-
|
| 27 |
)
|
| 28 |
|
| 29 |
if response.status_code != 200:
|
| 30 |
return {"error": f"HTTP {response.status_code}: {response.text}"}
|
| 31 |
|
| 32 |
result = response.json()
|
| 33 |
-
# La respuesta esperada es una lista de diccionarios con “label” y “score”
|
| 34 |
-
# Por ejemplo: [{"label":"Healthy", "score":0.85}, {"label":"Black Sigatoka", "score":0.10}, …]
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# Interfaz Gradio
|
| 42 |
demo = gr.Interface(
|
| 43 |
fn=predict_hf,
|
| 44 |
-
inputs=gr.Image(type="pil", label="Sube
|
| 45 |
-
outputs=gr.Label(num_top_classes=
|
| 46 |
-
title="Clasificador de Enfermedades del Banano
|
| 47 |
-
description="
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
|
|
|
| 51 |
|
|
|
|
| 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 |
|