File size: 2,668 Bytes
b5815d0
b2bd29c
b5815d0
b2bd29c
 
b5815d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# HP BERT Intent Classification Model

This model is fine-tuned BERT for classifying different types of queries in the HP documentation context.

## Model Details
- Base model: bert-base-uncased
- Task: 3-class classification
- Classes:
    - 0: Queries requiring PDF context
    - 1: Summary-related queries
    - 2: Metadata-related queries

## Usage
```python
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch

class BertInference:
    def __init__(self, model_path):
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.model = AutoModelForSequenceClassification.from_pretrained(model_path).to(self.device)
        self.tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
        self.template = "Question: {} Response: "
        self.label_map = {
            0: "query_with_pdf",
            1: "summarize_pdf",
            2: "query_metadata"
        }

    def predict(self, text):
        # Format the input text
        formatted_text = self.template.format(text)
        
        # Tokenize
        inputs = self.tokenizer(
            formatted_text,
            truncation=True,
            max_length=512,
            padding='max_length',
            return_tensors="pt"
        ).to(self.device)

        # Get prediction
        with torch.no_grad():
            outputs = self.model(**inputs)
            predictions = torch.softmax(outputs.logits, dim=1)
            predicted_class = torch.argmax(predictions, dim=1).item()
            confidence = predictions[0][predicted_class].item()

        return {
            "predicted_class": self.label_map[predicted_class],
            "confidence": confidence,
            "all_probabilities": {
                self.label_map[i]: prob.item()
                for i, prob in enumerate(predictions[0])
            }
        }

def main():
    # Initialize the model
    model_path = "output_dir_decision"  # Path to your saved model
    inferencer = BertInference(model_path)

    # Example usage
    test_questions = [
        "What are the new features in corolla cross?",
        "What is the summary of the provided pdf?",
        "The filesize of the pdf is?",
    ]

    for question in test_questions:
        result = inferencer.predict(question)
        print(f"\nQuestion: {question}")
        print(f"Predicted Class: {result['predicted_class']}")
        print(f"Confidence: {result['confidence']:.4f}")
        print("All Probabilities:")
        for class_name, prob in result['all_probabilities'].items():
            print(f"  {class_name}: {prob:.4f}")

if __name__ == "__main__":
    main()
```