Kokush1bo commited on
Commit
dbe2e04
·
verified ·
1 Parent(s): fe13e60

Upload llm_functionality.py

Browse files
Files changed (1) hide show
  1. llm_functionality.py +86 -0
llm_functionality.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
2
+ import torch
3
+ from typing import Literal
4
+
5
+ # Initialize models
6
+ profanity_model_name = "Dabid/abusive-tagalog-profanity-detection"
7
+ privacy_model_name = "roberta-base"
8
+
9
+ # Load profanity model
10
+ profanity_tokenizer = AutoTokenizer.from_pretrained(profanity_model_name)
11
+ profanity_model = AutoModelForSequenceClassification.from_pretrained(profanity_model_name)
12
+
13
+ # Load privacy model
14
+ privacy_tokenizer = AutoTokenizer.from_pretrained(privacy_model_name)
15
+ privacy_model = AutoModelForSequenceClassification.from_pretrained(privacy_model_name)
16
+
17
+ # Use GPU if available
18
+ device = 0 if torch.cuda.is_available() else -1
19
+
20
+ # Create classifiers
21
+ profanity_classifier = pipeline(
22
+ "text-classification",
23
+ model=profanity_model,
24
+ tokenizer=profanity_tokenizer,
25
+ device=device,
26
+ framework="pt"
27
+ )
28
+
29
+ privacy_classifier = pipeline(
30
+ "text-classification",
31
+ model=privacy_model,
32
+ tokenizer=privacy_tokenizer,
33
+ device=device,
34
+ framework="pt"
35
+ )
36
+
37
+
38
+ PROMPT_TEMPLATES = {
39
+ "Profanity Detection": {
40
+ "system": """Detect profanity in debt collection conversations. Flag if the text contains:
41
+ 1. Explicit swear words (e.g., "damn", "hell", "crap").
42
+ 2. Threats ("Pay or else").
43
+ 3. Derogatory terms ("idiot", "fraud").
44
+ 4. Aggressive tone ("Listen up!").
45
+
46
+ Examples:
47
+ - Text: "Pay your damn bill!" → Label: 1
48
+ - Text: "Can we discuss payment options?" → Label: 0
49
+
50
+ Text: "{text}"
51
+ Label:""",
52
+ "label_map": {"LABEL_0": 0, "LABEL_1": 1}
53
+ },
54
+
55
+ "Privacy Violation": {
56
+ "system": """Flag privacy violations if:
57
+ 1. Financial details (balance, account info) are shared **before** verifying:
58
+ - Full address, DOB, or SSN (last 4 digits).
59
+ 2. Verification is skipped or unconfirmed by the customer.
60
+
61
+ Examples:
62
+ - No Violation: Agent asks for DOB first, then shares balance. → Label: 0
63
+ - Violation: Agent says, "You owe $300" without verification. → Label: 1
64
+
65
+ Text: "{text}"
66
+ Label:""",
67
+ "label_map": {"LABEL_0": 0, "LABEL_1": 1}
68
+ }
69
+ }
70
+
71
+
72
+
73
+ def analyze_text(text: str, analysis_type: Literal["Profanity Detection", "Privacy Violation"]):
74
+ """Analyses the given text using Profanity and Compliance LLMs."""
75
+ template = PROMPT_TEMPLATES[analysis_type]
76
+
77
+ if analysis_type == "Profanity Detection":
78
+ # Use the Tagalog-English profanity model directly
79
+ result = profanity_classifier(text, truncation=True, max_length=512)
80
+ return 1 if result[0]['label'] == 'LABEL_1' else 0
81
+ else:
82
+ # Use RoBERTa with prompt engineering for privacy checks
83
+ prompt = template["system"].format(text=text)
84
+ result = privacy_classifier(prompt, truncation=True, max_length=512)
85
+ return template["label_map"][result[0]['label']]
86
+