riyadifirman commited on
Commit
b5e285e
1 Parent(s): d75157f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -1,8 +1,9 @@
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"
@@ -20,17 +21,23 @@ transform = Compose([
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."
 
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
+ import traceback
7
 
8
  # Load model and processor
9
  model_name = "riyadifirman/klasifikasiburung"
 
21
  ])
22
 
23
  def predict(image):
24
+ try:
25
+ image = Image.fromarray(image)
26
+ inputs = transform(image).unsqueeze(0)
27
+ outputs = model(inputs)
28
+ logits = outputs.logits
29
+ predicted_class_idx = logits.argmax(-1).item()
30
+ return processor.decode(predicted_class_idx)
31
+ except Exception as e:
32
+ # Menampilkan error
33
+ print("An error occurred:", e)
34
+ print(traceback.format_exc()) # Ini akan print error secara detail
35
+ return "An error occurred while processing your request."
36
 
37
  # Create Gradio interface
38
  interface = gr.Interface(
39
  fn=predict,
40
+ inputs=gr.Image(type="numpy"),
41
  outputs="text",
42
  title="Bird Classification",
43
  description="Upload an image of a bird to classify it."