File size: 7,197 Bytes
f1ef7e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Run selected V5 emotion/sentiment inference on AppTek call-center samples.

This script uses the final selected V5 model through EmotionPredictor and
generates realistic call-center inference outputs.

Run smoke test from ml-services:

    python -m src.inference.run_apptek_inference --max-calls 2 --max-duration-seconds 30

Run full AppTek sample:

    python -m src.inference.run_apptek_inference --max-calls 20 --max-duration-seconds 60
"""

import argparse
import json
from pathlib import Path
from typing import Any, Dict, Optional

import pandas as pd

from src.inference.emotion_predictor import (
    EmotionPredictor,
    SELECTED_EMOTION_MODEL_VERSION,
)


PROJECT_ROOT = Path(__file__).resolve().parents[3]
ML_SERVICES_ROOT = PROJECT_ROOT / "ml-services"

APPTEK_METADATA_PATH = (
    ML_SERVICES_ROOT / "data" / "processed" / "apptek" / "apptek_metadata.csv"
)

APPTEK_RESULTS_DIR = (
    ML_SERVICES_ROOT / "outputs" / "apptek" / "sentiment_results"
)

APPTEK_SUMMARY_PATH = (
    ML_SERVICES_ROOT / "outputs" / "apptek" / "apptek_sentiment_summary.csv"
)


def result_to_dict(result: Any) -> Dict:
    """
    Convert AudioSentimentResult to dictionary for JSON saving.
    Supports Pydantic v2, Pydantic v1, or dataclass-like objects.
    """
    if hasattr(result, "model_dump"):
        return result.model_dump()

    if hasattr(result, "dict"):
        return result.dict()

    if hasattr(result, "__dict__"):
        return result.__dict__

    raise TypeError(f"Cannot convert result to dict: {type(result)}")


def run_apptek_inference(
    metadata_path: Path = APPTEK_METADATA_PATH,
    max_calls: Optional[int] = None,
    max_duration_seconds: float = 60.0,
    build_timeline: bool = True,
) -> None:
    """
    Run selected emotion model on AppTek samples.
    """
    if not metadata_path.exists():
        raise FileNotFoundError(
            f"AppTek metadata not found: {metadata_path}\n"
            "Run python -m src.data.apptek_dataset first."
        )

    metadata = pd.read_csv(metadata_path)

    if max_calls is not None:
        metadata = metadata.head(max_calls)

    APPTEK_RESULTS_DIR.mkdir(parents=True, exist_ok=True)
    APPTEK_SUMMARY_PATH.parent.mkdir(parents=True, exist_ok=True)

    predictor = EmotionPredictor(max_duration_seconds=max_duration_seconds)

    summary_rows = []

    print("\nRunning AppTek inference")
    print("-" * 80)
    print(f"Selected model: {SELECTED_EMOTION_MODEL_VERSION}")
    print(f"Metadata: {metadata_path}")
    print(f"Calls to process: {len(metadata)}")
    print(f"Max duration per call: {max_duration_seconds} seconds")
    print(f"Build timeline: {build_timeline}")
    print("-" * 80)

    for _, row in metadata.iterrows():
        call_id = row["call_id"]
        audio_path = ML_SERVICES_ROOT / row["audio_path"]
        domain = row.get("selected_domain", row.get("domain", ""))
        raw_domain = row.get("raw_domain", domain)

        print(f"Processing {call_id} | domain={domain} | raw_domain={raw_domain} | audio={audio_path.name}")

        result = predictor.analyze_audio(
            audio_path=audio_path,
            call_id=call_id,
            build_timeline=build_timeline,
        )

        result_dict = result_to_dict(result)

        # Add AppTek metadata context to output.
        result_dict["apptek_metadata"] = {
            "domain": domain,
            "raw_domain": raw_domain,
            "gender": row.get("gender", ""),
            "accent": row.get("accent", ""),
            "duration_seconds": float(row.get("duration_seconds", 0.0)),
            "text_preview": str(row.get("text", ""))[:500],
        }

        output_path = APPTEK_RESULTS_DIR / f"{call_id}_sentiment.json"

        with output_path.open("w", encoding="utf-8") as file:
            json.dump(result_dict, file, indent=2)

        summary_rows.append(
            {
                "call_id": call_id,
                "domain": domain,
                "raw_domain": raw_domain,
                "gender": row.get("gender", ""),
                "accent": row.get("accent", ""),
                "duration_seconds": row.get("duration_seconds", 0.0),
                "processed_duration_seconds": max_duration_seconds,
                "overall_audio_sentiment": result_dict.get("overall_audio_sentiment"),
                "dominant_emotion": result_dict.get("dominant_emotion"),
                "negative_emotion_probability": result_dict.get(
                    "negative_emotion_probability"
                ),
                "anger_probability": result_dict.get("anger_probability"),
                "stress_probability": result_dict.get("stress_probability"),
                "sadness_probability": result_dict.get("sadness_probability"),
                "anxiety_probability": result_dict.get("anxiety_probability"),
                "calm_probability": result_dict.get("calm_probability"),
                "audio_escalation_score": result_dict.get("audio_escalation_score"),
                "risk_level": result_dict.get("risk_level"),
                "prediction_confidence": result_dict.get("prediction_confidence"),
                "confidence_level": result_dict.get("confidence_level"),
                "uncertain_prediction": result_dict.get("uncertain_prediction"),
                "model_version": result_dict.get("model_version"),
                "result_json": str(output_path.relative_to(ML_SERVICES_ROOT)),
            }
        )

    summary_df = pd.DataFrame(summary_rows)
    summary_df.to_csv(APPTEK_SUMMARY_PATH, index=False)

    print("\nAppTek inference completed successfully.")
    print("-" * 80)
    print(f"Saved JSON results to: {APPTEK_RESULTS_DIR}")
    print(f"Saved summary CSV to: {APPTEK_SUMMARY_PATH}")
    print("-" * 80)

    print("\nSummary preview:")
    print(
        summary_df[
            [
                "call_id",
                "domain",
                "dominant_emotion",
                "overall_audio_sentiment",
                "audio_escalation_score",
                "risk_level",
                "confidence_level",
            ]
        ].head()
    )


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Run selected V5 emotion inference on AppTek samples."
    )

    parser.add_argument(
        "--metadata-path",
        type=Path,
        default=APPTEK_METADATA_PATH,
        help="Path to AppTek metadata CSV.",
    )

    parser.add_argument(
        "--max-calls",
        type=int,
        default=None,
        help="Maximum number of AppTek calls to process.",
    )

    parser.add_argument(
        "--max-duration-seconds",
        type=float,
        default=60.0,
        help="Maximum duration per call to process.",
    )

    parser.add_argument(
        "--no-timeline",
        action="store_true",
        help="Disable sentiment timeline generation.",
    )

    return parser.parse_args()


if __name__ == "__main__":
    args = parse_args()

    run_apptek_inference(
        metadata_path=args.metadata_path,
        max_calls=args.max_calls,
        max_duration_seconds=args.max_duration_seconds,
        build_timeline=not args.no_timeline,
    )