Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| # Load your model | |
| pipe = pipeline("image-classification", model="ander-machine/autotrain-u2mob-eufcd") | |
| def predict_safe(image): | |
| try: | |
| # Run the pipeline | |
| preds = pipe(image) | |
| # preds normalmente es algo como: | |
| # [{'label': 'cat', 'score': 0.97}, {'label': 'dog', 'score': 0.03}] | |
| if isinstance(preds, list) and len(preds) > 0: | |
| best = preds[0] | |
| return { | |
| "label": best["label"], | |
| "confidences": [(p["label"], float(p["score"])) for p in preds] | |
| } | |
| else: | |
| # fallback si la lista viene vacía | |
| return {"label": "unknown", "confidences": [("unknown", 0.0)]} | |
| except Exception as e: | |
| # fallback en caso de error (como el HTTP 404 que viste) | |
| return {"label": "error", "confidences": [("error", 0.0)]} | |
| # Interfaz Gradio | |
| demo = gr.Interface( | |
| fn=predict_safe, | |
| inputs=gr.Image(type="pil"), | |
| outputs=gr.Label(num_top_classes=3), | |
| title="Image Classifier", | |
| description="Clasificador con fallback seguro" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |