Upload food not food text classifier demo from notebook
Browse files- README.md +11 -6
- app.py +47 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,17 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Food Not Food Text Classifier
|
| 3 |
+
emoji: ππ«π°
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk-version: 5.25.2
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# ππ«π° Food or Not Food Text Classifier
|
| 14 |
+
|
| 15 |
+
Demo to showcase a text classifier to determine if a sentence is about food or not food.
|
| 16 |
+
|
| 17 |
+
[DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) model fine-tuned on a small [synthetic dataset]((https://huggingface.co/datasets/mrdbourke/learn_hf_food_not_food_image_captions)) of 250 generated food/not_food image captions.
|
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import required packages
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from typing import Dict
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
|
| 8 |
+
# Define a function to use the model
|
| 9 |
+
def food_not_food_classifier(text: str) -> Dict[str, float]:
|
| 10 |
+
|
| 11 |
+
# Set up food not food text classifier
|
| 12 |
+
food_not_food_classifier_pipeline = pipeline(task="text-classification",
|
| 13 |
+
model="karenwky/learn_hf_food_not_food_text_classifier_distilbert-base-uncased",
|
| 14 |
+
batch_size=32,
|
| 15 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
| 16 |
+
top_k=None)
|
| 17 |
+
|
| 18 |
+
# Get the output from the pipeline
|
| 19 |
+
outputs = food_not_food_classifier_pipeline(text)[0]
|
| 20 |
+
|
| 21 |
+
# Format output for Gradio
|
| 22 |
+
output_dict = {}
|
| 23 |
+
for item in outputs:
|
| 24 |
+
output_dict[item["label"]] = item["score"]
|
| 25 |
+
|
| 26 |
+
return output_dict
|
| 27 |
+
|
| 28 |
+
# Create a Gradio interface with detilas about the app
|
| 29 |
+
description = """
|
| 30 |
+
A text classifier to determine if a sentence is about food or not food.
|
| 31 |
+
|
| 32 |
+
Fine-tuned from [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) on a [LLM-generated dataset of food/not_food image captions](https://huggingface.co/datasets/mrdbourke/learn_hf_food_not_food_image_captions).
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
demo = gr.Interface(
|
| 36 |
+
fn=food_not_food_classifier,
|
| 37 |
+
inputs="text",
|
| 38 |
+
outputs=gr.Label(num_top_classes=2),
|
| 39 |
+
title="ππ«π° Food or Not Food Text Classifier",
|
| 40 |
+
description=description,
|
| 41 |
+
examples=[["Today is a sunny day."],
|
| 42 |
+
["Pineapple fried rice."]]
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Launch the interface
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
transformers
|