forked from Uplyuz/PolarWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_load_apply.py
More file actions
58 lines (45 loc) · 1.97 KB
/
model_load_apply.py
File metadata and controls
58 lines (45 loc) · 1.97 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
######pass the translated output to the model #####
import torch
from transformers import RobertaTokenizer
from custom_class_final_model import CustomRobertaModel
#call/load model from hugging face
def load_custom_sentiment_model():
try:
model_name = "AleOfDurin/final_retrained_model"
model = CustomRobertaModel.from_pretrained(model_name)
tokenizer = RobertaTokenizer.from_pretrained(model_name)
model.eval() # Ensure the model is in evaluation mode
return model, tokenizer
except Exception as e:
raise RuntimeError(f"Error loading model:{e}") #using st.error in main script for reusability
# Load the model and tokenizer
model_custom, tokenizer_custom = load_custom_sentiment_model()
#tokenize translated df and pass translated df to the model
def predict_sentiment(model, tokenizer, sentence):
"""
Use the custom model to predict sentiment of a given sentence.
"""
# Tokenize the input sentence
inputs = tokenizer(sentence, return_tensors="pt", padding=True, truncation=True)
input_ids = inputs["input_ids"]
attention_mask = inputs["attention_mask"]
# Set model to evaluation mode
model.eval()
# No gradient calculation for inference
with torch.no_grad():
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
logits = outputs
# Get the predicted class (0 or 1)
predictions = torch.argmax(logits, dim=1)
return predictions.item()
def analyze_sentiments(model, tokenizer, df):
label_mapping = {0: "Negative", 1: "Positive"}
sentiments = []
for tweet in df['Tweet']:
# Get the predicted sentiment label
predicted_label = predict_sentiment(model, tokenizer, tweet)
sentiment = label_mapping.get(predicted_label, "Unknown")
sentiments.append(sentiment)
# Add the new column to the DataFrame
df['Sentiment'] = sentiments
return df