Spaces:
Runtime error
Runtime error
RuBERT
Browse files
app.py
CHANGED
|
@@ -1,39 +1,32 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig, BitsAndBytesConfig
|
| 4 |
|
| 5 |
-
model_name = "
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
llm_int8_enable_fp32_cpu_offload=True
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
"transformer.wte": "cpu",
|
| 19 |
-
"transformer.wpe": "cpu",
|
| 20 |
-
"transformer.h": "cpu",
|
| 21 |
-
"transformer.ln_f": "cpu",
|
| 22 |
-
"lm_head": "cpu",
|
| 23 |
-
}
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
{"role": "user", "content": "Докажи теорему о неподвижной точке"}
|
| 30 |
-
]
|
| 31 |
-
input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
| 32 |
-
outputs = model.generate(input_tensor.to(model.device))
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
-
gr.Textbox(
|
| 38 |
|
| 39 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
+
model_name = "DeepPavlov/rubert-base-cased"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 8 |
|
| 9 |
+
texts = [
|
| 10 |
+
"Фильм был просто потрясающий, мне понравилось!",
|
| 11 |
+
"Это худший сервис, который я видел.",
|
| 12 |
+
"Нейтральное сообщение без эмоций."
|
| 13 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
inputs = tokenizer(texts, padding=True, truncation=True, return_tensors="pt", max_length=512)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
outputs = model(**inputs)
|
| 19 |
+
predictions = torch.softmax(outputs.logits, dim=1)
|
| 20 |
|
| 21 |
+
labels = ["negative", "neutral", "positive"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
for text, pred in zip(texts, predictions):
|
| 24 |
+
print(f"Текст: {text}")
|
| 25 |
+
for i, score in enumerate(pred):
|
| 26 |
+
print(f"{labels[i]}: {score:.4f}")
|
| 27 |
+
print("---")
|
| 28 |
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
+
gr.Textbox("СМОТРИ ЕПТА ЛОГИ", label="Вывод строки", interactive=False)
|
| 31 |
|
| 32 |
demo.launch()
|