File size: 13,691 Bytes
e18c603
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env python3
"""
Multi-Head QA Metrics Inference Script
=====================================

This script loads a trained multi-head QA classification model and provides
inference capabilities for evaluating call center transcripts against various
QA metrics including opening, listening, proactiveness, resolution, hold, and closing.

Usage:
    python inference.py --model_path "path/to/model" --text "transcript text"
    
    Or use the interactive mode:
    python inference.py --model_path "path/to/model" --interactive
"""

import os
import torch
import torch.nn as nn
import numpy as np
import argparse
import json
from typing import Dict, List, Optional
from transformers import DistilBertTokenizer, DistilBertModel, AutoConfig, DistilBertPreTrainedModel
from transformers.modeling_outputs import SequenceClassifierOutput


# QA Heads Configuration - must match training configuration
QA_HEADS_CONFIG = {
    "opening": 1,
    "listening": 5,
    "proactiveness": 3,
    "resolution": 5,
    "hold": 2,
    "closing": 1
}

# Submetric labels for better output interpretation
HEAD_SUBMETRIC_LABELS = {
    "opening": [
        "Use of call opening phrase"
    ],
    "listening": [
        "Caller was not interrupted",
        "Empathizes with the caller",
        "Paraphrases or rephrases the issue",
        "Uses 'please' and 'thank you'",
        "Does not hesitate or sound unsure"
    ],
    "proactiveness": [
        "Willing to solve extra issues",
        "Confirms satisfaction with action points",
        "Follows up on case updates"
    ],
    "resolution": [
        "Gives accurate information",
        "Correct language use",
        "Consults if unsure",
        "Follows correct steps",
        "Explains solution process clearly"
    ],
    "hold": [
        "Explains before placing on hold",
        # "Provides status update after hold",
        "Thanks caller for holding"
    ],
    "closing": [
        "Proper call closing phrase used"
    ]
}


class MultiHeadQAClassifier(DistilBertPreTrainedModel):
    """
    Multi-head QA classifier model for call center transcript evaluation.
    Each head corresponds to a different QA metric.
    """
    
    def __init__(self, config):
        super().__init__(config)
        
        # Get heads config from model config
        self.heads_config = getattr(config, 'heads_config', {
            "opening": 1,
            "listening": 5,
            "proactiveness": 3,
            "resolution": 5,
            "hold": 2,
            "closing": 1
        })
        
        self.bert = DistilBertModel(config)
        classifier_dropout = getattr(config, 'classifier_dropout', 0.2)
        self.dropout = nn.Dropout(classifier_dropout)

        # Multiple heads, one per QA metric
        self.heads = nn.ModuleDict({
            head: nn.Linear(config.hidden_size, output_dim)
            for head, output_dim in self.heads_config.items()
        })
        
        # Initialize weights
        self.post_init()

    def forward(
        self,
        input_ids: Optional[torch.Tensor] = None,
        attention_mask: Optional[torch.Tensor] = None,
        labels: Optional[Dict[str, torch.Tensor]] = None,
        **kwargs
    ):
        outputs = self.bert(
            input_ids=input_ids,
            attention_mask=attention_mask,
            **kwargs
        )
        
        pooled_output = self.dropout(outputs.last_hidden_state[:, 0])  # [CLS]

        logits = {}
        losses = {}
        loss_total = 0

        for head_name, head_layer in self.heads.items():
            out = head_layer(pooled_output)
            logits[head_name] = torch.sigmoid(out)  # probabilities

            if labels is not None and head_name in labels:
                loss_fn = nn.BCEWithLogitsLoss()
                loss = loss_fn(out, labels[head_name])
                losses[head_name] = loss.item()
                loss_total += loss

        return {
            "logits": logits,
            "loss": loss_total if labels is not None else None,
            "losses": losses if labels is not None else None
        }


