File size: 2,654 Bytes
aa23652
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from flask import Flask, request, jsonify
from transformers import pipeline
import threading

app = Flask(__name__)

CANDIDATE_LABELS = [
    'programming',
    'health_and_fitness',
    'travel',
    'food_and_cooking',
    'music',
    'sports',
    'fashion',
    'art_and_design',
    'business_and_entrepreneurship',
    'education',
    'photography',
    'gaming',
    'science_and_technology',
    'parenting',
    'politics',
    'environment_and_sustainability',
    'beauty_and_skincare',
    'literature',
]

classifier_lock = threading.Lock()
classifier = None


def initialize_classifier():
    global classifier
    try:
        classifier = pipeline(
            "zero-shot-classification",
            model="facebook/bart-large-mnli",
            framework="pt",
            batch_size=4,
        )
        print("Classifier initialized successfully")
    except Exception as e:
        print(f"Classifier initialization failed: {e}")


def get_classifier():
    global classifier
    if classifier is None:
        with classifier_lock:
            if classifier is None:
                initialize_classifier()
    return classifier


@app.route('/', methods=['GET'])
def index():
    return jsonify({
        "response": {
            "status": 200,
            "statusText": "OK"
        }
    })


@app.route('/classify', methods=['POST'])
def classify():
    try:
        data = request.json

        if 'text' not in data or data['text'] == "":
            raise ValueError("Text is required")

        text = data['text']

        classifier = get_classifier()
        result = classifier(text, CANDIDATE_LABELS)

        labels = result['labels']
        scores = result['scores']

        returnData = {
            "response": {
                "statusText": "OK",
                "status": 200,
                "categories": []
            }
        }

        for label, score in zip(labels, scores):
            category = {
                "label": label.capitalize().replace("_", " "),
                "score": score
            }

            returnData["response"]["categories"].append(category)

        formattedReturnData = {
            "response": {
                "categories": sorted(returnData["response"]["categories"], key=lambda x: x["score"], reverse=True)
            }
        }

        return jsonify(formattedReturnData)
    except Exception as e:
        return jsonify({
            "response": {
                "status": 500,
                "statusText": str(e)
            }
        })


if __name__ == '__main__':
    initialize_classifier()
    app.run(host='0.0.0.0', port=5000, debug=True)