File size: 4,103 Bytes
c1e438c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import traceback
from core.device import DEVICE
from emotion.tracker import EmotionTracker
from reasoning.persona import PersonaController

class MVIState:
    def __init__(self):
        self.modules = {}
        self.health = {}

    def register(self, name, module, status=True):
        self.modules[name] = module
        self.health[name] = status

    def get(self, name):
        return self.modules.get(name)

    def report(self):
        return self.health


def safe_load(name, loader_fn, state):
    """
    Attempts to load a module and updates state health.
    """
    try:
        module = loader_fn()
        state.register(name, module, True)
        print(f"[LOAD SUCCESS] {name}")
    except Exception:
        traceback.print_exc()
        state.register(name, None, False)
        print(f"[LOAD FAILED] {name}")


def initialize_mvi():
    """
    Central MVI loader — loads all modules (language, emotion, vision, voice, memory)
    and registers them into MVIState with health tracking.
    """
    from language.tokenizer import SimpleTokenizer
    from language.embeddings import EmbeddingLayer
    from language.encoder import SentenceEncoder
    from language.intent import IntentClassifier
    from emotion.sentiment_model import SentimentRegressor
    from vision.image_encoder import ImageEncoder
    from vision.video_encoder import VideoEncoder
    from core.voice_encoder import VoiceEncoder
    from memory.short_term import ShortTermMemory
    from memory.long_term import LongTermMemory

    state = MVIState()

    # ---------- EMOTION TRACKER & PERSONA ----------
    state.register("emotion_tracker", EmotionTracker(window_size=6))
    state.register("persona", PersonaController())

    # ---------- LANGUAGE ----------
    def load_language():
        tokenizer = SimpleTokenizer()
        tokenizer.load_vocab("artifacts/vocab.json")

        embedder = EmbeddingLayer(
            tokenizer.vocab_size,
            pad_index=tokenizer.vocab[tokenizer.PAD_TOKEN]
        ).to(DEVICE)

        encoder = SentenceEncoder().to(DEVICE)
        encoder.load_state_dict(torch.load("artifacts/sentence_encoder.pt", map_location=DEVICE))

        intent = IntentClassifier(
            input_dim=encoder.projection.out_features,
            intent_labels=["question", "advice", "statement"]
        ).to(DEVICE)

        intent.load_state_dict(torch.load("artifacts/intent_classifier.pt", map_location=DEVICE))

        return {
            "tokenizer": tokenizer,
            "embedder": embedder.eval(),
            "encoder": encoder.eval(),
            "intent": intent.eval()
        }

    safe_load("language", load_language, state)

    # ---------- EMOTION ----------
    def load_emotion():
        model = SentimentRegressor(input_dim=128).to(DEVICE)
        model.load_state_dict(torch.load("artifacts/sentiment_regressor.pt", map_location=DEVICE))
        return model.eval()

    safe_load("emotion", load_emotion, state)

    # ---------- VISION ----------
    def load_image():
        model = ImageEncoder(embed_dim=128).to(DEVICE)
        model.load_state_dict(torch.load("artifacts/image_encoder.pt", map_location=DEVICE))
        return model.eval()

    safe_load("vision_image", load_image, state)

    def load_video():
        model = VideoEncoder(embed_dim=128).to(DEVICE)
        model.load_state_dict(torch.load("artifacts/video_encoder.pt", map_location=DEVICE))
        return model.eval()

    safe_load("vision_video", load_video, state)

    # ---------- VOICE ----------
    def load_voice():
        model = VoiceEncoder(embed_dim=128).to(DEVICE)
        model.load_state_dict(torch.load("artifacts/voice_encoder_best.pt", map_location=DEVICE))
        return model.eval()

    safe_load("voice", load_voice, state)

    # ---------- MEMORY ----------
    def load_memory():
        return {
            "stm": ShortTermMemory(max_len=10),
            "ltm": LongTermMemory(dim=128)
        }

    safe_load("memory", load_memory, state)

    print("\n[MVI SYSTEM HEALTH]")
    print(state.report())

    return state