astart01 commited on
Commit
9d14ef2
·
verified ·
1 Parent(s): 3cea361

Update bert.py

Browse files
Files changed (1) hide show
  1. bert.py +33 -33
bert.py CHANGED
@@ -2,36 +2,36 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
2
  import streamlit as st
3
  import torch
4
 
5
-
6
- MODEL_PATH = "rubert2"
7
- model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
8
- tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
9
- model.eval()
10
-
11
- # === Streamlit UI ===
12
- st.set_page_config(page_title="Оценка токсичности", layout="centered")
13
- st.title("💬 Оценка токсичности текста")
14
-
15
- text = st.text_area("Введите сообщение", "Ты ужасный человек!")
16
- submit = st.button("Проверить токсичность")
17
-
18
- if submit and text.strip():
19
- # Токенизация
20
- inputs = tokenizer(text, return_tensors="pt", truncation=True)
21
-
22
- # Предсказание
23
- with torch.no_grad():
24
- outputs = model(**inputs)
25
- logits = outputs.logits
26
- score = torch.sigmoid(logits).item() # степень токсичности
27
-
28
- # Вывод
29
- st.subheader("Результат:")
30
- st.write(f"**Степень токсичности:** `{score:.3f}`")
31
-
32
- if score > 0.8:
33
- st.error("⚠️ Высокая токсичность!")
34
- elif score > 0.4:
35
- st.warning("⚠️ Средняя токсичность")
36
- else:
37
- st.success("✅ Низкая токсичность")
 
2
  import streamlit as st
3
  import torch
4
 
5
+ def run():
6
+ MODEL_PATH = "rubert2"
7
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
8
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
9
+ model.eval()
10
+
11
+ # === Streamlit UI ===
12
+ st.set_page_config(page_title="Оценка токсичности", layout="centered")
13
+ st.title("💬 Оценка токсичности текста")
14
+
15
+ text = st.text_area("Введите сообщение", "Ты ужасный человек!")
16
+ submit = st.button("Проверить токсичность")
17
+
18
+ if submit and text.strip():
19
+ # Токенизация
20
+ inputs = tokenizer(text, return_tensors="pt", truncation=True)
21
+
22
+ # Предсказание
23
+ with torch.no_grad():
24
+ outputs = model(**inputs)
25
+ logits = outputs.logits
26
+ score = torch.sigmoid(logits).item() # степень токсичности
27
+
28
+ # Вывод
29
+ st.subheader("Результат:")
30
+ st.write(f"**Степень токсичности:** `{score:.3f}`")
31
+
32
+ if score > 0.8:
33
+ st.error("⚠️ Высокая токсичность!")
34
+ elif score > 0.4:
35
+ st.warning("⚠️ Средняя токсичность")
36
+ else:
37
+ st.success("✅ Низкая токсичность")