Views
No views yet
pip install transformers torch1
2from transformers import BertForSequenceClassification, BertTokenizer
3import torch
4
5# Load quantized model
6quantized_model_path = "AventIQ-AI/sentiment-analysis-for-patient-reviews-analysis"
7quantized_model = BertForSequenceClassification.from_pretrained(quantized_model_path)
8quantized_model.eval() # Set to evaluation mode
9quantized_model.half() # Convert model to FP16
10
11# Load tokenizer
12tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
13
14# Define a test sentence
15test_sentence = "I recently visited the clinic and had a wonderful experience. The doctor was extremely patient and explained everything in detail, which made me feel comfortable and reassured. However, the waiting time was quite long even though I had a prior appointment, which was a bit frustrating. The hospital premises were clean and well-maintained, and the nurses were attentive and kind. On the downside, the prescribed medication didn’t work for me, and I had to revisit after a week with no improvement in my condition. Despite that, the overall process of scheduling and consultation was smooth and the support staff was courteous."
16
17# Tokenize input
18inputs = tokenizer(test_sentence, return_tensors="pt", padding=True, truncation=True, max_length=128)
19
20# Ensure input tensors are in correct dtype
21inputs["input_ids"] = inputs["input_ids"].long() # Convert to long type
22inputs["attention_mask"] = inputs["attention_mask"].long() # Convert to long type
23
24# Make prediction
25with torch.no_grad():
26 outputs = quantized_model(**inputs)
27
28# Get predicted class
29predicted_class = torch.argmax(outputs.logits, dim=1).item()
30print(f"Predicted Class: {predicted_class}")
31
32
33label_mapping = {0: "very_negative", 1: "nagative", 2: "neutral", 3: "Positive", 4: "very_positive"} # Example
34
35predicted_label = label_mapping[predicted_class]
36print(f"Predicted Label: {predicted_label}")
37.
├── model/ # Contains the quantized model files
├── tokenizer_config/ # Tokenizer configuration and vocabulary files
├── model.safensors/ # Fine Tuned Model
├── README.md # Model documentation