iannnjay's picture
Upload inference.py with huggingface_hub
5b272e3 verified
# -*- coding: utf-8 -*-
"""Untitled16.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1ceJfc29fenDr72HRHvjuEBwP0lUTzKSK
"""
from huggingface_hub import from_pretrained_keras
from tensorflow.keras.applications.resnet50 import preprocess_input
import tensorflow as tf
import numpy as np
from PIL import Image
# Load model saat inference
model = from_pretrained_keras("iannnjay/resnet-dog-emotion-prediction")
# Label kelas kamu, contoh:
CLASS_NAMES = ["Happy", "Sad", "Angry", "Neutral"]
def preprocess(image: Image.Image):
image = image.resize((224, 224))
img_array = np.array(image)
img_array = preprocess_input(img_array)
return np.expand_dims(img_array, axis=0)
def predict(image: Image.Image):
input_tensor = preprocess(image)
preds = model.predict(input_tensor)
predicted_class = CLASS_NAMES[np.argmax(preds)]
confidence = float(np.max(preds))
return {"label": predicted_class, "confidence": confidence}