class QAMetricsInference:
    """
    Inference class for QA metrics prediction on call center transcripts.
    """
    
    def __init__(self, model_path: str, device: Optional[str] = None):
        """
        Initialize the inference engine. 
        
        Args:
            model_path: Path to the saved model directory
            device: Device to run inference on ('cpu', 'cuda', or None for auto-detect)
        """
        self.model_path = model_path
        self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
        self.max_length = 512
        
        # Load tokenizer and model
        self._load_model()
        
    def _load_model(self):
        """Load the trained model and tokenizer."""
        print(f"Loading model from: {self.model_path}")
        
        # Load tokenizer
        try:
            self.tokenizer = DistilBertTokenizer.from_pretrained(self.model_path)
            print("βœ“ Tokenizer loaded successfully")
        except Exception as e:
            print(f"βœ— Error loading tokenizer: {e}")
            raise
        
        # Load model
        try:
            if os.path.isdir(self.model_path):
                # Load from local directory
                config = AutoConfig.from_pretrained(self.model_path)
                self.model = MultiHeadQAClassifier(config)
                model_state_path = os.path.join(self.model_path, "pytorch_model.bin")
                
                if not os.path.exists(model_state_path):
                    raise FileNotFoundError(f"Model file not found: {model_state_path}")
                    
                state_dict = torch.load(model_state_path, map_location=self.device)
                self.model.load_state_dict(state_dict)
            else:
                # Load from Hugging Face Hub
                self.model = MultiHeadQAClassifier.from_pretrained(self.model_path)

            self.model.to(self.device)
            self.model.eval()
            print(f"βœ“ Model loaded successfully on {self.device}")
        except Exception as e:
            print(f"βœ— Error loading model: {e}")
            raise
    
    def predict(self, text: str, threshold: float = 0.5, return_raw: bool = False) -> Dict:
        """
        Predict QA metrics for a given transcript.
        
        Args:
            text: Input transcript text
            threshold: Threshold for binary classification (default: 0.5)
            return_raw: If True, return raw probabilities along with predictions
            
        Returns:
            Dictionary containing predictions for each QA head
        """
        # Tokenize input
        encoding = self.tokenizer(
            text,
            return_tensors="pt",
            padding="max_length",
            truncation=True,
            max_length=self.max_length
        )
        
        input_ids = encoding["input_ids"].to(self.device)
        attention_mask = encoding["attention_mask"].to(self.device)
        
        # Forward pass
        with torch.no_grad():
            outputs = self.model(input_ids=input_ids, attention_mask=attention_mask)
            logits = outputs["logits"]
        
        # Process results
        results = {}
        for head, probs in logits.items():
            probs_np = probs.cpu().numpy()[0]  # Get first (and only) example
            preds = (probs_np > threshold).astype(int)
            submetrics = HEAD_SUBMETRIC_LABELS.get(head, [f"Submetric {i+1}" for i in range(len(probs_np))])
            
            head_results = []
            for i, (label, prob, pred) in enumerate(zip(submetrics, probs_np, preds)):
                result_item = {
                    "submetric": label,
                    "prediction": bool(pred),
                    "score": "βœ“" if pred else "βœ—"
                }
                if return_raw:
                    result_item["probability"] = float(prob)
                
                head_results.append(result_item)
            
            results[head] = head_results
        
        return results
    
    def predict_and_display(self, text: str, threshold: float = 0.5):
        """
        Predict and display results in a formatted way. 
        
        Args:
            text: Input transcript text
            threshold: Threshold for binary classification
        """
        print(f"\nπŸ“ž Transcript Analysis")
        print("=" * 60)
        print(f"Text: {text[:200]}{'...' if len(text) > 200 else ''}")
        print("=" * 60)
        
        results = self.predict(text, threshold, return_raw=True)
        
        for head, head_results in results.items():
            print(f"\nπŸ”Ή {head.upper()}:")
            for item in head_results:
                prob = item["probability"]
                print(f"  ➀ {item['submetric']}: P={prob:.3f} β†’ {item['score']}")
    
    def batch_predict(self, texts: List[str], threshold: float = 0.5) -> List[Dict]:
        """
        Predict QA metrics for multiple transcripts.
        
        Args:
            texts: List of transcript texts
            threshold: Threshold for binary classification
            
        Returns:
            List of prediction dictionaries
        """
        results = []
        for text in texts:
            result = self.predict(text, threshold)
            results.append(result)
        return results
    
    def export_predictions(self, texts: List[str], output_path: str, threshold: float = 0.5):
        """
        Export predictions to a JSON file.
        
        Args:
            texts: List of transcript texts
            output_path: Path to save the results
            threshold: Threshold for binary classification
        """
        results = []
        for i, text in enumerate(texts):
            prediction = self.predict(text, threshold, return_raw=True)
            results.append({
                "text_id": i,
                "text": text,
                "predictions": prediction
            })
        
        with open(output_path, 'w', encoding='utf-8') as f:
            json.dump(results, f, indent=2, ensure_ascii=False)
        
        print(f"βœ“ Predictions exported to: {output_path}")


