mrdbourke commited on
Commit
02e73ec
β€’
1 Parent(s): e86d2af

Uploading food not food text classifier demo app.py

Browse files
Files changed (3) hide show
  1. README.md +12 -5
  2. app.py +38 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,19 @@
1
  ---
2
- title: Learn Hf Food Not Food Text Classifier Demo
3
- emoji: πŸ“ˆ
4
- colorFrom: gray
5
- colorTo: green
6
  sdk: gradio
7
  sdk_version: 4.36.1
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
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