Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 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 |
-
import traceback
|
| 7 |
|
| 8 |
# Load model and processor
|
| 9 |
model_name = "riyadifirman/klasifikasiburung"
|
|
@@ -14,45 +13,28 @@ model = AutoModelForImageClassification.from_pretrained(model_name)
|
|
| 14 |
normalize = Normalize(mean=processor.image_mean, std=processor.image_std)
|
| 15 |
transform = Compose([
|
| 16 |
Resize((224, 224)),
|
|
|
|
|
|
|
| 17 |
ToTensor(),
|
| 18 |
normalize,
|
| 19 |
])
|
| 20 |
|
| 21 |
def predict(image):
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return processor.decode(predicted_class_idx)
|
| 29 |
-
except Exception as e:
|
| 30 |
-
print("An error occurred:", e)
|
| 31 |
-
print(traceback.format_exc())
|
| 32 |
-
return "An error occurred while processing your request."
|
| 33 |
-
|
| 34 |
-
def predict_function(input_data):
|
| 35 |
-
try:
|
| 36 |
-
# model
|
| 37 |
-
output = f"Processed input: {input_data}" # Gantilah dengan model
|
| 38 |
-
return output
|
| 39 |
-
except Exception as e:
|
| 40 |
-
# Menampilkan error
|
| 41 |
-
print("An error occurred:", e)
|
| 42 |
-
print(traceback.format_exc()) # Ini akan print error secara detail
|
| 43 |
-
return "An error occurred while processing your request."
|
| 44 |
|
| 45 |
# Create Gradio interface
|
| 46 |
interface = gr.Interface(
|
| 47 |
fn=predict,
|
| 48 |
-
inputs=gr.Image(type="numpy"),
|
| 49 |
outputs="text",
|
| 50 |
title="Bird Classification",
|
| 51 |
description="Upload an image of a bird to classify it."
|
| 52 |
)
|
| 53 |
|
| 54 |
-
iface = gr.Interface(fn=predict_function, inputs="text", outputs="text")
|
| 55 |
-
|
| 56 |
if __name__ == "__main__":
|
| 57 |
interface.launch()
|
| 58 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 4 |
+
from torchvision.transforms import Compose, Resize, ToTensor, Normalize,RandomHorizontalFlip, RandomRotation
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
|
| 7 |
# Load model and processor
|
| 8 |
model_name = "riyadifirman/klasifikasiburung"
|
|
|
|
| 13 |
normalize = Normalize(mean=processor.image_mean, std=processor.image_std)
|
| 14 |
transform = Compose([
|
| 15 |
Resize((224, 224)),
|
| 16 |
+
RandomHorizontalFlip(),
|
| 17 |
+
RandomRotation(10),
|
| 18 |
ToTensor(),
|
| 19 |
normalize,
|
| 20 |
])
|
| 21 |
|
| 22 |
def predict(image):
|
| 23 |
+
image = Image.fromarray(image)
|
| 24 |
+
inputs = transform(image).unsqueeze(0)
|
| 25 |
+
outputs = model(inputs)
|
| 26 |
+
logits = outputs.logits
|
| 27 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 28 |
+
return processor.decode(predicted_class_idx)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Create Gradio interface
|
| 31 |
interface = gr.Interface(
|
| 32 |
fn=predict,
|
| 33 |
+
inputs=gr.inputs.Image(type="numpy"),
|
| 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()
|
|
|