import torch | |
import gradio as gr | |
from typing import Dict | |
from transformers import pipeline | |
def food_not_food_classifier(text:str) -> Dict[str, float]: | |
food_not_food_classifier = pipeline( | |
task = "text-classification", | |
#model = "mrdbourke/learn_hf_food_not_food_text_classifier-distilbert-base-uncased", | |
model = "YarnGuo/learn_hf_food_not_food_text_classifier-distilbert-base-uncased", | |
device = "cuda" if torch.cuda.is_available() else "cpu", | |
batch_size = 32, | |
top_k = None | |
) | |
#output is a list of dict | |
outputs = food_not_food_classifier(text)[0] | |
output_dict = {} | |
for item in outputs: | |
output_dict[item["label"]] = item["score"] | |
return output_dict | |
demo = gr.Interface( | |
fn = food_not_food_classifier, | |
inputs = "text", | |
outputs = gr.Label(num_top_classes=2), | |
title = "Food or Not Food Classifer", | |
description = "A text classfier to say a senstence about food or not food", | |
examples = [["I whipped up a fresh batch of code, but it seems to have a syntax error."], | |
["A delicious photo of a plate of scrambled eggs, bacon and toast."]] | |
) | |
if __name__ == "__main__": | |
demo.launch() | |