File size: 1,274 Bytes
0d599fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import AutoImageProcessor, AutoModelForImageClassification
from torchvision.transforms import Compose, Resize, ToTensor, Normalize
from PIL import Image

# Load model and processor
model_name = "riyadifirman/klasifikasiburung"
processor = AutoImageProcessor.from_pretrained(model_name)
model = AutoModelForImageClassification.from_pretrained(model_name)

# Define image transformations
normalize = Normalize(mean=processor.image_mean, std=processor.image_std)
transform = Compose([
    Resize((224, 224)),
    ToTensor(),
    normalize,
])

def predict(image):
    image = Image.fromarray(image)
    inputs = transform(image).unsqueeze(0)
    outputs = model(inputs)
    logits = outputs.logits
    predicted_class_idx = logits.argmax(-1).item()
    return processor.decode(predicted_class_idx)

# Create Gradio interface
# In newer versions of Gradio, 'inputs' and 'outputs' are directly
# specified within the gr.Interface constructor.
interface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="numpy"), # Changed from gr.inputs.Image to gr.Image
    outputs="text",
    title="Bird Classification",
    description="Upload an image of a bird to classify it."
)

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