Suraj-Yadav commited on
Commit
f141439
β€’
1 Parent(s): c89b257

Uploading food not food text classifier demo app.py

Browse files
Files changed (3) hide show
  1. README.md +10 -3
  2. app.py +97 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,19 @@
1
  ---
2
  title: Food Not Food Text Classifier
3
- emoji: 🐒
4
- colorFrom: pink
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 4.44.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.44.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
+ [Source code notebook](https://github.com/mrdbourke/learn-huggingface/blob/main/notebooks/hugging_face_text_classification_tutorial.ipynb).
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ huggingface_model_path = "Suraj-Yadav/learn_hf_food_not_food_text_classifier-distilbert-base-uncased"
9
+
10
+ # 2. Define function to use our model on given text
11
+ def food_not_food_classifier(
12
+ text: Union[str, list],
13
+ model_path: str,
14
+ batch_size: int = 32,
15
+ device: str = None,
16
+ get_classifier:bool = False
17
+ ) -> Dict[str, float]:
18
+ """
19
+ Classifies whether the given text is related to food or not, returning a dictionary of labels and their scores.
20
+
21
+ Args:
22
+ text (Union[str, list]): The input text or list of texts to classify.
23
+ model_path (str): The path to the Hugging Face model for classification.
24
+ batch_size (int): The batch size for processing. Default is 32.
25
+ device (str): The device to run inference on (e.g., 'cuda', 'cpu'). Default is None (auto-detect best available).
26
+
27
+ Returns:
28
+ Dict[str, float]: A dictionary where the keys are the labels and the values are the classification scores.
29
+ """
30
+
31
+ if device is None:
32
+ device = set_device()
33
+
34
+ classifier = pipeline(
35
+ task="text-classification",
36
+ model=model_path,
37
+ batch_size=batch_size,
38
+ device=device,
39
+ top_k=None # Keep all predictions
40
+ )
41
+
42
+ if get_classifier:
43
+ return classifier
44
+ else:
45
+
46
+ results = classifier(text) # [[{'label': 'food', 'score': 0.9500328898429871}, {'label': 'not_food', 'score': 0.04996709153056145}]]
47
+
48
+ output_dict = {}
49
+ for output in results[0]:
50
+ output_dict[output['label']] = output['score']
51
+
52
+ return output_dict
53
+
54
+
55
+ def gradio_food_classifier(text: str) -> dict:
56
+ """
57
+ A wrapper function for Gradio to classify text using the classify_food_text function.
58
+
59
+ Args:
60
+ text (str): The input text to classify.
61
+
62
+ Returns:
63
+ dict: Classification results as a dictionary of label and score.
64
+ """
65
+ classifier = food_not_food_classifier(text=text,
66
+ model_path=huggingface_model_path,
67
+ get_classifier=True)
68
+
69
+ results = classifier(text)
70
+
71
+ output_dict = {}
72
+ for output in results[0]:
73
+ output_dict[output['label']] = output['score']
74
+
75
+ return output_dict
76
+
77
+
78
+ # 3. Create a Gradio interface with details about our app
79
+ description = """
80
+ A text classifier to determine if a sentence is about food or not food.
81
+
82
+ 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).
83
+
84
+ See [source code](https://github.com/mrdbourke/learn-huggingface/blob/main/notebooks/hugging_face_text_classification_tutorial.ipynb).
85
+ """
86
+
87
+ demo = gr.Interface(fn=gradio_food_classifier,
88
+ inputs="text",
89
+ outputs=gr.Label(num_top_classes=2),
90
+ title="πŸ—πŸš«πŸ₯‘ Food or Not Food Text Classifier",
91
+ description=description,
92
+ examples=[["I whipped up a fresh batch of code, but it seems to have a syntax error."],
93
+ ["A delicious photo of a plate of scrambled eggs, bacon and toast."]])
94
+
95
+ # 4. Launch the interface
96
+ if __name__ == "__main__":
97
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ torch
3
+ transformers