File size: 13,575 Bytes
a59e76c
 
 
 
 
 
82bedfe
e870e13
 
57290d8
a59e76c
 
 
1aea0b7
afe0be6
ad7703f
 
 
1aea0b7
 
57290d8
2b40817
 
 
8f8b562
2b40817
a59e76c
ad7703f
05387f5
ad7703f
 
05387f5
 
 
 
 
 
a59e76c
 
 
05387f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a59e76c
 
05387f5
 
 
a59e76c
 
05387f5
 
 
 
afe0be6
a59e76c
afe0be6
a59e76c
 
 
 
 
 
 
afe0be6
 
a59e76c
 
05387f5
 
 
a59e76c
05387f5
 
 
 
a59e76c
05387f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a59e76c
57290d8
 
05387f5
 
 
 
 
ad7703f
a59e76c
 
 
 
05387f5
57290d8
 
 
 
 
 
a59e76c
57290d8
 
 
a59e76c
 
57290d8
a59e76c
57290d8
 
 
 
 
ad7703f
a59e76c
 
 
 
57290d8
 
05387f5
57290d8
a59e76c
 
 
57290d8
8a611c0
a59e76c
8a611c0
ad7703f
8a611c0
a59e76c
 
 
 
 
8a611c0
 
ad7703f
 
05387f5
ad7703f
57290d8
a59e76c
 
 
57290d8
05387f5
 
 
 
 
 
a59e76c
05387f5
 
a59e76c
05387f5
 
a59e76c
 
ad7703f
 
 
a59e76c
 
 
 
 
57290d8
a59e76c
 
57290d8
a59e76c
 
 
 
 
 
 
 
57290d8
05387f5
a59e76c
 
05387f5
 
a59e76c
 
 
05387f5
 
a59e76c
57290d8
a59e76c
ad7703f
 
 
57290d8
ad7703f
 
05387f5
57290d8
a59e76c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05387f5
 
afe0be6
a59e76c
ad7703f
afe0be6
a59e76c
ad7703f
05387f5
a59e76c
ad7703f
05387f5
a59e76c
ad7703f
05387f5
a59e76c
05387f5
 
 
 
 
 
 
 
 
 
 
a59e76c
05387f5
 
 
 
a59e76c
 
 
 
 
05387f5
 
 
57290d8
ad7703f
 
 
 
05387f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a59e76c
 
 
05387f5
a59e76c
57290d8
05387f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# -*- coding: utf-8 -*-
"""Mori – Inferencia Técnica (estable, UTF-8, con opción RAG ON/OFF)"""
#=====================================================================================
# Importing Libraries  ===============================================================
#=====================================================================================
import os, warnings, json, random, uuid, csv
import numpy as np
import streamlit as st
import datetime as dt
from pathlib import Path
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from Mori_TechnicalPrompts import answer_with_mori_rag, answer_with_mori_plain
import torch
from huggingface_hub import hf_hub_download, login
from sentence_transformers import SentenceTransformer  # RAG embeddings
#***************************************************************************
#Setting up variables
#***************************************************************************
# Token privado desde variable de entorno
HF_TOKEN = os.environ.get("HF_TOKEN")

#***************************************************************************
#Loading FAISS Vec DB
#***************************************************************************
REPO_ID = "tecuhtli/Mori_FAISS_Full"


#***************************************************************************
# Sidebar controls for generation params
#***************************************************************************

def sidebar_params():
    with st.sidebar:
        st.title("🎮 Personalidad (FLAN-T5)")

        ss = st.session_state

        # Estado inicial
        ss.setdefault("show_llm_controls", False)
        ss.setdefault("persona", "Mori Exacto")
        ss.setdefault("mode", "beam")  # 'beam' | 'sampling'
        ss.setdefault("max_new", 128)
        ss.setdefault("min_tok", 16)
        ss.setdefault("no_repeat", 3)
        ss.setdefault("num_beams", 4)
        ss.setdefault("length_penalty", 1.0)
        ss.setdefault("temperature", 0.7)
        ss.setdefault("top_p", 0.9)
        ss.setdefault("repetition_penalty", 1.0)

        # ----------------------------
        # Personalidad (presets)
        # ----------------------------
        st.header("💡 Personalidades predefinidas")
        c1, c2 = st.columns(2)

        with c1:
            if st.button("Exacto 🧐", use_container_width=True):
                ss.update({"persona": "exacto"})
                st.rerun()

        with c2:
            if st.button("Creativo 😃", use_container_width=True):
                ss.update({"persona": "creativo"})
                st.rerun()

        st.caption(f"Personalidad actual: **{ss.persona}**")

        st.markdown("---")
        st.title("👀 RAG")
        ss.setdefault("use_rag", True)
        ss.setdefault("rag_k", 1)
        ss.use_rag = st.checkbox(
            "Usar RAG (FAISS + One-Shot)",
            value=ss.use_rag,
            help="Recupera evidencias de la base FAISS de Mori en Hugging Face y las usa en el prompt."
        )

        st.markdown("---")
        st.title("🧾 Vista previa del Prompt")

        if "last_prompt" in ss and ss["last_prompt"]:
            with st.expander("Mostrar prompt generado"):
                st.text_area(
                    "Prompt actual:",
                    ss["last_prompt"],
                    height=200,
                    disabled=True
                )
        else:
            st.caption("👉 Aún no se ha generado ningún prompt.")

        # ----------------------------
        # Construir diccionario de parámetros
        # ----------------------------
        params = {
            "persona": ss.persona,
            "mode": ss.mode,
            "max_new_tokens": int(ss.max_new),
            "min_tokens": int(ss.min_tok),
            "no_repeat_ngram_size": int(ss.no_repeat),
            "repetition_penalty": float(ss.repetition_penalty),
        }

        return params

