-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranscriptAnalyzer.py
More file actions
99 lines (84 loc) Β· 2.91 KB
/
Copy pathTranscriptAnalyzer.py
File metadata and controls
99 lines (84 loc) Β· 2.91 KB
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
import ollama
import json
import re
client = ollama.Client(host='http://host.docker.internal:11434')
SYSTEM_PROMPT_QA = """
You are a seasoned Call Center Quality Assurance (QA) expert.
Evaluate the following customer service conversation with two tasks:
1. Rate the agent's performance based on:
- Empathy
- Problem-Solving
- Professionalism
2. Identify the main complaint type from the following list:
- Network Connectivity
- Internet Speed / Data Issues
- Billing & Charges
- Recharge or Balance Problems
- SIM or Number Portability
- Service Activation/Deactivation
- Customer Support Experience
- Account Login / App Issues
- Value-Added Services
- Other
Instructions:
- Assign an overall score from 1 to 5.
- Provide short, constructive feedback.
- Select only one complaint type from the list.
- Respond in this strict JSON format:
{
"score": <number from 1 to 5>,
"feedback": "<constructive feedback>",
"complaint_type": "<one of the categories above>"
}
"""
SYSTEM_PROMPT_REPORT = """
You are a Conversation Reviewer. Your job is to review the conversation and generate detailed feedback for each unique speaker.
Instructions:
- Detect the overall topic of the discussion.
- For each unique speaker, provide feedback focusing on communication effectiveness, clarity, tone, engagement, and any observed issues or strengths.
- Respond ONLY in the following JSON format:
{
"topic": "<detected topic>",
"speaker_feedback": {
"<Speaker1>": "<detailed feedback>",
"<Speaker2>": "<detailed feedback>"
}
}
"""
def extract_json(text):
try:
return json.loads(text)
except json.JSONDecodeError:
# Try to sanitize bad characters
sanitized = text.replace('\r', '').replace('\t', '\\t').replace('\n', '\\n')
match = re.search(r'\{.*\}', sanitized, re.DOTALL)
if match:
try:
return json.loads(match.group())
except json.JSONDecodeError as e:
return {
"score": 0,
"feedback": f"Partial JSON parse error after sanitizing: {str(e)}",
"complaint_type": "Other"
}
return {
"score": 0,
"feedback": "No valid JSON found even after sanitizing.",
"complaint_type": "Other"
}
def analyze_transcript(transcript):
messages = [
{"role": "system", "content": SYSTEM_PROMPT_QA},
{"role": "user", "content": f"Transcript:\n{transcript}"}
]
response = client.chat(model='llama3', messages=messages)
content = response['message']['content']
return extract_json(content)
def generate_call_report(transcript):
messages = [
{"role": "system", "content": SYSTEM_PROMPT_REPORT},
{"role": "user", "content": f"Transcript:\n{transcript}"}
]
response = client.chat(model='llama3', messages=messages)
content = response['message']['content']
return extract_json(content)