Uploading food not food text classifier demo app.py
Browse files- README.md +12 -6
- app.py +41 -0
- requirements.txt +3 -0
README.md
CHANGED
@@ -1,12 +1,18 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.44.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Food not Food Text Classifier app
|
3 |
+
emoji: ππ«π₯
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: red
|
6 |
sdk: gradio
|
|
|
7 |
app_file: app.py
|
8 |
pinned: false
|
9 |
+
license: apache-2.0
|
10 |
---
|
11 |
|
12 |
+
# Food Not Food Text Classifier
|
13 |
+
|
14 |
+
Small demo to showcase a text classifier to determine if a sentence is about food or not food. (Completed the course on 4/10/2024)
|
15 |
+
|
16 |
+
DistillBERT model fine-tuned on a small synthetic dataset of 250 generated [Food or Not Food image captions](https://huggingface.co/datasets/mrdbourke/learn_hf_food_not_food_image_captions).
|
17 |
+
|
18 |
+
[Source code notebook](https://github.com/mrdbourke/learn-huggingface/blob/main/notebooks/hugging_face_text_classification_tutorial.ipynb).
|
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
from typing import TypedDict
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
# Create function to use our model on given text
|
9 |
+
def food_not_food_classifier(text: str) -> Dict[str, float]:
|
10 |
+
food_not_food_classifier = pipeline(task="text-classifcation",
|
11 |
+
# Because our model is on Hugging Face already, we can pass in the model name directly
|
12 |
+
model="mrdbourke/learn_hf_food_not_food_text_classifier-distilbert-base-uncased",
|
13 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
14 |
+
top_k=None
|
15 |
+
)
|
16 |
+
outputs = food_not_food_classifier(text)[0]
|
17 |
+
|
18 |
+
output_dict = {}
|
19 |
+
for item in outputs:
|
20 |
+
output_dict[item["label"]] = item["score"]
|
21 |
+
|
22 |
+
return output_dict
|
23 |
+
|
24 |
+
description = """
|
25 |
+
A text classifier to determine if a sentence is about food or not food.
|
26 |
+
|
27 |
+
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).
|
28 |
+
|
29 |
+
See [source code](https://github.com/mrdbourke/learn-huggingface/blob/main/notebooks/hugging_face_text_classification_tutorial.ipynb).
|
30 |
+
"""
|
31 |
+
|
32 |
+
demo = gr.Interface(fn=food_not_food_classifier,
|
33 |
+
inputs="text",
|
34 |
+
outputs=gr.Label(num_top_classes=2),
|
35 |
+
title="ππ«π₯ Food or Not Food Text Classifier",
|
36 |
+
description=description,
|
37 |
+
examples=[["I whipped up a fresh batch of code, but it seems to have a syntax error."],
|
38 |
+
["A delicious photo of a plate of scrambled eggs, bacon and toast."]])
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
transformers
|