StellaSoh's picture
Uploading our food not food text classifier from the video!
ea87271 verified
import torch
import gradio as gr
from typing import Dict
from transformers import pipeline
# 2. Define our function to use with our model
def food_not_food_classifier(text: str) -> Dict[str, float]:
# Set up food_not_food_classifer pipeline
food_not_food_classifier_pipeline = pipeline(task="text-classification",
model="StellaSoh/learn_hf_food_not_food_text_classifier-distilbert-base-uncased",
batch_size=32,
device = 0 if torch.cuda.is_available() else -1,
top_k=None) # top_k=None --> all possible labels
# Get the output from the pipeline
outputs = food_not_food_classifier_pipeline(text)[0]
# Format output for Gradio
output_dict = {}
for item in outputs:
output_dict[item["label"]] = item["score"]
return output_dict
#3. Create a Gradio interface
description = """ A text classifier to determine if a sentence is about food or not food.
Fine-tuned from [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) on a [small dataset of food and not food text](https://huggingface.co/datasets/mrdbourke/learn_hf_food_not_food_image_captions).
See [source code](https://github.com/mrdbourke/learn-huggingface/blob/main/notebooks/hugging_face_text_classification_tutorial.ipynb).
"""
demo = gr.Interface(fn=food_not_food_classifier,
inputs="text",
outputs=gr.Label(num_top_classes=2), # show top 2 classes (that's all we have)
title="πŸ—πŸš«πŸ₯‘ Food or Not Food Text Classifier",
description=description,
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."]])
# 4. Launch the interface
if __name__ == "__main__":
demo.launch()