Spaces:
Sleeping
Sleeping
riyadifirman
commited on
Commit
•
0d599fc
1
Parent(s):
89a7aa4
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
4 |
+
from torchvision.transforms import Compose, Resize, ToTensor, Normalize
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Load model and processor
|
8 |
+
model_name = "riyadifirman/klasifikasiburung"
|
9 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
10 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# Define image transformations
|
13 |
+
normalize = Normalize(mean=processor.image_mean, std=processor.image_std)
|
14 |
+
transform = Compose([
|
15 |
+
Resize((224, 224)),
|
16 |
+
ToTensor(),
|
17 |
+
normalize,
|
18 |
+
])
|
19 |
+
|
20 |
+
def predict(image):
|
21 |
+
image = Image.fromarray(image)
|
22 |
+
inputs = transform(image).unsqueeze(0)
|
23 |
+
outputs = model(inputs)
|
24 |
+
logits = outputs.logits
|
25 |
+
predicted_class_idx = logits.argmax(-1).item()
|
26 |
+
return processor.decode(predicted_class_idx)
|
27 |
+
|
28 |
+
# Create Gradio interface
|
29 |
+
# In newer versions of Gradio, 'inputs' and 'outputs' are directly
|
30 |
+
# specified within the gr.Interface constructor.
|
31 |
+
interface = gr.Interface(
|
32 |
+
fn=predict,
|
33 |
+
inputs=gr.Image(type="numpy"), # Changed from gr.inputs.Image to gr.Image
|
34 |
+
outputs="text",
|
35 |
+
title="Bird Classification",
|
36 |
+
description="Upload an image of a bird to classify it."
|
37 |
+
)
|
38 |
+
|
39 |
+
if __name__ == "__main__":
|
40 |
+
interface.launch()
|