File size: 2,317 Bytes
7c6e389
 
 
 
21def63
b6222f9
7c6e389
 
b6222f9
 
 
21def63
b6222f9
 
 
 
7c6e389
b6222f9
 
 
 
 
 
 
21def63
b6222f9
7c6e389
21def63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c6e389
b6222f9
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import gradio as gr
from transformers import pipeline
from PIL import Image

# Cargamos el modelo CLIP
clf = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")

def dog_cat_classifier(image):
    if image is None:
        return {"❌ Sin imagen": 1.0}
    
    image = image.convert("RGB")
    candidate_labels = ["dog", "cat", "neither dog nor cat"]
    preds = clf(image, candidate_labels=candidate_labels)
    
    result = {}
    for p in preds:
        label = p["label"]
        score = p["score"]
        if label == "dog":
            result["🐶 Dog"] = score
        elif label == "cat":
            result["🐱 Cat"] = score
        else:
            result["❌ Other"] = score
    return result

# --- DISEÑO DE LA INTERFAZ ---
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="slate")) as demo:
    
    # Encabezado con Estilo
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown(
                """
                # 🐾 AI Pet Detector
                ### Clasificador inteligente basado en CLIP (Zero-Shot)
                Sube una foto y deja que la IA identifique si es un gato, un perro o algo totalmente distinto.
                """
            )
    
    gr.HTML("<hr>") # Una línea separadora elegante

    with gr.Row():
        # Columna Izquierda: Entrada
        with gr.Column(variant="panel"):
            gr.Markdown("### 📸 Paso 1: Sube tu imagen")
            input_img = gr.Image(type="pil", label=None)
            btn = gr.Button("🚀 Analizar Mascota", variant="primary")

        # Columna Derecha: Salida
        with gr.Column(variant="panel"):
            gr.Markdown("### 🐾 Paso 2: Resultado")
            output_label = gr.Label(num_top_classes=3, label="Probabilidades")
    
    # Ejemplos (Añaden mucha "presencia" visual)
    gr.Examples(
        examples=[["ejemplo_perro.jpg"], ["ejemplo_gato.jpg"]], # Asegúrate de tener estas imágenes o quita esta parte
        inputs=input_img
    )

    # Pie de página
    gr.Markdown(
        """
        ---
        *Desarrollado con ❤️ usando OpenAI CLIP y Gradio.*
        """
    )

    # Evento de clic
    btn.click(fn=dog_cat_classifier, inputs=input_img, outputs=output_label)

if __name__ == "__main__":
    demo.launch()