File size: 3,397 Bytes
968f79d
 
386ee45
 
 
 
 
968f79d
 
386ee45
 
 
 
 
 
 
 
 
 
 
 
 
968f79d
386ee45
 
968f79d
 
 
 
 
386ee45
968f79d
386ee45
 
 
 
 
 
968f79d
386ee45
 
 
 
 
968f79d
 
 
 
386ee45
 
 
 
 
 
 
 
 
 
 
bb02a23
386ee45
 
 
 
 
 
 
968f79d
386ee45
 
 
 
968f79d
386ee45
 
 
 
 
 
 
968f79d
386ee45
 
 
 
 
 
 
 
 
 
 
968f79d
386ee45
 
 
 
08b3bdf
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
import uuid
from flask import Flask, render_template, request, session
from modules.db import init_db, save_message, get_recent
from modules.hf_helpers import hf_infer
from modules.helpers import detect_lang, MODELS, LABELS, update_history, bar

app = Flask(__name__)
app.secret_key = "seed-string"

init_db()

COLOR_MAP = {
    "POSITIVE": "green",
    "NEGATIVE": "red",
    "NEUTRAL": "orange",
    "ПОЗИТИВНЫЙ": "green",
    "НЕГАТИВНЫЙ": "red",
    "НЕЙТРАЛЬНЫЙ": "orange",
    "Emotion": "blue",
    "Toxicity": "purple"
}


@app.route("/", methods=["GET", "POST"])
def index():
    
    if "user_id" not in session:
        session["user_id"] = str(uuid.uuid4())
    user_id = session["user_id"]

    result = None

    if request.method == "POST":
        text = request.form.get("text", "").strip()
        if text:
            lang = detect_lang(text)
            labels = LABELS[lang]

            # Sentiment
            sentiment_model = MODELS["ru_sentiment"] if lang == "ru" else MODELS["en_sentiment"]
            sentiment_data = hf_infer(sentiment_model, text)
            sentiment_result = None
            if sentiment_data:
                top_raw_label = max(sentiment_data, key=lambda x: x["score"])
                sentiment = labels.get(
                    top_raw_label["label"].lower() if lang == "ru" else top_raw_label["label"],
                    top_raw_label["label"]
                ).upper()
                score = top_raw_label["score"]
                sentiment_result = {
                    "name": sentiment,
                    "score": int(score * 100),
                    "bar": bar(score),
                    "color": COLOR_MAP.get(sentiment, "black")
                }
                save_message(user_id, text, sentiment, score)

            pos_count = update_history(user_id, sentiment_result["name"] if sentiment_result else "")

            # Emotion (EN only)
            emotion_result = None
            if lang == "en":
                emo_data = hf_infer(MODELS["emotion"], text)
                if emo_data:
                    top_emo = max(emo_data, key=lambda x: x["score"])
                    emotion_result = {
                        "name": top_emo["label"].capitalize(),
                        "score": int(top_emo["score"] * 100),
                        "bar": bar(top_emo["score"]),
                        "color": COLOR_MAP["Emotion"]
                    }

            # Toxicity (EN only)
            tox_result = None
            if lang == "en":
                tox_data = hf_infer(MODELS["toxicity"], text)
                if tox_data:
                    top_tox = max(tox_data, key=lambda x: x["score"])
                    tox_result = {
                        "name": top_tox["label"],
                        "score": int(top_tox["score"] * 100),
                        "bar": bar(top_tox["score"]),
                        "color": COLOR_MAP["Toxicity"]
                    }

            result = {
                "sentiment": sentiment_result,
                "emotion": emotion_result,
                "toxicity": tox_result,
                "pos_count": pos_count
            }

    history = get_recent(user_id, 10)
    return render_template("index.html", result=result, history=history)


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860, debug=False)