File size: 9,472 Bytes
8d1c4bc
3340b1c
 
4e9f5d4
8d1c4bc
879e7eb
3340b1c
8d1c4bc
 
 
 
6debdfd
9ff5f83
3340b1c
6c9f8ba
3340b1c
bc0f75e
879e7eb
8d1c4bc
4e9f5d4
5f0c401
f547fd9
5f0c401
2d11842
5f0c401
 
acb6ed9
3340b1c
acb6ed9
3340b1c
acb6ed9
bc0f75e
5f0c401
 
35c1a65
32d6875
 
 
 
 
5f0c401
32d6875
5f0c401
32d6875
 
 
5f0c401
32d6875
5f0c401
32d6875
 
fbfe5d9
32d6875
 
 
 
 
 
 
5f33166
32d6875
 
 
 
 
 
 
 
08860c0
 
a5501b7
bc0f75e
 
 
8d1c4bc
32d6875
 
 
 
 
 
5f33166
32d6875
 
 
bc0f75e
3340b1c
5f33166
32d6875
 
3340b1c
 
9a485d3
8d1c4bc
 
5910af2
3340b1c
 
f547fd9
4e9f5d4
8d1c4bc
3340b1c
4e9f5d4
9a485d3
3340b1c
 
 
9a485d3
73dbfa8
50587b1
 
 
8910a27
 
9a485d3
4e9f5d4
2d11842
4e9f5d4
 
 
a5501b7
9fa3a58
d388d30
 
 
32d6875
6293d43
4e9f5d4
1d11933
 
 
 
 
 
 
6293d43
 
9a485d3
f547fd9
9e7c830
9a485d3
5910af2
 
9a485d3
fbfe5d9
 
 
 
 
bda1f51
32d6875
 
 
879e7eb
 
 
 
bc0f75e
879e7eb
bc0f75e
 
2d11842
 
 
 
 
 
bc0f75e
5f33166
bc0f75e
 
 
 
 
 
8910a27
bc0f75e
73dbfa8
bc0f75e
 
73dbfa8
bc0f75e
 
32d6875
9f4a9e1
bc0f75e
 
 
 
 
8d1c4bc
bc0f75e
6895902
bc0f75e
 
 
 
3bac2a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc0f75e
 
3bac2a0
 
 
8d1c4bc
 
3bac2a0
bc0f75e
 
 
 
f547fd9
 
bc0f75e
 
f547fd9
 
 
 
bc0f75e
 
 
2d11842
f547fd9
bc0f75e
f547fd9
bc0f75e
4e9f5d4
bc0f75e
 
 
 
 
 
2d11842
a5501b7
 
3340b1c
4e9f5d4
 
 
 
 
f547fd9
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
import os
import json
import pickle
import random
import tempfile
import asyncio
import numpy as np
import redis
import uvicorn
import nltk
from nltk.stem import WordNetLemmatizer
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Dropout, Input
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.models import load_model, save_model
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
from dotenv import load_dotenv
from datetime import datetime
import logging
from tqdm import tqdm  # Importar tqdm

# Configuración de logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

load_dotenv()

app = FastAPI()

lemmatizer = WordNetLemmatizer()
redis_password = os.getenv("REDIS_PASSWORD")
r = redis.Redis(host=os.getenv("REDIS_HOST"), port=int(os.getenv("REDIS_PORT")), password=redis_password)

def create_intents_json():
    intents = {
        "intents": [
            {
                "tag": "greeting",
                "patterns": ["Hola", "¿Cómo estás?", "Buenos días"],
                "responses": ["¡Hola!", "¿Cómo puedo ayudarte?"],
                "date": "2021-01-01"
            },
            {
                "tag": "goodbye",
                "patterns": ["Adiós", "Hasta luego", "Nos vemos"],
                "responses": ["¡Hasta luego!", "Cuídate!"],
                "date": "2021-01-01"
            }
        ]
    }
    with open('intents.json', 'w') as f:
        json.dump(intents, f, ensure_ascii=False, indent=4)

def load_and_filter_data():
    with open("intents.json") as file:
        intents = json.load(file)

    filtered_intents = {"intents": []}
    for intent in intents['intents']:
        if "date" in intent:
            intent_date = datetime.strptime(intent["date"], "%Y-%m-%d")
            if intent_date.year >= 2000 and intent_date <= datetime.now():
                filtered_intents['intents'].append(intent)

    return filtered_intents

if not os.path.exists('models'):
    os.makedirs('models')

