Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ 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"
|
|
@@ -18,16 +19,30 @@ transform = Compose([
|
|
| 18 |
])
|
| 19 |
|
| 20 |
def predict(image):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
@@ -36,5 +51,8 @@ interface = gr.Interface(
|
|
| 36 |
description="Upload an image of a bird to classify it."
|
| 37 |
)
|
| 38 |
|
|
|
|
|
|
|
| 39 |
if __name__ == "__main__":
|
| 40 |
-
interface.launch()
|
|
|
|
|
|
| 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"
|
|
|
|
| 19 |
])
|
| 20 |
|
| 21 |
def predict(image):
|
| 22 |
+
try:
|
| 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 |
+
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"), # Changed from gr.inputs.Image to gr.Image
|
|
|
|
| 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()
|