File size: 1,430 Bytes
9189e38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import random
import numpy as np
import torch
from transformers import pipeline

# Load a pre-trained SBERT model

# Set seeds for reproducibility of zero-shot classification
def set_seed(seed):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

set_seed(1)

# Load a pre-trained model and tokenizer
classifier = pipeline("zero-shot-classification", model="roberta-large-mnli")

# Classify item as food or non-food
def classify_as_food_nonfood(item):
    cleaned_item = item.strip().lower()
    result = classifier(cleaned_item, candidate_labels=["food", "non-food"])
    label = result["labels"][0]
    score = result["scores"][0]
    # print(f"Item: {item}, Label: {label}, Score: {score}")
    return label, score

def pessimistic_food_nonfood_score(food_nonfood, similarity_score):
    # For us to truly believe that the word is nonfood, we need to be confident that it is nonfood.
    #
    # Three conditions need to be met:
    # 1. The word must be classified as nonfood
    # 2. The food_nonfood_score must be greater than a threshold

    is_food = food_nonfood[0] == 'food'
    food_nonfood_score = food_nonfood[1]

    if is_food == False and food_nonfood_score >= 0.7:
        is_food = False
    else:
        is_food = True

    return is_food, food_nonfood_score