mdarefin commited on
Commit
97dc5ff
Β·
verified Β·
1 Parent(s): fbf8536

Uploading food not food text classifier demo app.py

Browse files
Files changed (3) hide show
  1. README.md +13 -6
  2. app.py +62 -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: yellow
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 5.2.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
+ sdk_version: 5.0.2
8
  app_file: app.py
9
+ pinned: true
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/Adnan-edu/hugging_custom_ai_model).
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # 1. Import the required packages
3
+ import torch
4
+ import gradio as gr
5
+
6
+ from typing import Dict
7
+ from transformers import pipeline
8
+
9
+ # 2. Define our function to use with our model.
10
+
11
+ def set_device():
12
+ if torch.cuda.is_available():
13
+ device = torch.device("cuda")
14
+ elif torch.backends.mps.is_available() and torch.backends.mps.is_built():
15
+ device = torch.device("mps")
16
+ else:
17
+ device = torch.device("cpu")
18
+ return device
19
+
20
+ DEVICE = set_device()
21
+
22
+ # 1. Create a function to take a String input
23
+
24
+ def food_not_food_classifier(text: str) -> Dict[str, float]:
25
+ # Setup food not food text classifier
26
+ food_not_food_classifier_pipeline = pipeline(task="text-classification",
27
+ model="mdarefin/learn_hf_food_not_food_text_classifier-distilbert-base-uncased",
28
+ batch_size=32,
29
+ device=DEVICE,
30
+ top_k=None) # top_k = None => Return all possible labels
31
+
32
+ # Get the outputs from our pipeline
33
+ outputs = food_not_food_classifier_pipeline(text)[0]
34
+
35
+ # Format output from Gradio
36
+
37
+ output_dict = {}
38
+ for item in outputs:
39
+ output_dict[item["label"]] = item["score"]
40
+
41
+ return output_dict
42
+
43
+ # 3. Create a Gradio interface with details about our app
44
+ description = """
45
+ A text classifier to determine if a sentence is about food or not food.
46
+
47
+ 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).
48
+
49
+ See [source code](https://github.com/Adnan-edu/hugging_custom_ai_model).
50
+ """
51
+
52
+ demo = gr.Interface(fn=food_not_food_classifier,
53
+ inputs="text",
54
+ outputs=gr.Label(num_top_classes=2), # show top 2 classes
55
+ title="πŸ—πŸš«πŸ₯‘ Food or Not Food Text Classifier",
56
+ description=description,
57
+ examples=[["I whipped up a fresh batch of code, but it seems to have a syntax error."],
58
+ ["A delicious photo of a plate of scrambled eggs, bacon and toast."]])
59
+
60
+ # 4. Launch the interface
61
+ if __name__ == "__main__":
62
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio==5.0.2
2
+ torch
3
+ transformers==4.45.2