File size: 2,823 Bytes
6f053b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import pandas as pd
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier

# Ensure infrastructure is importable if it's a sibling package in src
# Assuming src is in PYTHONPATH or we add it relative to this file
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from infrastructure.rules import crear_dataset_rules
from infrastructure.client_requests import (
    consult_insurance_policy,
    report_emergency,
    consult_payments,
    schedule_inspection,
    manage_claims,
    quote_new_insurance,
    consult_bank_channel
)

class InsuranceChatbot:
    def __init__(self):
        self.pipeline = None
        self.intents = {
            "consultar_poliza": consult_insurance_policy,
            "reportar_emergencia": report_emergency,
            "pagos": consult_payments,
            # "inspeccion": schedule_inspection, # Not in original rules dictionary but in imports
            # "reclamos": manage_claims, # Not in original rules dictionary but in imports
            "cotizar": quote_new_insurance,
            # "banco": consult_bank_channel # Not in original rules dictionary but in imports
        }
        # Mapping intent labels to functions
        # The rules.py dataset generator produces specific labels, need to match them.
        # Labels from rules.py: consultar_poliza, reportar_emergencia, pagos, cotizar
        
        # We can extend the dataset or mapping if needed. For now, matching the notebook logic.

    def train_model(self):
        """Generates dataset and trains the classification pipeline."""
        print("Generating dataset...")
        df = crear_dataset_rules(n_pos_clas=1000)
        
        print(f"Training model on {len(df)} samples...")
        self.pipeline = Pipeline([
            ('tfidf', TfidfVectorizer()),
            ('clf', RandomForestClassifier(random_state=42))
        ])
        
        X = df['text']
        y = df['label']
        self.pipeline.fit(X, y)
        print("Model trained successfully.")

    def predict_intent(self, text):
        """Predicts the intent of the given text."""
        if not self.pipeline:
            raise ValueError("Model not trained. Call train_model() first.")
        
        prediction = self.pipeline.predict([text])[0]
        return prediction

    def handle_message(self, text):
        """Processes a message and executes the corresponding action."""
        intent = self.predict_intent(text)
        print(f"Detected intent: {intent}")
        
        action = self.intents.get(intent)
        if action:
            return action()
        else:
            return "Lo siento, no entendí tu solicitud o no tengo una acción para ese intento."