File size: 2,666 Bytes
4b85b9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import sys
from pathlib import Path


BACKEND_DIR = Path(__file__).resolve().parents[1]
PROJECT_DIR = BACKEND_DIR.parent
LOCAL_EMOTION = PROJECT_DIR / "saved_models" / "emotion_v2"
LOCAL_SARCASM = PROJECT_DIR / "saved_models" / "sarcasm_v4"

if LOCAL_EMOTION.is_dir():
    os.environ.setdefault("USE_LOCAL_MODELS", "true")
    os.environ.setdefault("MOODLENS_EMOTION_MODEL_ID", str(LOCAL_EMOTION))

if LOCAL_SARCASM.is_dir():
    os.environ.setdefault("USE_LOCAL_MODELS", "true")
    os.environ.setdefault("MOODLENS_SARCASM_MODEL_ID", str(LOCAL_SARCASM))

sys.path.insert(0, str(BACKEND_DIR))
sys.path.insert(0, str(Path(__file__).resolve().parent))

from app.services.fusion_engine import analyze_text  # noqa: E402
from fusion_balance_cases import BALANCE_CASES  # noqa: E402


def compact_audit(result):
    return {
        "input": result["input_text"],
        "overall_mood": result["overall"]["overall_mood"],
        "overall_mood_score": result["overall"]["mood_score"],
        "dominant_emotion": result["overall"]["dominant_emotion"],
        "statements": [
            {
                "text": statement["text"],
                "raw_emotion_top3": statement["top3_emotions"],
                "raw_sarcasm_probabilities": {
                    "Not Sarcastic": statement["raw_sarcasm"]["model_not_sarcasm_score"],
                    "Sarcastic": statement["raw_sarcasm"]["model_sarcasm_score"],
                },
                "sarcastic_index": statement.get("sarcastic_index", 1),
                "raw_sarcasm_score": statement["raw_sarcasm"]["model_sarcasm_score"],
                "threshold_used": statement.get("sarcasm_threshold_used"),
                "raw_sarcasm_label": statement["raw_sarcasm"]["label"],
                "rule_applied": statement.get("rule_applied"),
                "calibration_reason": statement.get("calibration_reason"),
                "final_sarcasm_label": statement["sarcasm_label"],
                "final_primary_emotion": statement["primary_emotion"],
                "final_sentiment": statement["sentiment"],
                "final_mood_score": statement["mood_score"],
                "interpretation": statement["interpretation"],
                "reason": statement["sarcasm_reason"],
            }
            for statement in result["statements"]
        ],
    }


def main():
    texts = sys.argv[1:]
    if not texts:
        texts = [
            text
            for case in BALANCE_CASES
            for text in case["texts"]
        ]

    for text in texts:
        print(json.dumps(compact_audit(analyze_text(text)), indent=2))


if __name__ == "__main__":
    main()