Uploading our food not food text classifier demo from the video!
Browse files- README.md +11 -4
- app.py +48 -0
- requirements.txt +3 -0
README.md
CHANGED
@@ -1,12 +1,19 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
colorFrom: blue
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.40.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Food Not Food Text Classifier
|
3 |
+
emoji: ππ«π₯
|
4 |
colorFrom: blue
|
5 |
+
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.40.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
+
# ππ«π₯ Food Not Food Text Classifier
|
14 |
+
|
15 |
+
Small demo to showcase a text classifier to determine if a sentence is about food or not food.
|
16 |
+
|
17 |
+
DistilBERT model fine-tuned on a small synthetic dataset of [250 generated food/not_food image captions](https://huggingface.co/datasets/mrdbourke/learn_hf_food_not_food_image_captions).
|
18 |
+
|
19 |
+
See [source code notebook](https://github.com/mrdbourke/learn-huggingface/blob/main/notebooks/hugging_face_text_classification_tutorial.ipynb).
|
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 1. Import the required packages
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
from typing import Dict
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
# 2. Define our function to use with our model.
|
9 |
+
def food_not_food_classifier(text: str) -> Dict[str, float]:
|
10 |
+
# 2. Setup food not food text classifier
|
11 |
+
food_not_food_classifier_pipeline = pipeline(task="text-classification",
|
12 |
+
model="mrdbourke/learn_hf_food_not_food_text_classifier-distilbert-base-uncased",
|
13 |
+
batch_size=32,
|
14 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
15 |
+
top_k=None) # top_k=None => return all possible labels
|
16 |
+
|
17 |
+
# 3. Get the outputs from our pipeline
|
18 |
+
outputs = food_not_food_classifier_pipeline(text)[0]
|
19 |
+
|
20 |
+
# 4. Format output for Gradio
|
21 |
+
output_dict = {}
|
22 |
+
for item in outputs:
|
23 |
+
output_dict[item["label"]] = item["score"]
|
24 |
+
|
25 |
+
return output_dict
|
26 |
+
|
27 |
+
# 3. Create a Gradio interface
|
28 |
+
description = """
|
29 |
+
A text classifier to determine if a sentence is about food or not food.
|
30 |
+
|
31 |
+
Fine-tuned from [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) a [dataset of LLM generated food/not_food image captions](https://huggingface.co/datasets/mrdbourke/learn_hf_food_not_food_image_captions).
|
32 |
+
|
33 |
+
See [source code](https://github.com/mrdbourke/learn-huggingface/blob/main/notebooks/hugging_face_text_classification_tutorial.ipynb).
|
34 |
+
"""
|
35 |
+
|
36 |
+
demo = gr.Interface(
|
37 |
+
fn=food_not_food_classifier,
|
38 |
+
inputs="text",
|
39 |
+
outputs=gr.Label(num_top_classes=2),
|
40 |
+
title="ππ«π₯ Food or Not Food Text Classifier",
|
41 |
+
description=description,
|
42 |
+
examples=[["I whipped up a fresh batch of code, but it to seems to have a syntax error"],
|
43 |
+
["A plate of pancakes and strawberry icing"]]
|
44 |
+
)
|
45 |
+
|
46 |
+
# 4. Launch the interface
|
47 |
+
if __name__ == "__main__":
|
48 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
transformers
|