import torch import gradio as gr from transformers import pipeline from typing import Dict def food_not_food_classifier(text: str) -> Dict[str, float]: # Create the classifier pipeline food_not_food_classifier_pipeline = pipeline( task="text-classification", model="joadithya/learn_hf_food_not_food_text_classifier-distilbert-base-uncased", batch_size=32, device="cuda" if torch.cuda.is_available() else "cpu", top_k=None # Returning all possible labels for a given input ) # Get the outputs 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 description = """ A text classifier model to determine whether a caption 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 captions](https://huggingface.co/datasets/mrdbourke/learn_hf_food_not_food_image_captions) """ demo = gr.Interface( fn=food_not_food_classifier, inputs="text", outputs=gr.Label(num_top_classes=2), title="Food Caption Classifier", description=description, examples=[["Nothing beats the taste of home"], ["Love served on a plate"], ["A toast with cherry on top"]] ) if __name__ == "__main__": demo.launch()