mrdbourke commited on
Commit
a17aa18
Β·
verified Β·
1 Parent(s): 12ebbba

Uploading food not food text classifier demo app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -2
app.py CHANGED
@@ -1,11 +1,15 @@
 
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", # link to model on HF Hub
10
  device="cuda" if torch.cuda.is_available() else "cpu",
11
  top_k=None) # return all possible scores (not just top-1)
@@ -20,8 +24,11 @@ def food_not_food_classifier(text):
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
  """
@@ -34,5 +41,6 @@ demo = gr.Interface(fn=food_not_food_classifier,
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()
 
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
  # Set up text classification pipeline
11
  food_not_food_classifier = pipeline(task="text-classification",
12
+ # Because our model is on Hugging Face already, we can pass in the model name directly
13
  model="mrdbourke/learn_hf_food_not_food_text_classifier-distilbert-base-uncased", # link to model on HF Hub
14
  device="cuda" if torch.cuda.is_available() else "cpu",
15
  top_k=None) # return all possible scores (not just top-1)
 
24
 
25
  return output_dict
26
 
27
+ # 3. Create a Gradio interface with details about our app
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) on a [small dataset of food and not food text](https://huggingface.co/datasets/mrdbourke/learn_hf_food_not_food_image_captions).
32
 
33
  TK - See source code:
34
  """
 
41
  examples=[["I whipped up a fresh batch of code, but it seems to have a syntax error."],
42
  ["A delicious photo of a plate of scrambled eggs, bacon and toast."]])
43
 
44
+ # 4. Launch the interface
45
  if __name__ == "__main__":
46
  demo.launch()