Spaces:
Sleeping
Sleeping
File size: 996 Bytes
fee9b9e 616d402 62ecefe e9d4432 62ecefe fee9b9e 62ecefe e9d4432 62ecefe e9d4432 62ecefe fee9b9e 62ecefe | 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 |
from config import model_name
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
def sentiment_analysis(text)->str:
'''принимает строку, возвращает тональность'''
try:
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
with torch.no_grad():
outputs = model(**inputs)
#вероятности классов
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
pred_class = torch.argmax(probabilities, dim=-1).item()
if pred_class <= 1:
return "Негативный"
elif pred_class ==2:
return "Нейтральный"
else:
return "Позитивный"
except Exception as e:
return f"Error: {str(e)}"
|