Spaces:
Sleeping
Sleeping
Uploading food classifier demo
Browse files- README.md +23 -14
- Requirements.txt +4 -0
- app.py +100 -0
README.md
CHANGED
|
@@ -1,14 +1,23 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
title: Food Not Food Text Classifier
|
| 2 |
+
emoji: ππ«π₯
|
| 3 |
+
colorFrom: blue
|
| 4 |
+
colorTo: yellow
|
| 5 |
+
sdk: gradio
|
| 6 |
+
sdk_version: 4.36.1
|
| 7 |
+
app_file: app.py
|
| 8 |
+
pinned: false
|
| 9 |
+
license: apache-2.0
|
| 10 |
+
# ππ«π₯ Food Not Food Text Classifier
|
| 11 |
+
|
| 12 |
+
Small demo to showcase a text classifier to determine if a sentence is about food or not food.
|
| 13 |
+
|
| 14 |
+
library_name: transformers
|
| 15 |
+
license: apache-2.0
|
| 16 |
+
base_model: distilbert/distilbert-base-uncased
|
| 17 |
+
tags:
|
| 18 |
+
- generated_from_trainer
|
| 19 |
+
metrics:
|
| 20 |
+
- accuracy
|
| 21 |
+
model-index:
|
| 22 |
+
- name: learn_hf_food_not_food_text_classfier-distilbert-base-uncased
|
| 23 |
+
results: []
|
Requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Requirements ππ«π₯ Food Not Food Text Classifier
|
| 2 |
+
gradio
|
| 3 |
+
torch
|
| 4 |
+
transformers
|
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Lets Launch Sagar's demo on web
|
| 2 |
+
# YAML metadata looks for app.py by default
|
| 3 |
+
# app.py import packages , define gradio function, create demo, run demo with demo.launch
|
| 4 |
+
# %%writefile magic pyhton function
|
| 5 |
+
# Create Model Demo: Gradio App: inputs - model -output in gradio app
|
| 6 |
+
# Create a function to perform infeence
|
| 7 |
+
# 1 take an input of strings
|
| 8 |
+
# 2 setup a text classsifcation pipeline
|
| 9 |
+
# 3 get the output from piepeline return theoutput from the pipeline in step3 as a formatted dictionary with format:
|
| 10 |
+
# 4 Return the output from the pipeline in step 3 as a formatted dictionary with the format {"label_1": probability_1, "label_2: probability_2"}
|
| 11 |
+
######################################################################################################################
|
| 12 |
+
# Import necessary packages
|
| 13 |
+
import pprint
|
| 14 |
+
from pathlib import Path
|
| 15 |
+
|
| 16 |
+
import numpy as np
|
| 17 |
+
import torch
|
| 18 |
+
|
| 19 |
+
import datasets
|
| 20 |
+
import evaluate
|
| 21 |
+
from typing import Dict, List
|
| 22 |
+
from transformers import pipeline
|
| 23 |
+
from transformers import TrainingArguments, Trainer
|
| 24 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# 1. Provide Hugging face model path copy from hugging face
|
| 28 |
+
|
| 29 |
+
food_not_food_pipeline = pipeline(
|
| 30 |
+
task="text-classification",
|
| 31 |
+
model="Quantum-Monk/learn_hf_food_not_food_text_classfier-distilbert-base-uncased",
|
| 32 |
+
batch_size=32,
|
| 33 |
+
# Use 0 for the first GPU or "cpu"
|
| 34 |
+
device=0 if torch.cuda.is_available() else -1,
|
| 35 |
+
top_k=None # None returns all possible labels
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# 2. Create the function to use the pipeline
|
| 39 |
+
def food_not_food_classifier(text: str) -> List[Dict[str, float]]:
|
| 40 |
+
# 3. Get the outputs from our pipeline
|
| 41 |
+
# The pipeline returns a list of results
|
| 42 |
+
outputs = food_not_food_pipeline(text)[0]
|
| 43 |
+
return outputs
|
| 44 |
+
|
| 45 |
+
# 4. Test the function
|
| 46 |
+
result = food_not_food_classifier(text="Yo we're building a local demo!")
|
| 47 |
+
print(result)
|
| 48 |
+
|
| 49 |
+
#format output for gradio
|
| 50 |
+
import gradio as gr
|
| 51 |
+
|
| 52 |
+
# 1. Update your classifier function to include the formatting logic
|
| 53 |
+
def food_not_food_classifier(text: str):
|
| 54 |
+
# Get the raw output from your pipeline (already defined earlier)
|
| 55 |
+
# This returns a list of dictionaries because top_k=None
|
| 56 |
+
raw_outputs = food_not_food_pipeline(text)[0]
|
| 57 |
+
|
| 58 |
+
# 2. Format the output specifically for Gradio Label component
|
| 59 |
+
output_dict = {}
|
| 60 |
+
for item in raw_outputs:
|
| 61 |
+
output_dict[item["label"]] = item["score"]
|
| 62 |
+
|
| 63 |
+
return output_dict
|
| 64 |
+
|
| 65 |
+
# 3. Create the Gradio interface
|
| 66 |
+
|
| 67 |
+
desciption = "A text classfier to determine if a sentence is about food or not food fine tuner from distilbert HF model and dataset. my personal repo located at https://huggingface.co/Quantum-Monk/learn_hf_food_not_food_text_classfier-distilbert-base-uncased my space https://huggingface.co/spaces/Quantum-Monk/MyWorkSS "
|
| 68 |
+
|
| 69 |
+
demo = gr.Interface(
|
| 70 |
+
fn=food_not_food_classifier,
|
| 71 |
+
inputs="text",
|
| 72 |
+
outputs=gr.Label(num_top_classes=2),
|
| 73 |
+
title="Food Not Food Classifier",
|
| 74 |
+
description="A text classifier to determine if a sentence is about food or not.",
|
| 75 |
+
examples=[
|
| 76 |
+
["I whipped up a fresh batch of code, but it seems to have a syntax error"],
|
| 77 |
+
["A pancake plate of ice cream"]
|
| 78 |
+
]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# Make directory to store our demo
|
| 83 |
+
|
| 84 |
+
from pathlib import Path
|
| 85 |
+
# Make Directory
|
| 86 |
+
demos_dir = Path("../demos")
|
| 87 |
+
demos_dir.mkdir(exist_ok=True)
|
| 88 |
+
|
| 89 |
+
# Create a folder for food_not_food_text_classfier demo
|
| 90 |
+
|
| 91 |
+
food_not_food_text_classifier_demo_dir = Path(demos_dir, "food_not_food_text_classifier")
|
| 92 |
+
food_not_food_text_classifier_demo_dir.mkdir(exist_ok=True)
|
| 93 |
+
|
| 94 |
+
# 4. Sagar HF Launch interface!
|
| 95 |
+
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
demo.launch()
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|