File size: 1,327 Bytes
3839ac0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b13982
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
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import streamlit as st
import torch


MODEL_PATH = "rubert2"
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
model.eval()

# === Streamlit UI ===
st.set_page_config(page_title="Оценка токсичности", layout="centered")
st.title("💬 Оценка токсичности текста")

text = st.text_area("Введите сообщение", "Ты ужасный человек!")
submit = st.button("Проверить токсичность")

if submit and text.strip():
    # Токенизация
    inputs = tokenizer(text, return_tensors="pt", truncation=True)

    # Предсказание
    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits
        score = torch.sigmoid(logits).item()  # степень токсичности

    # Вывод
    st.subheader("Результат:")
    st.write(f"**Степень токсичности:** `{score:.3f}`")

    if score > 0.8:
        st.error("⚠️ Высокая токсичность!")
    elif score > 0.4:
        st.warning("⚠️ Средняя токсичность")
    else:
        st.success("✅ Низкая токсичность")