File size: 1,749 Bytes
6692a2b
 
 
 
 
 
4f12f52
 
 
6692a2b
 
 
4f12f52
6692a2b
4f12f52
6692a2b
4f12f52
6692a2b
 
4f12f52
 
 
 
 
 
 
6692a2b
4f12f52
 
 
 
 
 
6692a2b
4f12f52
 
6692a2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f12f52
6692a2b
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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)