Spaces:
Sleeping
Sleeping
import gradio as gr | |
import torch | |
import os | |
from model import create_effnet_b2 | |
from timeit import default_timer as timer | |
from typing import Tuple, Dict | |
from PIL import Image | |
import numpy as np | |
class_names = ["pizza", "steak", "sushi"] | |
effnet_b2_model , effnet_b2_transform = create_effnet_b2() | |
effnet_b2_model.load_state_dict(torch.load(f = "./effnet_b2.pt", map_location = torch.device("cpu"))) | |
def predict(img) -> Tuple[Dict, float]: | |
start_time = timer() | |
# Convert from NumPy array to PIL image | |
if isinstance(img, np.ndarray): | |
img = Image.fromarray(img.astype("uint8"), "RGB") | |
img = effnet_b2_transform(img).unsqueeze(0) | |
effnet_b2_model.eval() | |
with torch.inference_mode(): | |
pred_prob = torch.softmax(effnet_b2_model(img), dim=1) | |
pred_label_probs = { | |
class_names[i]: float(pred_prob[0][i]) for i in range(len(class_names)) | |
} | |
end_time = timer() | |
pred_time = round(end_time - start_time, 4) | |
return pred_label_probs, pred_time | |
import os | |
# Create separate output components | |
exmaple_list = [["examples/" + example] for example in os.listdir("examples")] | |
label_output = gr.Label(label="Classification Probabilities") | |
number_output = gr.Number(label="Inference Time (seconds)") # Changed label to be more accurate | |
demo = gr.Interface( | |
fn=predict, | |
inputs="image", | |
outputs=[label_output, number_output], | |
examples=exmaple_list, # Handle case where image_path might be None | |
title="Food Vision Mini π", | |
description="Upload an image to see classification probabilities and inference time.Finetuned on effnet_b2 on(pizza,steak,sushi)", | |
article="Created By sachin", | |
allow_flagging="never" | |
) | |
demo.launch(debug=False, share=True) | |