File size: 2,813 Bytes
2a7e139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5facf4
2a7e139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
import gradio as gr
import torch
from utils.model_loader import ModelLoader

# Initialize model loader
model_loader = ModelLoader(bucket_name="model-deployment-data")
model = model_loader.model
transform = model_loader.get_transforms()
labels = model_loader.labels
facts = model_loader.facts

def predict(image):
    """Make prediction and return label, confidence, and fact"""
    if image is None:
        return None, None
        
    # Preprocess image
    img_tensor = transform(image).unsqueeze(0)
    
    # Get prediction
    with torch.no_grad():
        outputs = model(img_tensor)
        probabilities = torch.nn.functional.softmax(outputs, dim=1)
    
    # Create prediction dictionary for all classes
    predictions = {
        labels[idx]: float(prob)
        for idx, prob in enumerate(probabilities[0])
    }
    
    # Get the fact for the top prediction
    top_label = max(predictions.items(), key=lambda x: x[1])[0]
    fact = facts[top_label]
    
    return predictions, fact

# Create Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Upload an image"),
    outputs=[
        gr.Label(num_top_classes=5, label="Classification Results"),
        gr.Textbox(label="Fun Fact About This Category!")
    ],
    title="🎯 Scene and Sport Classification",
    description="""
    ## Classify Scenes and Sports!
    Upload a clear photo, and I'll classify it into one of our categories and share an interesting fact about it! 
    This model can identify various scenes and sports activities with high accuracy.
    
    ### Supported Categories:
    **Scenes**: Buildings, Forest, Glacier, Mountain, Sea, Street
    **Sports**: Badminton, Baseball, Basketball, Football, Rowing, Swimming, Tennis
    """,
    article="""
    ### Tips for best results:
    - Use clear, well-lit photos
    - Ensure the main subject is visible
    - Avoid blurry or dark images
    
    ### Model Information:
    - This model is automatically updated with the best performing version through our CI/CD pipeline
    - Latest model accuracy and performance metrics are tracked and monitored
    - Trained on a combined dataset of natural scenes and sports activities
    """,
    examples=[
        ["examples/basketball.png"],
        ["examples/boxing.png"],
        ["examples/buildings.png"],
        ["examples/cricket.png"],
        ["examples/football.png"],
        ["examples/forest.png"],
        ["examples/formula_racing.png"],
        ["examples/glacier.png"],
        ["examples/golf.png"],
        ["examples/hockey.png"],
        ["examples/mountain.png"],
        ["examples/sea.png"],
        ["examples/street.png"]
    ],
    theme=gr.themes.Citrus(),
    css="footer {display: none !important;}"
)

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