#***************************************************************************
# Functions
#***************************************************************************

# Function to clean the question field (por si luego lo quieres usar en un botón)
def limpiar_input():
    st.session_state["entrada"] = ""

# ✅ Corrige la ruta correctamente desde Scripts hacia Models
def get_model_path(folder_name):
    return Path("Models") / folder_name

# Function to save user interaction
def saving_interaction(question, response, user_id, use_of_rag, bot_personality):
    """
    Guarda la interacción en CSV y JSONL para análisis posterior.
    """
    timestamp = dt.datetime.now().isoformat()
    stats_dir = Path("Statistics")
    stats_dir.mkdir(parents=True, exist_ok=True)

    archivo_csv = stats_dir / "conversaciones_log.csv"
    existe_csv = archivo_csv.exists()

    # CSV
    with open(archivo_csv, mode="a", encoding="utf-8", newline="") as f_csv:
        writer = csv.writer(f_csv)
        if not existe_csv:
            writer.writerow(["timestamp", "user_id", "pregunta", "respuesta", "rag", "personality"])
        writer.writerow([timestamp, user_id, question, response, use_of_rag, bot_personality])

    # JSONL
    archivo_jsonl = stats_dir / "conversaciones_log.jsonl"
    with open(archivo_jsonl, mode="a", encoding="utf-8") as f_jsonl:
        registro = {
            "timestamp": timestamp,
            "user_id": user_id,
            "pregunta": question,
            "respuesta": response,
            "uso_rag": use_of_rag,
            "personality": bot_personality
        }
        f_jsonl.write(json.dumps(registro, ensure_ascii=False) + "\n")

# Function to load models within the huggingface repositories space
@st.cache_resource
def load_remote_model(repo_id: str, token: str = None):
    tokenizer = AutoTokenizer.from_pretrained(repo_id, token=token)
    model = AutoModelForSeq2SeqLM.from_pretrained(repo_id, token=token)
    return model, tokenizer
#-------------------------------------------------------------------------
# Seeds
#-------------------------------------------------------------------------

def set_seeds(seed: int = 42):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

#***************************************************************************
# MAIN
#***************************************************************************