def main():
    """Main function for command-line interface."""
    parser = argparse.ArgumentParser(description="QA Metrics Inference Script")
    parser.add_argument("--model_path", required=True, help="Path to the trained model directory")
    parser.add_argument("--text", help="Text to analyze")
    parser.add_argument("--input_file", help="Path to text file containing transcripts (one per line)")
    parser.add_argument("--output_file", help="Path to save predictions (JSON format)")
    parser.add_argument("--threshold", type=float, default=0.5, help="Classification threshold (default: 0.5)")
    parser.add_argument("--interactive", action="store_true", help="Run in interactive mode")
    parser.add_argument("--device", help="Device to use (cpu/cuda)")
    
    args = parser.parse_args()
    
    # Initialize inference engine
    try:
        inference_engine = QAMetricsInference(args.model_path, args.device)
    except Exception as e:
        print(f"Failed to initialize inference engine: {e}")
        return
    
    # Interactive mode
    if args.interactive:
        print("\nπŸ€– QA Metrics Interactive Analysis")
        print("Type 'quit' to exit, 'help' for commands")
        print("-" * 50)
        
        while True:
            try:
                user_input = input("\nEnter transcript text: ").strip()
                
                if user_input.lower() == 'quit':
                    break
                elif user_input.lower() == 'help':
                    print("\nCommands:")
                    print("  - Enter transcript text to analyze")
                    print("  - 'quit' to exit")
                    print("  - 'help' to show this message")
                    continue
                elif not user_input:
                    print("Please enter some text to analyze.")
                    continue
                
                inference_engine.predict_and_display(user_input, args.threshold)
                
            except KeyboardInterrupt:
                print("\n\nGoodbye! πŸ‘‹")
                break
            except Exception as e:
                print(f"Error during analysis: {e}")
    
    # Single text analysis
    elif args.text:
        inference_engine.predict_and_display(args.text, args.threshold)
    
    # Batch processing from file
    elif args.input_file:
        try:
            with open(args.input_file, 'r', encoding='utf-8') as f:
                texts = [line.strip() for line in f if line.strip()]
            
            print(f"Processing {len(texts)} transcripts...")
            
            if args.output_file:
                inference_engine.export_predictions(texts, args.output_file, args.threshold)
            else:
                results = inference_engine.batch_predict(texts, args.threshold)
                for i, result in enumerate(results):
                    print(f"\n--- Transcript {i+1} ---")
                    print(json.dumps(result, indent=2))
                    
        except FileNotFoundError:
            print(f"Input file not found: {args.input_file}")
        except Exception as e:
            print(f"Error processing file: {e}")
    
    else:
        print("Please provide either --text, --input_file, or use --interactive mode")
        print("Use --help for more information")


if __name__ == "__main__":
    main()