Views
No views yet
pip install transformers torch1
2from transformers import BertForSequenceClassification, BertTokenizer
3import torch
4device = 'cuda' if torch.cuda.is_available() else 'cpu'
5
6# Load quantized model
7model_name = "AventIQ-AI/sentiment_analysis_for_customer_feedback"
8model = BertForSequenceClassification.from_pretrained(model_name).to(device)
9tokenizer = BertTokenizer.from_pretrained(model_name)
10
11#Function to make analysis
12def predict_sentiment(text):
13 # Tokenize input text
14 inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
15
16 # Move tensors to GPU if available
17 inputs = {key: val.to(device) for key, val in inputs.items()}
18
19 # Get model prediction
20 with torch.no_grad():
21 outputs = model(**inputs)
22
23 # Get predicted class
24 logits = outputs.logits
25 predicted_class = torch.argmax(logits, dim=1).item()
26
27 # Map back to sentiment labels
28 sentiment_labels = {0: "Negative", 1: "Neutral", 2: "Positive"}
29 return sentiment_labels[predicted_class]
30
31# Define a test sentence
32test_sentence = "Spending time with family always brings me so much joy."
33print(f"Predicted Sentiment: {predict_sentiment(text)}").
├── model/ # Contains the quantized model files
├── tokenizer_config/ # Tokenizer configuration and vocabulary files
├── model.safensors/ # Fine Tuned Model
├── README.md # Model documentation