manually call model
Browse files
app.py
CHANGED
@@ -1,19 +1,16 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
3 |
-
import
|
|
|
4 |
|
5 |
@st.cache_resource()
|
6 |
def load_model():
|
7 |
-
pipe = pipeline(
|
8 |
-
"text-classification",
|
9 |
-
model="alxvlsv/rubert-emotions",
|
10 |
-
tokenizer="alxvlsv/rubert-emotions",
|
11 |
-
top_k=None
|
12 |
-
)
|
13 |
tokenizer = AutoTokenizer.from_pretrained("alxvlsv/rubert-emotions")
|
14 |
-
|
|
|
15 |
|
16 |
model, tokenizer = load_model()
|
|
|
17 |
|
18 |
label_map = {
|
19 |
"LABEL_0": "admiration",
|
@@ -83,9 +80,23 @@ st.write("Введите текст на русском, и мы покажем,
|
|
83 |
text_input = st.text_area("Введите текст здесь:")
|
84 |
|
85 |
if text_input:
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
# Нормализуем вероятности до 100%
|
91 |
total_score = sum(r["score"] for r in results)
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import torch
|
4 |
+
import torch.nn.functional as F
|
5 |
|
6 |
@st.cache_resource()
|
7 |
def load_model():
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
tokenizer = AutoTokenizer.from_pretrained("alxvlsv/rubert-emotions")
|
9 |
+
model = AutoModelForSequenceClassification.from_pretrained("alxvlsv/rubert-emotions")
|
10 |
+
return model, tokenizer
|
11 |
|
12 |
model, tokenizer = load_model()
|
13 |
+
model.eval()
|
14 |
|
15 |
label_map = {
|
16 |
"LABEL_0": "admiration",
|
|
|
80 |
text_input = st.text_area("Введите текст здесь:")
|
81 |
|
82 |
if text_input:
|
83 |
+
inputs = tokenizer(
|
84 |
+
text_input,
|
85 |
+
max_length=256,
|
86 |
+
truncation=True,
|
87 |
+
return_tensors="pt"
|
88 |
+
)
|
89 |
+
|
90 |
+
with torch.no_grad():
|
91 |
+
outputs = model(**inputs)
|
92 |
+
probs = F.softmax(outputs.logits, dim=1)[0]
|
93 |
+
|
94 |
+
results = []
|
95 |
+
for i, score in enumerate(probs):
|
96 |
+
results.append({
|
97 |
+
"label": f"LABEL_{i}",
|
98 |
+
"score": score.item()
|
99 |
+
})
|
100 |
|
101 |
# Нормализуем вероятности до 100%
|
102 |
total_score = sum(r["score"] for r in results)
|