dream

Форк
0
77 строк · 2.3 Кб
1
import logging
2
import time
3
import os
4

5
import sentry_sdk
6
from catboost import CatBoostClassifier
7
from flask import Flask, request, jsonify
8
from score import get_features
9

10
sentry_sdk.init(os.getenv("SENTRY_DSN"))
11

12
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
13
logger = logging.getLogger(__name__)
14
logging.getLogger("werkzeug").setLevel("WARNING")
15

16
app = Flask(__name__)
17

18

19
def get_probas(contexts, hypotheses):
20
    features = get_features(contexts, hypotheses)
21
    pred = cb.predict_proba(features)[:, 1]
22
    return pred
23

24

25
try:
26
    cb = CatBoostClassifier()
27
    cb.load_model("model-confidence-convert-old_midas.cbm")
28
    contexts = [
29
        [
30
            "i'm good how are you",
31
            "Spectacular, by all reports! Do you want to know what I can do?",
32
            "absolutely",
33
            "I'm a socialbot, and I'm all about chatting with people like you. "
34
            "I can answer questions, share fun facts, discuss movies, books and news. What do you want to talk about?",
35
            "let's talk about movies",
36
        ]
37
    ]
38
    hypotheses = [
39
        {
40
            "is_best": True,
41
            "text": "Kong: Skull Island is a good action movie. What do you think about it?",
42
            "confidence": 1.0,
43
            "convers_evaluator_annotator": {
44
                "isResponseOnTopic": 0.505,
45
                "isResponseErroneous": 0.938,
46
                "responseEngagesUser": 0.344,
47
                "isResponseInteresting": 0.084,
48
                "isResponseComprehensible": 0.454,
49
            },
50
        }
51
    ]
52
    get_probas(contexts, hypotheses)
53
except Exception as e:
54
    logger.exception("Scorer not loaded")
55
    sentry_sdk.capture_exception(e)
56
    raise e
57

58

59
@app.route("/batch_model", methods=["POST"])
60
def batch_respond():
61
    st_time = time.time()
62
    contexts = request.json["contexts"]
63
    hypotheses = request.json["hypotheses"]
64

65
    try:
66
        responses = get_probas(contexts, hypotheses).tolist()
67
    except Exception as e:
68
        responses = [0] * len(hypotheses)
69
        sentry_sdk.capture_exception(e)
70
        logger.exception(e)
71

72
    logging.warning(f"hypothesis_scorer exec time {time.time() - st_time}")
73
    return jsonify([{"batch": responses}])
74

75

76
if __name__ == "__main__":
77
    app.run(debug=False, host="0.0.0.0", port=3000)
78

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.