shivajimallela commited on
Commit
70d20b9
Β·
verified Β·
1 Parent(s): 4e52b69

Uploading food not food text classifier demo app.py

Browse files
Files changed (3) hide show
  1. README.md +12 -6
  2. app.py +49 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,18 @@
1
  ---
2
- title: Learn Hf Food Not Food Text Classifier Demo
3
- emoji: 🏒
4
- colorFrom: green
5
- colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 5.6.0
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
  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.
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,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 function to use our model on given text
9
+ def food_not_food_classifier(text: str) -> Dict[str, float]:
10
+ """
11
+ Takes an input string of text and classifies it into food/not_food in the form of a dictionary.
12
+ """
13
+ # 2. Setup the pipeline to use the local model (or Hugging Face model path)
14
+ food_not_food_classifier = pipeline(task='text-classification',
15
+ model = "shivajimallela/learn_hf_food_not_food_classifier-ditsilbert-base-uncased",
16
+ device= "cuda" if torch.cuda.is_available() else "cpu",
17
+ top_k = None)
18
+
19
+ # 3. Get outputs from pipeline (as a list of dicts)
20
+ outputs = food_not_food_classifier(text)[0]
21
+ # print(food_not_food_classifier(text))
22
+
23
+ # 4. Format output for Gradio (e.g. {"label_1": probability_1, "label_2": probability_2})
24
+ output_dict = {}
25
+ for item in outputs:
26
+ output_dict[item['label']] = item['score']
27
+
28
+ return output_dict
29
+
30
+ # 3. Create a Gradio interface with details about our app
31
+ description = """
32
+ A text classifier to determine if a sentence is about food or not food.
33
+
34
+ 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).
35
+
36
+ See [source code](https://github.com/mrdbourke/learn-huggingface/blob/main/notebooks/hugging_face_text_classification_tutorial.ipynb).
37
+ """
38
+
39
+ demo = gr.Interface(fn=food_not_food_classifier,
40
+ inputs='text',
41
+ outputs=gr.Label(num_top_classes=2),
42
+ title="πŸ˜‹πŸ™…πŸ₯‘ Food or Not Food Text Classifier",
43
+ description=description,
44
+ examples=[['I whipped up a fresh batch of code, but it seems to have a syntax error.'],
45
+ ["A delicious photo of a plate of scrambled eggs, bacon and toast."]])
46
+
47
+ # 4. Launch the interface
48
+ if __name__ == "__main__":
49
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ torch
3
+ transformers