if __name__ == "__main__":
    # Semilla para reproducibilidad
    set_seeds(42)

    # --- Estado que debe persistir en todos los reruns ---
    ss = st.session_state
    ss.setdefault("historial", [])
    ss.setdefault("last_prompt", "")
    ss.setdefault("last_response", "")
    ss.setdefault("just_generated", False)

    # Sidebar (control total)
    GEN_PARAMS = sidebar_params()
    GEN_PARAMS["persona"] = ss.persona  # por si acaso

    # Assigning a new ID to the current user
    if "user_id" not in ss:
        ss["user_id"] = str(uuid.uuid4())[:8]


    # Modelo Técnico
    MODEL_REPO_ID = "tecuhtli/mori-tecnico-model"
    model, tokenizer = load_remote_model(MODEL_REPO_ID, HF_TOKEN)
    
    # Presentación de Mori
    st.title("🤖 Mori - Tu Asistente Personal ⌨️")

    st.caption("🙋🏽‍ Puedes preguntarme conceptos sobre machine learning, estadística, visualización, BI, limpieza de datos y más.")
    st.caption("🙇🏽‍ Por el momento, solo puedo contestar preguntas simples como:")

    st.caption("  🔹 **Definiciones** — Ejemplo: *¿Qué es machine learning?*")
    st.caption("  🔹 **Procedimientos** — Ejemplo: *¿Cómo limpiar datos?*")
    st.caption("  🔹 **Funcionalidad** — Ejemplo: *¿Para qué sirve un autoencoder?*")

    st.markdown("<br>", unsafe_allow_html=True)

    st.caption("🦾 Aún estoy aprendiendo. Puedes ver mi desarrollo aquí:")
    st.caption("[hazutecuhtli.github.io](https://github.com/hazutecuhtli/Mori_Development)")

    st.markdown("<br>", unsafe_allow_html=True)

    st.caption("✏️ Escribe tu pregunta abajo.")

    # 🔁 Limpieza segura antes del formulario
    if ss.pop("_clear_entrada", False):
        if "entrada" in ss:
            del ss["entrada"]

    # 🧠 Flash de respuesta (la guardamos, pero la mostraremos después del form)
    _flash = ss.pop("_flash_response", None)

    # Formulario principal
    with st.form("formulario_mori"):
        user_question = st.text_area("📝 Escribe tu pregunta aquí", key="entrada", height=100)
        submitted = st.form_submit_button("Responder")

    if submitted:
        if not user_question:
            st.info("Mori: ¿Podrías repetir eso? No entendí bien 😅")
        else:
            use_rag = st.session_state.get("use_rag", False)
            
            persona = GEN_PARAMS.get("persona", ss.persona)

            if use_rag:
                use_of_rag = 'Con RAG'
                response, prompt = answer_with_mori_rag(
                    tokenizer, model, user_question,
                    modo=persona,
                    verbose=False
                )
            else:
                use_of_rag = 'Sin RAG'
                response, prompt = answer_with_mori_plain(
                    tokenizer, model, user_question,
                    modo=persona
                )

            ss["last_prompt"] = prompt
            ss["just_generated"] = True

            # 🧠 Guarda historial
            hora_actual = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            ss.historial.append(("Tú", user_question, hora_actual))

            hora_actual = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            ss.historial.append(("Mori", response, hora_actual, use_of_rag, persona))

            # 💾 Guarda conversación
            saving_interaction(user_question, response, ss["user_id"], use_of_rag, persona)

            # 🟩 Guarda respuesta para mostrar después del rerun
            ss["_flash_response"] = response

            # 🧼 Limpieza del textarea en el próximo ciclo
            ss["_clear_entrada"] = True

            # ♻️ Forzar refresh (sidebar verá el nuevo prompt)
            st.rerun()

    # -----------------------------------------------------------
    # 💬 Mostrar la respuesta actual (flash) justo aquí ↓↓↓
    # -----------------------------------------------------------
    if _flash:
        st.success(_flash)

    # 🔁 Historial con estilo chat y contenedor con scroll
    if ss.historial:
        st.markdown("---")

        # 💾 Botón de descarga arriba del historial
        lineas = []
        for msg in reversed(ss.historial):
            if len(msg) == 5:
                autor, texto, hora, rag, bot_per = msg
                lineas.append(f"[{hora}] {autor}: {texto} RAG:{rag} Persoality:{bot_per}")
            else:
                autor, texto, hora = msg
                lineas.append(f"[{hora}] {autor}: {texto}")
        texto_chat = "\n\n".join(lineas)

        st.download_button(
            label="💾 Descargar conversación como .txt",
            data=texto_chat,
            file_name="conversacion_mori.txt",
            mime="text/plain",
            use_container_width=True
        )

        # 🪟 Contenedor con scroll y burbujas
        st.markdown(
            """
            <div id="chat-container" style="
                max-height: 400px;
                overflow-y: auto;
                padding: 10px;
                border: 1px solid #333;
                border-radius: 10px;
                background: linear-gradient(180deg, #0e0e0e 0%, #1b1b1b 100%);
                margin-top: 10px;
            ">
            """,
            unsafe_allow_html=True
        )

        for msg in reversed(ss.historial):
            if len(msg) == 5:
                autor, texto, hora, rag, bot_per = msg
            else:
                autor, texto, hora = msg

            if autor == "Tú":
                st.markdown(
                    f"""
                    <div style="
                        text-align: right;
                        background-color: #2d2d2d;
                        color: #e6e6e6;
                        padding: 10px 14px;
                        border-radius: 12px;
                        margin: 6px 0;
                        border: 1px solid #3a3a3a;
                        display: inline-block;
                        max-width: 80%;
                        float: right;
                        clear: both;
                    ">
                        🧍‍♂️ <b>{autor}:</b> {texto}
                    </div>
                    """,
                    unsafe_allow_html=True
                )
            else:
                st.markdown(
                    f"""
                    <div style="
                        text-align: left;
                        background-color: #162b1f;
                        color: #d9ead3;
                        padding: 10px 14px;
                        border-radius: 12px;
                        margin: 6px 0;
                        border: 1px solid #264d36;
                        display: inline-block;
                        max-width: 80%;
                        float: left;
                        clear: both;
                    ">
                        🤖 <b>{autor}:</b> {texto}
                    </div>
                    """,
                    unsafe_allow_html=True
                )

        st.markdown("</div>", unsafe_allow_html=True)