Spaces:
Sleeping
Sleeping
Uploading food not food text classifier demo app.py
Browse files- README.md +12 -5
- app.py +38 -0
- requirements.txt +3 -0
README.md
CHANGED
@@ -1,12 +1,19 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.36.1
|
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.36.1
|
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 |
+
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).
|
18 |
+
|
19 |
+
TK - see the demo notebook on how to create this
|
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
def food_not_food_classifier(text):
|
7 |
+
# Set up text classification pipeline
|
8 |
+
food_not_food_classifier = pipeline(task="text-classification",
|
9 |
+
model="mrdbourke/learn_hf_food_not_food_text_classifier-distilbert-base-uncased",
|
10 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
11 |
+
top_k=None) # return all possible scores (not just top-1)
|
12 |
+
|
13 |
+
# Get outputs from pipeline (as a list of dicts)
|
14 |
+
outputs = food_not_food_classifier(text)[0]
|
15 |
+
|
16 |
+
# Format output for Gradio (e.g. {"label_1": probability_1, "label_2": probability_2})
|
17 |
+
output_dict = {}
|
18 |
+
for item in outputs:
|
19 |
+
output_dict[item["label"]] = item["score"]
|
20 |
+
|
21 |
+
return output_dict
|
22 |
+
|
23 |
+
description = """
|
24 |
+
A text classifier to determine if a sentence is about food or not food.
|
25 |
+
|
26 |
+
TK - See source code:
|
27 |
+
"""
|
28 |
+
|
29 |
+
demo = gr.Interface(fn=food_not_food_classifier,
|
30 |
+
inputs="text",
|
31 |
+
outputs=gr.Label(num_top_classes=2), # show top 2 classes (that's all we have)
|
32 |
+
title="ππ«π₯ Food or Not Food Text Classifier",
|
33 |
+
description=description,
|
34 |
+
examples=[["I whipped up a fresh batch of code, but it seems to have a syntax error."],
|
35 |
+
["A delicious photo of a plate of scrambled eggs, bacon and toast."]])
|
36 |
+
|
37 |
+
if __name__ == "__main__":
|
38 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
transformers
|