File size: 1,586 Bytes
4856f1f
 
8d30630
 
4856f1f
 
8d30630
 
 
 
 
 
 
 
 
 
 
4856f1f
 
8d30630
4856f1f
 
 
 
 
 
 
 
 
7fab19a
4856f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fab19a
4856f1f
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
import gradio as gr
from src.infer import inference_pipeline
from typing import Optional
from huggingface_hub import hf_hub_download

# model_path = "checkpoints/ckpt_23_10_2025/best_cat_dog_classifier_model_20251019_122336.pth"
hf = True
# Load weights
if hf:
    # Download from Hugging Face Model Hub (not from Spaces)
    model_path = hf_hub_download(
        repo_id="vikenkd/catdog-model",  
        filename="best_cat_dog_classifier_model_20251019_122336.pth",
        repo_type="model"
    )
else:
    model_path = "checkpoints/ckpt_23_10_2025/best_cat_dog_classifier_model_20251019_122336.pth"

def classify_image(
        image_path: str,
) -> str:
    """
    Classify the input image as cat or dog.
    """
    if image_path is None:
        return "Please upload an image."
    try:
        prediction = inference_pipeline(
            image_path=image_path, 
            model_path=model_path
        )
        return f"Prediction: {prediction.capitalize()}"
    except Exception as e:
        return f"Error: {str(e)}"

with gr.Blocks() as demo:
    gr.Markdown("# 🐶🐱 Cat vs Dog Classifier")

    with gr.Row():
        with gr.Column():
            image_input = gr.Image(
                type="filepath", 
                label="Input"
            )
            classify_button = gr.Button("🔍 Classify")
        with gr.Column():
            output_text = gr.Textbox(label="🧠 Prediction", placeholder="Result will appear here")

    classify_button.click(fn=classify_image, inputs=[image_input], outputs=[output_text])

demo.launch(share=True, debug=True)