async def train_and_save_model():
    global lemmatizer, r
    while True:
        words, classes, documents = [], [], []
        intents = load_and_filter_data()

        user_questions = r.lrange('user_questions', 0, -1)

        for question in user_questions:
            question = question.decode('utf-8')
            processed_words = nltk.word_tokenize(question)
            documents.append((processed_words, "user_question"))
            words.extend(processed_words)

        for intent in intents['intents']:
            for pattern in intent['patterns']:
                processed_words = nltk.word_tokenize(pattern)
                documents.append((processed_words, intent['tag']))
                words.extend(processed_words)
                if intent['tag'] not in classes:
                    classes.append(intent['tag'])

        words = sorted(set(words))
        classes = sorted(set(classes))

        training = []
        output_empty = [0] * len(classes)
        for doc in tqdm(documents, desc="Preparando datos"):
            bag = [0] * len(words)
            pattern_words = [lemmatizer.lemmatize(word.lower()) for word in doc[0]]
            for w in words:
                bag[words.index(w)] = 1 if w in pattern_words else 0

            output_row = list(output_empty)
            output_row[classes.index(doc[1])] = 1
            training.append([bag, output_row])

        if not training:
            await asyncio.sleep(60)
            continue

        train_x = np.array([row[0] for row in training])
        train_y = np.array([row[1] for row in training])

        if train_x.shape[1] != len(words):
            logger.error(f"Error: Las dimensiones de entrada no coinciden. Esperado: {len(words)}, encontrado: {train_x.shape[1]}")
            await asyncio.sleep(60)
            continue

        if r.exists('chatbot_model'):
            with tempfile.NamedTemporaryFile(delete=False, suffix='.h5') as temp_file:
                temp_file.write(r.get('chatbot_model'))
                temp_file_name = temp_file.name
            model = load_model(temp_file_name)
            os.remove(temp_file.name)
        else:
            input_layer = Input(shape=(len(words),))
            layer1 = Dense(128, activation='relu')(input_layer)
            layer2 = Dropout(0.5)(layer1)
            layer3 = Dense(64, activation='relu')(layer2)
            layer4 = Dropout(0.5)(layer3)
            output_layer = Dense(len(classes), activation='softmax')(layer4)

            model = Sequential(layers=[input_layer, layer1, layer2, layer3, layer4, output_layer])
            sgd = SGD(learning_rate=0.01, momentum=0.9, nesterov=True)
            model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

        print("Entrenando el modelo...")
        model.fit(train_x, train_y, epochs=1, batch_size=len(train_x), verbose=0)

        r.set('words', pickle.dumps(words))
        r.set('classes', pickle.dumps(classes))

        with tempfile.NamedTemporaryFile(delete=False, suffix='.h5') as temp_file:
            save_model(model, temp_file.name)
            with open(temp_file.name, 'rb') as f:
                r.set('chatbot_model', f.read())
            os.remove(temp_file.name)

async def handle_new_message(message: str):
    r.rpush('user_questions', message)
    await train_and_save_model()

class ChatMessage(BaseModel):
    message: str

@app.post("/chat")
async def chat(message: ChatMessage):
    words = pickle.loads(r.get('words'))
    classes = pickle.loads(r.get('classes'))
    
    with tempfile.NamedTemporaryFile(delete=False) as temp_file:
        temp_file.write(r.get('chatbot_model'))
        temp_file_name = temp_file.name

    model = load_model(temp_file_name)

    sentence_words = nltk.word_tokenize(message.message)
    bag = [0] * len(words)
    for s in sentence_words:
        for i, w in enumerate(words):
            if w == s:
                bag[i] = 1

    p = model.predict(np.array([bag]))[0]
    ERROR_THRESHOLD = 0.25
    results = [[i, p] for i, p in enumerate(p) if p > ERROR_THRESHOLD]
    results.sort(key=lambda x: x[1], reverse=True)
    return_list = []
    for i, p in results:
        return_list.append({"intent": classes[i], "probability": str(p)})

    await handle_new_message(message.message)

    return return_list

@app.post("/tag")
async def tag_question(question: str, tag: str):
    r.set(f"tag:{question}", tag)
    return {"message": "Etiqueta guardada"}

html_code = """
<!DOCTYPE html>
<html>
<head>
    <title>Chatbot</title>
    <style>
        body {
            font-family: sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        #container {
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            padding: 30px;
            width: 80%;
            max-width: 600px;
        }
        h1 {
            text-align: center;
            margin-bottom: 20px;
            color: #333;
        }
        #chatbox {
            height: 300px;
            overflow-y: auto;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            margin-bottom: 10px;
        }
        #chatbox p {
            margin: 5px 0;
        }
        #user_input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            margin-bottom: 10px;
            box-sizing: border-box;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div id="container">
        <h1>Chatbot</h1>
        <div id="chatbox"></div>
        <input type="text" id="user_input" placeholder="Escribe tu mensaje...">
        <button onclick="sendMessage()">Enviar</button>
    </div>
    <script>
    function sendMessage() {
        let userInput = document.getElementById('user_input').value;
        document.getElementById('user_input').value = '';
        document.getElementById('chatbox').innerHTML += '<p><b>Tú:</b> ' + userInput + '</p>';
        
        fetch('/chat', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ message: userInput })
        })
        .then(response => response.json())
        .then(data => {
            data.forEach(response => {
                document.getElementById('chatbox').innerHTML += '<p><b>Bot:</b> ' + response.intent + ' (' + response.probability + ')</p>';
            });
            document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight;
        });
    }
    </script>
</body>
</html>
"""

@app.get("/", response_class=HTMLResponse)
async def read_root():
    return html_code

if __name__ == "__main__":
    nltk.download('punkt')
    nltk.download('wordnet')
    nltk.download('omw-1.4')
    nltk.download('averaged_perceptron_tagger')
    nltk.download('punkt_tab')
    uvicorn.run(app, host="0.0.0.0", port=8000)