tool_classifier / app.py
edesaras's picture
Fixed 'example images not showing'
7146057
import pathlib
import gradio as gr
# to solve the mismatch between windows (local) system and huggingspace (linux)
import platform
plt = platform.system()
if plt == 'Linux': pathlib.WindowsPath = pathlib.PosixPath
elif plt == 'Windows': pathlib.PosixPath = pathlib.WindowsPath
from fastai.vision.all import *
class_names = ["resistor", "bipolar transistor", "mosfet", "capacitor", "inductor", "wire", "led", "diode", "thermistor", "switch", "battery", "hammer", "screwdriver", "scissors", "wrench", "mallet", "axe"]
learn = load_learner('model.pkl')
labels = learn.dls.vocab
def predict(img):
img = PILImage.create(img)
pred,pred_idx,probs = learn.predict(img)
return {labels[i]: float(probs[i]) for i in range(len(labels))}
iface = gr.Interface(
fn=predict,
inputs=gr.components.Image(shape=(224, 224)),
outputs=gr.components.Label(num_top_classes=3),
description="Tool Classifier",
article=f"<h1>Class Names</h1><br><p style='text-align: center'>{', '.join(class_names)}</p>",
examples=[["mosfet.jpg"], ["screwdriver.jpg"], ["hammel.png"], ["led.jpg"]],
live=True,
)
iface.launch(enable_queue=True)