Repository navigation
Expand file tree
/
Copy pathorchestrator.py
More file actions
334 lines (284 loc) · 10.8 KB
/
Copy pathorchestrator.py
File metadata and controls
334 lines (284 loc) · 10.8 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
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
"""
Orchestrator — Main Entry Point
This module orchestrates the complete transcript intelligence pipeline:
1. Load and validate transcripts
2. Run hybrid per-call analysis
3. Extract cross-call insights
4. Generate slide content
5. Output results JSON
Entry point: run_agent()
Example:
>>> python orchestrator.py
# Or programmatically:
>>> from orchestrator import run_agent
>>> results = run_agent()
>>> print(results["statistics"]["total_calls"])
"""
import json
import time
from pathlib import Path
from collections import defaultdict
from config import DATASET_PATH, OUTPUT_PATH, LLM_PROVIDER
from config import (
USE_LOCAL_CLASSIFIER,
USE_LOCAL_SENTIMENT,
USE_LOCAL_NER,
PER_CALL_LLM_SUMMARY,
)
from llm_client import LLMClient
from transcript_loader import load_transcript
from local_analyzers import (
classify_sentiment_local,
classify_topic_local,
classify_topic_from_tags,
extract_action_items_local,
extract_risks_local,
speaker_engagement_local,
)
from cross_call_insights import extract_cross_call_insights
from slide_content import generate_slide_content
def analyze_transcript_hybrid(transcript_data, llm):
"""
Run hybrid analysis on a single transcript.
Orchestrates local models for high-volume tasks, with optional LLM calls
for per-call summaries (disabled by default as expensive).
Args:
transcript_data (dict): Loaded transcript (from transcript_loader)
llm (LLMClient): LLM client instance
Returns:
dict: Analysis result with keys:
- sentiment_score, sentiment_label, key_moments
- action_items, action_item_entities
- risks, speaker_engagement
- summary, primary_topic, secondary_topics, topic_scores (or raw_topics)
- error (optional): Present if analysis failed
"""
text = transcript_data["full_text"]
sentences = transcript_data["sentences"]
summary = transcript_data["summary"]
result = {}
# LOCAL: Sentiment (DistilBERT)
if USE_LOCAL_SENTIMENT:
result.update(classify_sentiment_local(text, sentences))
else:
result["sentiment_score"] = 0
result["sentiment_label"] = "neutral"
result["key_moments"] = []
# LOCAL: Action Items + Engagement
if USE_LOCAL_NER:
action_items = extract_action_items_local(sentences)
else:
action_items = []
risks = extract_risks_local(text, sentences)
engagement = speaker_engagement_local(transcript_data)
result["action_items"] = [a["text"] for a in action_items]
result["action_item_entities"] = action_items
result["risks"] = risks
result["speaker_engagement"] = engagement
result["summary"] = summary
# LOCAL: Topic Classification (two approaches)
# (A) Tag-based: if summary includes topics, map them to canonical topics
# (B) Zero-shot: classify summary against VALID_TOPICS
topics = transcript_data.get("summary_topics", [])
if topics:
result.update(classify_topic_from_tags(topics))
elif USE_LOCAL_CLASSIFIER:
result.update(classify_topic_local(transcript_data["summary"]))
else:
result["primary_topic"] = "Other"
result["secondary_topics"] = []
result["raw_topics"] = []
return result
def generate_statistics(all_transcripts, all_analysis):
"""
Aggregate statistics across all calls.
Args:
all_transcripts (list): All loaded transcripts
all_analysis (list): All per-call analyses
Returns:
dict: Statistics with keys:
- total_calls, call_types, topic_distribution, sentiment_distribution
- avg_sentiment_by_call_type, risk_count, total_word_count
- total_speakers, calls_with_errors
"""
stats = {
"total_calls": len(all_transcripts),
"call_types": defaultdict(int),
"topic_distribution": defaultdict(int),
"sentiment_distribution": defaultdict(int),
"avg_sentiment_by_call_type": {},
"risk_count": 0,
"total_word_count": 0,
"total_speakers": set(),
"calls_with_errors": 0,
}
sentiment_by_type = defaultdict(list)
for i, t in enumerate(all_transcripts):
# Tally call types and word counts
stats["call_types"][t["call_type"]] += 1
stats["total_word_count"] += t["word_count"]
stats["total_speakers"].update(t["speakers"])
# Per-call analysis
a = all_analysis[i]
if "error" in a:
stats["calls_with_errors"] += 1
continue
# Tally topics and sentiment
stats["topic_distribution"][a.get("primary_topic", "Other")] += 1
stats["sentiment_distribution"][a.get("sentiment_label", "neutral")] += 1
# Aggregate sentiment by call type (for average computation)
sentiment_by_type[t["call_type"]].append(
a.get("sentiment_score", 0)
)
# Count risks
if a.get("risks"):
stats["risk_count"] += len(a["risks"])
# Compute averages
for ct, scores in sentiment_by_type.items():
if scores:
stats["avg_sentiment_by_call_type"][ct] = round(
sum(scores) / len(scores), 2
)
# Convert sets/defaultdicts to regular dicts/ints
stats["total_speakers"] = len(stats["total_speakers"])
stats["call_types"] = dict(stats["call_types"])
stats["topic_distribution"] = dict(stats["topic_distribution"])
stats["sentiment_distribution"] = dict(stats["sentiment_distribution"])
return stats
def run_agent():
"""
Main pipeline: load → analyze → report.
Orchestrates the complete transcript intelligence pipeline:
1. Initialize LLM
2. Load and validate transcripts
3. Run hybrid per-call analysis
4. Extract cross-call insights (1 LLM call)
5. Generate slide content (1 LLM call)
6. Compute statistics
7. Save results JSON
Returns:
dict: Complete results object with metadata, statistics, analyses, insights
"""
print("\n" + "=" * 80)
print("TRANSCRIPT INTELLIGENCE AGENT v3 (Hybrid: Local + LLM)")
print("=" * 80)
print(f"LLM Provider: {LLM_PROVIDER}")
print(
f"Local classifier: {USE_LOCAL_CLASSIFIER} | "
f"Local sentiment: {USE_LOCAL_SENTIMENT} | "
f"Local NER: {USE_LOCAL_NER}"
)
print(f"Per-call LLM summary: {PER_CALL_LLM_SUMMARY}")
print(f"Dataset: {DATASET_PATH}\n")
# Initialize LLM
llm = LLMClient()
# Load all transcripts from dataset
call_dirs = sorted([d for d in DATASET_PATH.iterdir() if d.is_dir()])
if not call_dirs:
print(f"❌ No transcripts found in {DATASET_PATH}")
return
print(f"Found {len(call_dirs)} transcripts\n")
# Process each call
all_transcripts, all_analysis = [], []
failed_calls = []
for idx, call_dir in enumerate(call_dirs, 1):
print(
f"\n{'='*80}\nCALL {idx}/{len(call_dirs)}: {call_dir.name}\n"
f"{'='*80}"
)
# Load transcript
try:
print("📂 Loading transcript...")
t = load_transcript(call_dir)
print(
f"✅ Loaded: {t['title']} | Type: {t['call_type']} | "
f"Speakers: {len(t['speakers'])} | Words: {t['word_count']}"
)
except Exception as e:
print(f"❌ Failed to load: {e}")
failed_calls.append((call_dir.name, str(e)))
continue
# Analyze transcript
try:
print("🔍 Hybrid analysis (local models + optional LLM)...")
analysis = analyze_transcript_hybrid(t, llm)
print(
f"✅ Topic: {analysis.get('primary_topic')} | "
f"Sentiment: {analysis.get('sentiment_label')} "
f"({analysis.get('sentiment_score')})"
)
if analysis.get("risks"):
print(f" ⚠️ Risks: {len(analysis['risks'])} flagged")
except Exception as e:
print(f"❌ Failed analysis: {e}")
analysis = {
"error": True,
"message": str(e),
"primary_topic": "Other",
"sentiment_label": "neutral",
"sentiment_score": 0,
"risks": [],
}
all_transcripts.append(t)
all_analysis.append(analysis)
# Generate cross-call insights
print(f"\n{'='*80}\nCROSS-CALL INSIGHTS (LLM: {LLM_PROVIDER})\n{'='*80}\n")
insights = extract_cross_call_insights(all_transcripts, all_analysis, llm)
if "insights" in insights and insights["insights"]:
print("📊 Key Insights:")
for i, ins in enumerate(insights["insights"], 1):
print(f"\n{i}. {ins.get('insight')}")
print(f" Evidence: {ins.get('evidence')}")
print(f" Urgency: {ins.get('urgency')}")
# Generate statistics
print(f"\n{'='*80}\nSTATISTICS\n{'='*80}\n")
stats = generate_statistics(all_transcripts, all_analysis)
# Generate slide content
print("\n🎨 Generating slide content...")
slide_content = generate_slide_content(
all_transcripts, all_analysis, stats, insights, llm
)
if "error" in slide_content:
print(f" ⚠️ Slide content generation failed: "
f"{slide_content.get('message')}")
slide_content = {}
insights["slide_content"] = slide_content
# Print summary
print(f"\n{'='*80}\nSUMMARY\n{'='*80}")
print(f"Total Calls: {stats['total_calls']} | Failed: {len(failed_calls)}")
print(f"Words: {stats['total_word_count']:,} | "
f"Speakers: {stats['total_speakers']}")
print(f"Risks: {stats['risk_count']}")
print(f"Call Types: {stats['call_types']}")
print(f"Topics: {stats['topic_distribution']}")
print(f"Sentiment: {stats['sentiment_distribution']}")
# Build results object
results = {
"metadata": {
"llm_provider": LLM_PROVIDER,
"llm_model": llm.model,
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
"total_calls": len(all_transcripts),
"failed_calls": len(failed_calls),
"config": {
"USE_LOCAL_CLASSIFIER": USE_LOCAL_CLASSIFIER,
"USE_LOCAL_SENTIMENT": USE_LOCAL_SENTIMENT,
"USE_LOCAL_NER": USE_LOCAL_NER,
"PER_CALL_LLM_SUMMARY": PER_CALL_LLM_SUMMARY,
},
},
"statistics": stats,
"individual_analyses": all_analysis,
"cross_call_insights": insights,
"failed_calls": [
{"call_id": c, "error": e} for c, e in failed_calls
],
}
# Save results JSON
output_file = OUTPUT_PATH / "agent_analysis_results_v3.json"
with open(output_file, "w") as f:
json.dump(results, f, indent=2)
print(f"\n\n✅ Results saved to: {output_file}")
return results
if __name__ == "__main__":
run_agent()