tecuhtli commited on
Commit
3cbf6dc
·
verified ·
1 Parent(s): dab33dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +635 -635
app.py CHANGED
@@ -1,635 +1,635 @@
1
- #***************************************************************************
2
- # Mori (tech-only) — Streamlit App sin sidebar ni social, con RAG opcional
3
- #***************************************************************************
4
- import os, re, json, csv, uuid, unicodedata, faiss, random
5
- import numpy as np
6
- os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
7
-
8
- import streamlit as st
9
- import datetime as dt
10
- from pathlib import Path
11
- import torch
12
-
13
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
14
- from huggingface_hub import hf_hub_download
15
- from sentence_transformers import SentenceTransformer # RAG embeddings
16
-
17
- # =========================
18
- # Configuración general
19
- # =========================
20
- HF_TOKEN = os.environ.get("HF_TOKEN") # Token privado (colócalo en Secrets o variable de entorno)
21
- RAG_REPO_ID = "tecuhtli/Mori_FAISS_Full" # Dataset privado con mori.faiss, mori_ids.npy, mori_metas.json
22
-
23
- # =========================
24
- # Utilidades de texto
25
- # =========================
26
- def truncate_sentences(text: str, max_sentences: int = 4) -> str:
27
- _SENT_SPLIT = re.compile(r'(?<=[\.\!\?…])\s+')
28
- s = text.strip()
29
- if not s:
30
- return s
31
- parts = _SENT_SPLIT.split(s)
32
- cut = " ".join(parts[:max_sentences]).strip()
33
- if cut and cut[-1] not in ".!?…":
34
- cut += "."
35
- return cut
36
-
37
- def _load_json_safe(path: Path, fallback: dict) -> dict:
38
- try:
39
- with open(path, "r", encoding="utf-8") as f:
40
- return json.load(f)
41
- except Exception:
42
- return fallback
43
-
44
- def load_prompt_cases():
45
- base = Path("Prompts")
46
- tech = _load_json_safe(base / "prompts_technical.json", {"modes": {}})
47
- social = _load_json_safe(base / "prompts_social.json", {"modes": {}}) # no usado, se deja por compatibilidad
48
- return {"technical": tech, "social": social}
49
-
50
- def polish_spanish(s: str) -> str:
51
- s = unicodedata.normalize("NFC", s).strip()
52
- s = re.sub(r'\s*[\[\(]\s*Mori\s+(?:Social|T[eé]nico|T[eé]cnico)\s*[\]\)]\s*', '', s, flags=re.I)
53
- fixes = [
54
- (r'(?i)(^|\W)T\s+puedes(?P<p>[^\w]|$)', r'\1Tú puedes\g<p>'),
55
- (r'(?i)(^|\W)T\s+(ya|eres|estas|estás|tienes|puedes)\b', r'\1Tú \2'),
56
- (r'(?i)\bclaro que s(?:i|í)?\b(?P<p>[,.\!?…])?', r'Claro que sí\g<p>'),
57
- (r'(?i)(^|\s)si,', r'\1Sí,'),
58
- (r'(?i)(\beso\s+)s(\s+est[áa]\b)', r'\1sí\2'),
59
- (r'(?i)(^|[\s,;:])s(\s+es\b)', r'\1sí\2'),
60
- (r'(?i)\btiles\b', 'útiles'),
61
- (r'(?i)\butiles\b', 'útiles'),
62
- (r'(?i)\butil\b', 'útil'),
63
- (r'(?i)\baqui\b', 'aquí'),
64
- (r'(?i)\balgn\b', 'algún'),
65
- (r'(?i)\bAnimo\b', 'Ánimo'),
66
- (r'(?i)\baprendisaje\b', 'aprendizaje'),
67
- (r'(?i)\bmanana\b', 'mañana'),
68
- (r'(?i)\benergia\b', 'energía'),
69
- (r'(?i)\bextrano\b', 'extraño'),
70
- (r'(?i)\bextrana\b', 'extraña'),
71
- (r'(?i)\bextranar\b', 'extrañar'),
72
- (r'(?i)\bextranarte\b', 'extrañarte'),
73
- (r'(?i)\bextranas\b', 'extrañas'),
74
- (r'(?i)\bextranos\b', 'extraños'),
75
- (r'(?i)\bestare\b', 'estaré'),
76
- (r'(?i)\bclarin\b', 'clarín'),
77
- (r'(?i)\bclar[íi]n\s+cornetas\b', 'clarín cornetas'),
78
- (r'(?i)(^|\s)s([,.;:!?])', r'\1Sí\2'),
79
- (r'(?i)\bfutbol\b', 'fútbol'),
80
- (r'(?i)(^|\s)as(\s+se\b)', r'\1Así\2'),
81
- (r'(?i)\bbuen dia\b', 'buen día'),
82
- (r'(?i)\bgran dia\b', 'gran día'),
83
- (r'(?i)\bdias\b', 'días'),
84
- (r'(?i)\bdia\b', 'día'),
85
- (r'(?i)\bacompa?a(r|rte|do|da|dos|das)?\b', r'acompaña\1'),
86
- (r'(?i)(^|\s)S lo se\b', r'\1Sí lo sé'),
87
- (r'(?i)\bcuidate\b', 'cuídate'),
88
- (r'(?i)\bcuidese\b', 'cuídese'),
89
- (r'(?i)\bcuidense\b', 'cuídense'),
90
- (r'(?i)\bgracias por confiar en m\b', 'gracias por confiar en mí'),
91
- (r'(?i)\bcada dia\b', 'cada día'),
92
- (r'(?i)\bsegun\b', 'según'),
93
- (r'(?i)\bcaracteristica(s)?\b', r'característica\1'),
94
- (r'(?i)\bcaracterstica(s)?\b', r'característica\1'),
95
- (r'(?i)\b([a-záéíóúñ]+)cion\b', r'\1ción'),
96
- (r'(?i)\bdeterminacio\b', 'determinación'),
97
- ]
98
- for pat, rep in fixes:
99
- s = re.sub(pat, rep, s)
100
- s = re.sub(r'(?i)^eso es todo!(?P<r>(\s|$).*)', r'¡Eso es todo!\g<r>', s)
101
- s = re.sub(r'\s+', ' ', s).strip()
102
- if s and s[-1] not in ".!?…":
103
- s += "."
104
- return s
105
-
106
- def normalize_for_route(s: str) -> str:
107
- s = unicodedata.normalize("NFKD", s)
108
- s = "".join(ch for ch in s if not unicodedata.combining(ch))
109
- s = re.sub(r"[^\w\s-]", " ", s, flags=re.UNICODE)
110
- s = re.sub(r"\s+", " ", s).strip().lower()
111
- return s
112
-
113
- def anti_echo(response: str, user_text: str) -> str:
114
- rn = normalize_for_route(response)
115
- un = normalize_for_route(user_text)
116
- def _clean_leading(s: str) -> str:
117
- s = re.sub(r'^\s*[,;:\-–—]\s*', '', s)
118
- s = re.sub(r'^\s+', '', s)
119
- return s
120
- if len(un) >= 4 and rn.startswith(un):
121
- cut = re.sub(r'^\s*[^,;:\.\!\?]{0,120}[,;:\-]\s*', '', response).lstrip()
122
- if cut and cut != response:
123
- return _clean_leading(cut)
124
- return _clean_leading(response[len(user_text):])
125
- return response
126
-
127
- # =========================
128
- # Prompting técnico
129
- # =========================
130
- def build_prompt_from_cases(domain: str,
131
- prompt_type: str,
132
- persona: str,
133
- question: str,
134
- context: str | None = None) -> str:
135
- key_map = {
136
- "Zero-shot": "zero_shot",
137
- "One-shot": "one_shot",
138
- "Few-shot (3)": "few_shot_3"
139
- }
140
- mode_key = key_map.get(prompt_type, "zero_shot")
141
- data = st.session_state.PROMPT_CASES.get(domain, {}).get("modes", {}).get(mode_key, {})
142
-
143
- tone = data.get("tone", "")
144
- out_fmt = data.get("output_format", "")
145
- rules = "\n- ".join(data.get("rules", []))
146
- ctx_line = f"\n- Contexto: {context}" if context else ""
147
-
148
- # ejemplos si hay
149
- examples = data.get("examples", [])
150
- ex_str = ""
151
- if examples:
152
- parts = []
153
- for i, ex in enumerate(examples, 1):
154
- parts.append(f"Ejemplo {i} →\nPregunta: {ex.get('input','')}\nRespuesta: {ex.get('output','')}")
155
- ex_str = "\n\n" + "\n\n".join(parts) + "\n\nAhora responde:"
156
-
157
- # prompt final (siempre técnico)
158
- prompt = (
159
- f"Tarea: {data.get('instruction','Responde como asistente técnico en procesamiento de datos.')}\n"
160
- f"Reglas:\n- {rules}{ctx_line}\n"
161
- f"Estilo: {tone}\n"
162
- f"Formato de salida: {out_fmt}\n"
163
- f"{ex_str}\n"
164
- f"pregunta={question}\n"
165
- )
166
- return prompt.strip()
167
-
168
- def set_seeds(seed: int = 42):
169
- random.seed(seed); np.random.seed(seed); torch.manual_seed(seed)
170
- if torch.cuda.is_available():
171
- torch.cuda.manual_seed_all(seed)
172
- torch.backends.cudnn.deterministic = True
173
- torch.backends.cudnn.benchmark = False
174
-
175
- # =========================
176
- # RAG helpers
177
- # =========================
178
- @st.cache_resource
179
- def load_rag_assets(device_str: str = "cpu"):
180
- """
181
- Carga E5 + FAISS + metadatos desde Hugging Face (dataset privado).
182
- """
183
- token = os.getenv("HF_TOKEN")
184
- if not token:
185
- st.warning("⚠️ No se encontró HF_TOKEN; RAG no estará disponible.")
186
- return None, None, None
187
-
188
- try:
189
- faiss_path = hf_hub_download(repo_id=RAG_REPO_ID, filename="mori.faiss", repo_type="dataset", token=token)
190
- ids_path = hf_hub_download(repo_id=RAG_REPO_ID, filename="mori_ids.npy", repo_type="dataset", token=token)
191
- meta_path = hf_hub_download(repo_id=RAG_REPO_ID, filename="mori_metas.json", repo_type="dataset", token=token)
192
-
193
- index = faiss.read_index(faiss_path)
194
- _ = np.load(ids_path, allow_pickle=True) # ids no usados explícitamente, se conserva por consistencia
195
- with open(meta_path, "r", encoding="utf-8") as f:
196
- metas = json.load(f)
197
-
198
- e5 = SentenceTransformer("intfloat/multilingual-e5-base", device=device_str)
199
- st.info(f"✅ RAG cargado con {index.ntotal} vectores.")
200
- return e5, index, metas
201
- except Exception as e:
202
- st.error(f"❌ Error al cargar RAG: {e}")
203
- return None, None, None
204
-
205
- def rag_retrieve(e5, index, metas, user_text: str, k: int = 5):
206
- if e5 is None or index is None or metas is None or index.ntotal == 0:
207
- return []
208
- qv = e5.encode([f"query: {user_text}"], normalize_embeddings=True,
209
- convert_to_numpy=True).astype("float32")
210
- k = max(1, min(int(k), index.ntotal))
211
- scores, idxs = index.search(qv, k)
212
- out = []
213
- for rank, (s, i) in enumerate(zip(scores[0], idxs[0]), 1):
214
- if i == -1:
215
- continue
216
- m = metas[i]
217
- out.append({
218
- "rank": rank, "score": float(s),
219
- "id": m.get("id",""),
220
- "canonical_term": m.get("canonical_term",""),
221
- "context": m.get("context",""),
222
- "input": m.get("input",""),
223
- "output": m.get("output",""),
224
- })
225
- return out
226
-
227
- def build_rag_prompt_technical(base_prompt: str, user_text: str, passages):
228
- ev_lines = []
229
- for p in passages:
230
- ev_lines.append(
231
- f"[{p['rank']}] term='{p.get('canonical_term','')}' ctx='{p.get('context','')}'\n"
232
- f"input: {p.get('input','')}\n"
233
- f"output: {p.get('output','')}"
234
- )
235
- ev_block = "\n".join(ev_lines)
236
- rag_rules = (
237
- "\n\n[ Modo RAG ]\n"
238
- "- Usa EXCLUSIVAMENTE la información relevante de las evidencias.\n"
239
- "- Si algo no aparece en las evidencias, dilo explícitamente.\n"
240
- "- Cita las evidencias con [n] (ej. [1], [3]).\n"
241
- )
242
- return f"{base_prompt.strip()}\n{rag_rules}\nEVIDENCIAS:\n{ev_block}\n"
243
-
244
- def get_bad_words_ids(tok):
245
- bad = []
246
- for sym in ["[", "]"]:
247
- ids = tok.encode(sym, add_special_tokens=False)
248
- if ids and all(isinstance(t, int) and t >= 0 for t in ids):
249
- bad.append(ids)
250
- return bad
251
-
252
- # =========================
253
- # Generación técnica
254
- # =========================
255
- def technical_asnwer(question, context, model, tokenizer, device, gen_params=None):
256
- model = model.to(device).eval()
257
-
258
- persona_name = (gen_params or {}).get("persona", st.session_state.get("persona", "Mori Normal"))
259
- prompt_type = st.session_state.get("prompt_type", "Zero-shot")
260
-
261
- input_text = build_prompt_from_cases(
262
- domain="technical",
263
- prompt_type=prompt_type,
264
- persona=persona_name,
265
- question=question,
266
- context=context
267
- )
268
-
269
- st.session_state["last_prompt"] = input_text
270
- st.session_state["just_generated"] = True
271
-
272
- enc = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
273
-
274
- bad_words = ["["]
275
- bad_ids = [tokenizer(bw, add_special_tokens=False).input_ids for bw in bad_words]
276
-
277
- max_new = int((gen_params or {}).get("max_new_tokens", 128))
278
- min_new = int((gen_params or {}).get("min_tokens", 16))
279
- no_repeat = int((gen_params or {}).get("no_repeat_ngram_size", 3))
280
- rep_pen = float((gen_params or {}).get("repetition_penalty", 1.0))
281
- mode = (gen_params or {}).get("mode", "beam")
282
-
283
- eos_id = tokenizer.eos_token_id or tokenizer.convert_tokens_to_ids("</s>")
284
- pad_id = tokenizer.pad_token_id or eos_id
285
-
286
- if mode == "sampling":
287
- temperature = float((gen_params or {}).get("temperature", 0.8))
288
- top_p = float((gen_params or {}).get("top_p", 0.9))
289
- kwargs = dict(
290
- do_sample=True, num_beams=1,
291
- temperature=max(0.1, temperature),
292
- top_p=min(1.0, max(0.5, top_p)),
293
- max_new_tokens=max_new,
294
- min_new_tokens=max(0, min_new),
295
- no_repeat_ngram_size=no_repeat,
296
- repetition_penalty=max(1.0, rep_pen),
297
- bad_words_ids=bad_ids,
298
- eos_token_id=eos_id,
299
- pad_token_id=pad_id,
300
- )
301
- else:
302
- num_beams = max(2, int((gen_params or {}).get("num_beams", 4)))
303
- length_penalty = float((gen_params or {}).get("length_penalty", 1.0))
304
- kwargs = dict(
305
- do_sample=False, num_beams=num_beams, length_penalty=length_penalty,
306
- max_new_tokens=max_new,
307
- min_new_tokens=max(0, min_new),
308
- no_repeat_ngram_size=no_repeat,
309
- repetition_penalty=max(1.0, rep_pen),
310
- bad_words_ids=bad_ids,
311
- eos_token_id=eos_id,
312
- pad_token_id=pad_id,
313
- )
314
-
315
- out_ids = model.generate(
316
- input_ids=enc["input_ids"], attention_mask=enc["attention_mask"], **kwargs
317
- )
318
- text = tokenizer.decode(out_ids[0], skip_special_tokens=True)
319
-
320
- if persona_name == "Mori Normal":
321
- text = truncate_sentences(text, max_sentences=1)
322
-
323
- st.session_state["last_response"] = text
324
- return polish_spanish(text)
325
-
326
- def technical_answer_rag(
327
- question, tec_model, tec_tok, device, gen_params,
328
- e5, index, metas, k=5, sim_threshold=0.40
329
- ):
330
- passages = rag_retrieve(e5, index, metas, question, k=k)
331
- if not passages:
332
- return "No encontré evidencias relevantes para responder con certeza. ¿Puedes dar más contexto?"
333
-
334
- persona_name = (gen_params or {}).get("persona", st.session_state.get("persona", "Mori Normal"))
335
- _ = st.session_state.get("prompt_type", "Zero-shot") # guardado por compatibilidad
336
-
337
- base_prompt = build_prompt_from_cases(
338
- domain="technical",
339
- prompt_type="Zero-shot",
340
- persona=persona_name,
341
- question=question,
342
- context="RAG"
343
- )
344
-
345
- prompt = build_rag_prompt_technical(base_prompt, question, passages)
346
-
347
- max_sim = passages[0]["score"]
348
- if max_sim < sim_threshold:
349
- prompt = "⚠️ Baja similitud con la base; podría faltar contexto.\n\n" + prompt
350
- st.session_state["last_prompt"] = prompt
351
- st.session_state["just_generated"] = True
352
-
353
- enc = tec_tok(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
354
-
355
- bad_ids = get_bad_words_ids(tec_tok)
356
-
357
- max_new = int((gen_params or {}).get("max_new_tokens", 128))
358
- min_new = int((gen_params or {}).get("min_tokens", 16))
359
- no_repeat = int((gen_params or {}).get("no_repeat_ngram_size", 3))
360
- rep_pen = float((gen_params or {}).get("repetition_penalty", 1.0))
361
- mode = (gen_params or {}).get("mode", "beam")
362
-
363
- eos_id = tec_tok.eos_token_id or tec_tok.convert_tokens_to_ids("</s>")
364
- pad_id = tec_tok.pad_token_id or eos_id
365
-
366
- if mode == "sampling":
367
- temperature = float((gen_params or {}).get("temperature", 0.8))
368
- top_p = float((gen_params or {}).get("top_p", 0.9))
369
- kwargs = dict(
370
- do_sample=True, num_beams=1,
371
- temperature=max(0.1, temperature),
372
- top_p=min(1.0, max(0.5, top_p)),
373
- max_new_tokens=max_new,
374
- min_new_tokens=max(0, min_new),
375
- no_repeat_ngram_size=no_repeat,
376
- repetition_penalty=max(1.0, rep_pen),
377
- eos_token_id=eos_id,
378
- pad_token_id=pad_id,
379
- )
380
- else:
381
- num_beams = max(2, int((gen_params or {}).get("num_beams", 4)))
382
- length_penalty = float((gen_params or {}).get("length_penalty", 1.0))
383
- kwargs = dict(
384
- do_sample=False, num_beams=num_beams, length_penalty=length_penalty,
385
- max_new_tokens=max_new,
386
- min_new_tokens=max(0, min_new),
387
- no_repeat_ngram_size=no_repeat,
388
- repetition_penalty=max(1.0, rep_pen),
389
- eos_token_id=eos_id,
390
- pad_token_id=pad_id,
391
- )
392
-
393
- if bad_ids:
394
- kwargs["bad_words_ids"] = bad_ids
395
-
396
- out_ids = tec_model.generate(**enc, **kwargs)
397
- text = tec_tok.decode(out_ids[0], skip_special_tokens=True)
398
-
399
- if persona_name == "Mori Normal":
400
- text = truncate_sentences(text, max_sentences=1)
401
- text = polish_spanish(text)
402
-
403
- st.session_state["last_response"] = text
404
- return text
405
-
406
- # =========================
407
- # Persistencia simple
408
- # =========================
409
- def saving_interaction(question, response, context, user_id):
410
- timestamp = dt.datetime.now().isoformat()
411
- stats_dir = Path("Statistics")
412
- stats_dir.mkdir(parents=True, exist_ok=True)
413
-
414
- archivo_csv = stats_dir / "conversaciones_log.csv"
415
- existe_csv = archivo_csv.exists()
416
-
417
- with open(archivo_csv, mode="a", encoding="utf-8", newline="") as f_csv:
418
- writer = csv.writer(f_csv)
419
- if not existe_csv:
420
- writer.writerow(["timestamp", "user_id", "contexto", "pregunta", "respuesta"])
421
- writer.writerow([timestamp, user_id, context, question, response])
422
-
423
- archivo_jsonl = stats_dir / "conversaciones_log.jsonl"
424
- with open(archivo_jsonl, mode="a", encoding="utf-8") as f_jsonl:
425
- registro = {
426
- "timestamp": timestamp,
427
- "user_id": user_id,
428
- "context": context,
429
- "pregunta": question,
430
- "respuesta": response
431
- }
432
- f_jsonl.write(json.dumps(registro, ensure_ascii=False) + "\n")
433
-
434
- # =========================
435
- # Enrutador técnico único
436
- # =========================
437
- def answer_technical_only(user_text: str, device, gen_params,
438
- tec_model, tec_tok):
439
- # Intentar RAG si está activado
440
- use_rag = st.session_state.get("use_rag", True)
441
- if use_rag:
442
- e5, index, metas = load_rag_assets("cuda" if torch.cuda.is_available() else "cpu")
443
- if e5 is not None and index is not None and index.ntotal > 0:
444
- return technical_answer_rag(
445
- user_text, tec_model, tec_tok, device, gen_params,
446
- e5=e5, index=index, metas=metas,
447
- k=st.session_state.get("rag_k", 3), sim_threshold=0.40
448
- )
449
- # Fallback sin RAG
450
- return technical_asnwer(
451
- question=user_text,
452
- context="procesamiento de datos",
453
- model=tec_model, tokenizer=tec_tok, device=device,
454
- gen_params=gen_params
455
- )
456
-
457
- # =========================
458
- # MAIN
459
- # =========================
460
- if __name__ == '__main__':
461
- # Estado persistente
462
- ss = st.session_state
463
- ss.setdefault("historial", [])
464
- ss.setdefault("last_prompt", "")
465
- ss.setdefault("last_response", "")
466
- ss.setdefault("just_generated", False)
467
-
468
- # Prompt cases y presets (sin sidebar)
469
- if "PROMPT_CASES" not in ss:
470
- ss.PROMPT_CASES = load_prompt_cases()
471
-
472
- ss.setdefault("persona", "Mori Normal")
473
- ss.setdefault("prompt_type", "Zero-shot")
474
- ss.setdefault("use_rag", True)
475
- ss.setdefault("rag_k", 3)
476
-
477
- GEN_PARAMS = {
478
- "persona": ss.get("persona", "Mori Normal"),
479
- "mode": "beam", # 'beam' | 'sampling'
480
- "max_new_tokens": 128,
481
- "min_tokens": 16,
482
- "no_repeat_ngram_size": 3,
483
- "num_beams": 4,
484
- "length_penalty": 1.0,
485
- "temperature": 0.8, # usado solo si mode == "sampling"
486
- "top_p": 0.9, # usado solo si mode == "sampling"
487
- "repetition_penalty": 1.0,
488
- "seed": 42,
489
- }
490
-
491
- # ID de sesión
492
- if "user_id" not in ss:
493
- ss["user_id"] = str(uuid.uuid4())[:8]
494
-
495
- # Carga del modelo técnico
496
- tec_tok = AutoTokenizer.from_pretrained("tecuhtli/mori-tecnico-model", use_auth_token=HF_TOKEN)
497
- tec_model = AutoModelForSeq2SeqLM.from_pretrained("tecuhtli/mori-tecnico-model", use_auth_token=HF_TOKEN)
498
-
499
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
500
-
501
- # Presentación (solo técnico)
502
- st.title("🤖 Mori — Asistente Técnico en Procesamiento de Datos")
503
- st.caption("🙋🏽‍ Pregunta sobre: limpieza, features, evaluación, modelos, MLOps, BI, visualización, etc.")
504
- st.caption("➡️ Ejemplos: Define X, ¿Para qué sirve Y?, Explícame Z, Diferencia entre A y B, ¿Cómo implemento ...?")
505
- st.markdown("<br>", unsafe_allow_html=True)
506
- st.caption("✏️ Escribe 'salir' para terminar.")
507
-
508
- # Limpieza previa del textarea si corresponde
509
- if ss.pop("_clear_entrada", False):
510
- if "entrada" in ss:
511
- del ss["entrada"]
512
-
513
- # Respuesta flash de ciclo anterior
514
- _flash = ss.pop("_flash_response", None)
515
-
516
- # Formulario
517
- with st.form("formulario_mori"):
518
- user_question = st.text_area("📝 Escribe tu pregunta aquí", key="entrada", height=100)
519
- submitted = st.form_submit_button("Responder")
520
-
521
- if submitted:
522
- if not user_question:
523
- st.info("Mori: ¿Podrías repetir eso? No entendí bien 😅")
524
- else:
525
- response = answer_technical_only(user_question, device, GEN_PARAMS, tec_model, tec_tok)
526
-
527
- # Historial
528
- hora_actual = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
529
- ss.historial.append(("Tú", user_question, hora_actual))
530
- hora_actual = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
531
- ss.historial.append(("Mori", response, hora_actual))
532
-
533
- # Guardado persistente
534
- saving_interaction(user_question, response, "procesamiento de datos", ss["user_id"])
535
-
536
- # Flash y limpieza
537
- ss["_flash_response"] = response
538
- ss["_clear_entrada"] = True
539
- st.rerun()
540
-
541
- # Mostrar respuesta flash
542
- if _flash:
543
- st.success(_flash)
544
-
545
- # Historial + descarga
546
- if ss.historial:
547
- st.markdown("---")
548
-
549
- lineas = []
550
- for msg in reversed(ss.historial):
551
- if len(msg) == 3:
552
- autor, texto, hora = msg
553
- lineas.append(f"[{hora}] {autor}: {texto}")
554
- else:
555
- autor, texto = msg
556
- lineas.append(f"{autor}: {texto}")
557
- texto_chat = "\n\n".join(lineas)
558
-
559
- st.download_button(
560
- label="💾 Descargar conversación como .txt",
561
- data=texto_chat,
562
- file_name="conversacion_mori.txt",
563
- mime="text/plain",
564
- use_container_width=True
565
- )
566
-
567
- # Contenedor con estilo
568
- st.markdown(
569
- """
570
- <div id="chat-container" style="
571
- max-height: 400px;
572
- overflow-y: auto;
573
- padding: 10px;
574
- border: 1px solid #333;
575
- border-radius: 10px;
576
- background: linear-gradient(180deg, #0e0e0e 0%, #1b1b1b 100%);
577
- margin-top: 10px;
578
- ">
579
- """,
580
- unsafe_allow_html=True
581
- )
582
-
583
- for msg in reversed(ss.historial):
584
- if len(msg) == 3:
585
- autor, texto, _ = msg
586
- else:
587
- autor, texto = msg
588
-
589
- if autor == "Tú":
590
- st.markdown(
591
- f"""
592
- <div style="
593
- text-align: right;
594
- background-color: #2d2d2d;
595
- color: #e6e6e6;
596
- padding: 10px 14px;
597
- border-radius: 12px;
598
- margin: 6px 0;
599
- border: 1px solid #3a3a3a;
600
- display: inline-block;
601
- max-width: 80%;
602
- float: right;
603
- clear: both;
604
- ">
605
- 🧍‍♂️ <b>{autor}:</b> {texto}
606
- </div>
607
- """,
608
- unsafe_allow_html=True
609
- )
610
- else:
611
- st.markdown(
612
- f"""
613
- <div style="
614
- text-align: left;
615
- background-color: #162b1f;
616
- color: #d9ead3;
617
- padding: 10px 14px;
618
- border-radius: 12px;
619
- margin: 6px 0;
620
- border: 1px solid #264d36;
621
- display: inline-block;
622
- max-width: 80%;
623
- float: left;
624
- clear: both;
625
- ">
626
- 🤖 <b>{autor}:</b> {texto}
627
- </div>
628
- """,
629
- unsafe_allow_html=True
630
- )
631
-
632
- st.markdown("</div>", unsafe_allow_html=True)
633
- #***************************************************************************
634
- # FIN
635
- #***************************************************************************
 
1
+ #***************************************************************************
2
+ # Mori (tech-only) — Streamlit App sin sidebar ni social, con RAG opcional
3
+ #***************************************************************************
4
+ import os, re, json, csv, uuid, unicodedata, faiss, random
5
+ import numpy as np
6
+ os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
7
+
8
+ import streamlit as st
9
+ import datetime as dt
10
+ from pathlib import Path
11
+ import torch
12
+
13
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
14
+ from huggingface_hub import hf_hub_download
15
+ from sentence_transformers import SentenceTransformer # RAG embeddings
16
+
17
+ # =========================
18
+ # Configuración general
19
+ # =========================
20
+ HF_TOKEN = os.environ.get("HF_TOKEN") # Token privado (colócalo en Secrets o variable de entorno)
21
+ RAG_REPO_ID = "tecuhtli/Mori_FAISS_Full" # Dataset privado con mori.faiss, mori_ids.npy, mori_metas.json
22
+
23
+ # =========================
24
+ # Utilidades de texto
25
+ # =========================
26
+ def truncate_sentences(text: str, max_sentences: int = 4) -> str:
27
+ _SENT_SPLIT = re.compile(r'(?<=[\.\!\?…])\s+')
28
+ s = text.strip()
29
+ if not s:
30
+ return s
31
+ parts = _SENT_SPLIT.split(s)
32
+ cut = " ".join(parts[:max_sentences]).strip()
33
+ if cut and cut[-1] not in ".!?…":
34
+ cut += "."
35
+ return cut
36
+
37
+ def _load_json_safe(path: Path, fallback: dict) -> dict:
38
+ try:
39
+ with open(path, "r", encoding="utf-8") as f:
40
+ return json.load(f)
41
+ except Exception:
42
+ return fallback
43
+
44
+ def load_prompt_cases():
45
+ base = Path("Prompts")
46
+ tech = _load_json_safe(base / "prompts_technical.json", {"modes": {}})
47
+ social = _load_json_safe(base / "prompts_social.json", {"modes": {}}) # no usado, se deja por compatibilidad
48
+ return {"technical": tech, "social": social}
49
+
50
+ def polish_spanish(s: str) -> str:
51
+ s = unicodedata.normalize("NFC", s).strip()
52
+ s = re.sub(r'\s*[\[\(]\s*Mori\s+(?:Social|T[eé]nico|T[eé]cnico)\s*[\]\)]\s*', '', s, flags=re.I)
53
+ fixes = [
54
+ (r'(?i)(^|\W)T\s+puedes(?P<p>[^\w]|$)', r'\1Tú puedes\g<p>'),
55
+ (r'(?i)(^|\W)T\s+(ya|eres|estas|estás|tienes|puedes)\b', r'\1Tú \2'),
56
+ (r'(?i)\bclaro que s(?:i|í)?\b(?P<p>[,.\!?…])?', r'Claro que sí\g<p>'),
57
+ (r'(?i)(^|\s)si,', r'\1Sí,'),
58
+ (r'(?i)(\beso\s+)s(\s+est[áa]\b)', r'\1sí\2'),
59
+ (r'(?i)(^|[\s,;:])s(\s+es\b)', r'\1sí\2'),
60
+ (r'(?i)\btiles\b', 'útiles'),
61
+ (r'(?i)\butiles\b', 'útiles'),
62
+ (r'(?i)\butil\b', 'útil'),
63
+ (r'(?i)\baqui\b', 'aquí'),
64
+ (r'(?i)\balgn\b', 'algún'),
65
+ (r'(?i)\bAnimo\b', 'Ánimo'),
66
+ (r'(?i)\baprendisaje\b', 'aprendizaje'),
67
+ (r'(?i)\bmanana\b', 'mañana'),
68
+ (r'(?i)\benergia\b', 'energía'),
69
+ (r'(?i)\bextrano\b', 'extraño'),
70
+ (r'(?i)\bextrana\b', 'extraña'),
71
+ (r'(?i)\bextranar\b', 'extrañar'),
72
+ (r'(?i)\bextranarte\b', 'extrañarte'),
73
+ (r'(?i)\bextranas\b', 'extrañas'),
74
+ (r'(?i)\bextranos\b', 'extraños'),
75
+ (r'(?i)\bestare\b', 'estaré'),
76
+ (r'(?i)\bclarin\b', 'clarín'),
77
+ (r'(?i)\bclar[íi]n\s+cornetas\b', 'clarín cornetas'),
78
+ (r'(?i)(^|\s)s([,.;:!?])', r'\1Sí\2'),
79
+ (r'(?i)\bfutbol\b', 'fútbol'),
80
+ (r'(?i)(^|\s)as(\s+se\b)', r'\1Así\2'),
81
+ (r'(?i)\bbuen dia\b', 'buen día'),
82
+ (r'(?i)\bgran dia\b', 'gran día'),
83
+ (r'(?i)\bdias\b', 'días'),
84
+ (r'(?i)\bdia\b', 'día'),
85
+ (r'(?i)\bacompa?a(r|rte|do|da|dos|das)?\b', r'acompaña\1'),
86
+ (r'(?i)(^|\s)S lo se\b', r'\1Sí lo sé'),
87
+ (r'(?i)\bcuidate\b', 'cuídate'),
88
+ (r'(?i)\bcuidese\b', 'cuídese'),
89
+ (r'(?i)\bcuidense\b', 'cuídense'),
90
+ (r'(?i)\bgracias por confiar en m\b', 'gracias por confiar en mí'),
91
+ (r'(?i)\bcada dia\b', 'cada día'),
92
+ (r'(?i)\bsegun\b', 'según'),
93
+ (r'(?i)\bcaracteristica(s)?\b', r'característica\1'),
94
+ (r'(?i)\bcaracterstica(s)?\b', r'característica\1'),
95
+ (r'(?i)\b([a-záéíóúñ]+)cion\b', r'\1ción'),
96
+ (r'(?i)\bdeterminacio\b', 'determinación'),
97
+ ]
98
+ for pat, rep in fixes:
99
+ s = re.sub(pat, rep, s)
100
+ s = re.sub(r'(?i)^eso es todo!(?P<r>(\s|$).*)', r'¡Eso es todo!\g<r>', s)
101
+ s = re.sub(r'\s+', ' ', s).strip()
102
+ if s and s[-1] not in ".!?…":
103
+ s += "."
104
+ return s
105
+
106
+ def normalize_for_route(s: str) -> str:
107
+ s = unicodedata.normalize("NFKD", s)
108
+ s = "".join(ch for ch in s if not unicodedata.combining(ch))
109
+ s = re.sub(r"[^\w\s-]", " ", s, flags=re.UNICODE)
110
+ s = re.sub(r"\s+", " ", s).strip().lower()
111
+ return s
112
+
113
+ def anti_echo(response: str, user_text: str) -> str:
114
+ rn = normalize_for_route(response)
115
+ un = normalize_for_route(user_text)
116
+ def _clean_leading(s: str) -> str:
117
+ s = re.sub(r'^\s*[,;:\-–—]\s*', '', s)
118
+ s = re.sub(r'^\s+', '', s)
119
+ return s
120
+ if len(un) >= 4 and rn.startswith(un):
121
+ cut = re.sub(r'^\s*[^,;:\.\!\?]{0,120}[,;:\-]\s*', '', response).lstrip()
122
+ if cut and cut != response:
123
+ return _clean_leading(cut)
124
+ return _clean_leading(response[len(user_text):])
125
+ return response
126
+
127
+ # =========================
128
+ # Prompting técnico
129
+ # =========================
130
+ def build_prompt_from_cases(domain: str,
131
+ prompt_type: str,
132
+ persona: str,
133
+ question: str,
134
+ context: str | None = None) -> str:
135
+ key_map = {
136
+ "Zero-shot": "zero_shot",
137
+ "One-shot": "one_shot",
138
+ "Few-shot (3)": "few_shot_3"
139
+ }
140
+ mode_key = key_map.get(prompt_type, "zero_shot")
141
+ data = st.session_state.PROMPT_CASES.get(domain, {}).get("modes", {}).get(mode_key, {})
142
+
143
+ tone = data.get("tone", "")
144
+ out_fmt = data.get("output_format", "")
145
+ rules = "\n- ".join(data.get("rules", []))
146
+ ctx_line = f"\n- Contexto: {context}" if context else ""
147
+
148
+ # ejemplos si hay
149
+ examples = data.get("examples", [])
150
+ ex_str = ""
151
+ if examples:
152
+ parts = []
153
+ for i, ex in enumerate(examples, 1):
154
+ parts.append(f"Ejemplo {i} →\nPregunta: {ex.get('input','')}\nRespuesta: {ex.get('output','')}")
155
+ ex_str = "\n\n" + "\n\n".join(parts) + "\n\nAhora responde:"
156
+
157
+ # prompt final (siempre técnico)
158
+ prompt = (
159
+ f"Tarea: {data.get('instruction','Responde como asistente técnico en procesamiento de datos.')}\n"
160
+ f"Reglas:\n- {rules}{ctx_line}\n"
161
+ f"Estilo: {tone}\n"
162
+ f"Formato de salida: {out_fmt}\n"
163
+ f"{ex_str}\n"
164
+ f"pregunta={question}\n"
165
+ )
166
+ return prompt.strip()
167
+
168
+ def set_seeds(seed: int = 42):
169
+ random.seed(seed); np.random.seed(seed); torch.manual_seed(seed)
170
+ if torch.cuda.is_available():
171
+ torch.cuda.manual_seed_all(seed)
172
+ torch.backends.cudnn.deterministic = True
173
+ torch.backends.cudnn.benchmark = False
174
+
175
+ # =========================
176
+ # RAG helpers
177
+ # =========================
178
+ @st.cache_resource
179
+ def load_rag_assets(device_str: str = "cpu"):
180
+ """
181
+ Carga E5 + FAISS + metadatos desde Hugging Face (dataset privado).
182
+ """
183
+ token = os.getenv("HF_TOKEN")
184
+ if not token:
185
+ st.warning("⚠️ No se encontró HF_TOKEN; RAG no estará disponible.")
186
+ return None, None, None
187
+
188
+ try:
189
+ faiss_path = hf_hub_download(repo_id=RAG_REPO_ID, filename="mori.faiss", repo_type="dataset", token=token)
190
+ ids_path = hf_hub_download(repo_id=RAG_REPO_ID, filename="mori_ids.npy", repo_type="dataset", token=token)
191
+ meta_path = hf_hub_download(repo_id=RAG_REPO_ID, filename="mori_metas.json", repo_type="dataset", token=token)
192
+
193
+ index = faiss.read_index(faiss_path)
194
+ _ = np.load(ids_path, allow_pickle=True) # ids no usados explícitamente, se conserva por consistencia
195
+ with open(meta_path, "r", encoding="utf-8") as f:
196
+ metas = json.load(f)
197
+
198
+ e5 = SentenceTransformer("intfloat/multilingual-e5-base", device=device_str)
199
+ st.info(f"✅ RAG cargado con {index.ntotal} vectores.")
200
+ return e5, index, metas
201
+ except Exception as e:
202
+ st.error(f"❌ Error al cargar RAG: {e}")
203
+ return None, None, None
204
+
205
+ def rag_retrieve(e5, index, metas, user_text: str, k: int = 5):
206
+ if e5 is None or index is None or metas is None or index.ntotal == 0:
207
+ return []
208
+ qv = e5.encode([f"query: {user_text}"], normalize_embeddings=True,
209
+ convert_to_numpy=True).astype("float32")
210
+ k = max(1, min(int(k), index.ntotal))
211
+ scores, idxs = index.search(qv, k)
212
+ out = []
213
+ for rank, (s, i) in enumerate(zip(scores[0], idxs[0]), 1):
214
+ if i == -1:
215
+ continue
216
+ m = metas[i]
217
+ out.append({
218
+ "rank": rank, "score": float(s),
219
+ "id": m.get("id",""),
220
+ "canonical_term": m.get("canonical_term",""),
221
+ "context": m.get("context",""),
222
+ "input": m.get("input",""),
223
+ "output": m.get("output",""),
224
+ })
225
+ return out
226
+
227
+ def build_rag_prompt_technical(base_prompt: str, user_text: str, passages):
228
+ ev_lines = []
229
+ for p in passages:
230
+ ev_lines.append(
231
+ f"[{p['rank']}] term='{p.get('canonical_term','')}' ctx='{p.get('context','')}'\n"
232
+ f"input: {p.get('input','')}\n"
233
+ f"output: {p.get('output','')}"
234
+ )
235
+ ev_block = "\n".join(ev_lines)
236
+ rag_rules = (
237
+ "\n\n[ Modo RAG ]\n"
238
+ "- Usa EXCLUSIVAMENTE la información relevante de las evidencias.\n"
239
+ "- Si algo no aparece en las evidencias, dilo explícitamente.\n"
240
+ "- Cita las evidencias con [n] (ej. [1], [3]).\n"
241
+ )
242
+ return f"{base_prompt.strip()}\n{rag_rules}\nEVIDENCIAS:\n{ev_block}\n"
243
+
244
+ def get_bad_words_ids(tok):
245
+ bad = []
246
+ for sym in ["[", "]"]:
247
+ ids = tok.encode(sym, add_special_tokens=False)
248
+ if ids and all(isinstance(t, int) and t >= 0 for t in ids):
249
+ bad.append(ids)
250
+ return bad
251
+
252
+ # =========================
253
+ # Generación técnica
254
+ # =========================
255
+ def technical_asnwer(question, context, model, tokenizer, device, gen_params=None):
256
+ model = model.to(device).eval()
257
+
258
+ persona_name = (gen_params or {}).get("persona", st.session_state.get("persona", "Mori Normal"))
259
+ prompt_type = st.session_state.get("prompt_type", "Zero-shot")
260
+
261
+ input_text = build_prompt_from_cases(
262
+ domain="technical",
263
+ prompt_type=prompt_type,
264
+ persona=persona_name,
265
+ question=question,
266
+ context=context
267
+ )
268
+
269
+ st.session_state["last_prompt"] = input_text
270
+ st.session_state["just_generated"] = True
271
+
272
+ enc = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
273
+
274
+ bad_words = ["["]
275
+ bad_ids = [tokenizer(bw, add_special_tokens=False).input_ids for bw in bad_words]
276
+
277
+ max_new = int((gen_params or {}).get("max_new_tokens", 128))
278
+ min_new = int((gen_params or {}).get("min_tokens", 16))
279
+ no_repeat = int((gen_params or {}).get("no_repeat_ngram_size", 3))
280
+ rep_pen = float((gen_params or {}).get("repetition_penalty", 1.0))
281
+ mode = (gen_params or {}).get("mode", "beam")
282
+
283
+ eos_id = tokenizer.eos_token_id or tokenizer.convert_tokens_to_ids("</s>")
284
+ pad_id = tokenizer.pad_token_id or eos_id
285
+
286
+ if mode == "sampling":
287
+ temperature = float((gen_params or {}).get("temperature", 0.8))
288
+ top_p = float((gen_params or {}).get("top_p", 0.9))
289
+ kwargs = dict(
290
+ do_sample=True, num_beams=1,
291
+ temperature=max(0.1, temperature),
292
+ top_p=min(1.0, max(0.5, top_p)),
293
+ max_new_tokens=max_new,
294
+ min_new_tokens=max(0, min_new),
295
+ no_repeat_ngram_size=no_repeat,
296
+ repetition_penalty=max(1.0, rep_pen),
297
+ bad_words_ids=bad_ids,
298
+ eos_token_id=eos_id,
299
+ pad_token_id=pad_id,
300
+ )
301
+ else:
302
+ num_beams = max(2, int((gen_params or {}).get("num_beams", 4)))
303
+ length_penalty = float((gen_params or {}).get("length_penalty", 1.0))
304
+ kwargs = dict(
305
+ do_sample=False, num_beams=num_beams, length_penalty=length_penalty,
306
+ max_new_tokens=max_new,
307
+ min_new_tokens=max(0, min_new),
308
+ no_repeat_ngram_size=no_repeat,
309
+ repetition_penalty=max(1.0, rep_pen),
310
+ bad_words_ids=bad_ids,
311
+ eos_token_id=eos_id,
312
+ pad_token_id=pad_id,
313
+ )
314
+
315
+ out_ids = model.generate(
316
+ input_ids=enc["input_ids"], attention_mask=enc["attention_mask"], **kwargs
317
+ )
318
+ text = tokenizer.decode(out_ids[0], skip_special_tokens=True)
319
+
320
+ if persona_name == "Mori Normal":
321
+ text = truncate_sentences(text, max_sentences=1)
322
+
323
+ st.session_state["last_response"] = text
324
+ return polish_spanish(text)
325
+
326
+ def technical_answer_rag(
327
+ question, tec_model, tec_tok, device, gen_params,
328
+ e5, index, metas, k=5, sim_threshold=0.40
329
+ ):
330
+ passages = rag_retrieve(e5, index, metas, question, k=k)
331
+ if not passages:
332
+ return "No encontré evidencias relevantes para responder con certeza. ¿Puedes dar más contexto?"
333
+
334
+ persona_name = (gen_params or {}).get("persona", st.session_state.get("persona", "Mori Normal"))
335
+ _ = st.session_state.get("prompt_type", "Zero-shot") # guardado por compatibilidad
336
+
337
+ base_prompt = build_prompt_from_cases(
338
+ domain="technical",
339
+ prompt_type="Zero-shot",
340
+ persona=persona_name,
341
+ question=question,
342
+ context="RAG"
343
+ )
344
+
345
+ prompt = build_rag_prompt_technical(base_prompt, question, passages)
346
+
347
+ max_sim = passages[0]["score"]
348
+ if max_sim < sim_threshold:
349
+ prompt = "⚠️ Baja similitud con la base; podría faltar contexto.\n\n" + prompt
350
+ st.session_state["last_prompt"] = prompt
351
+ st.session_state["just_generated"] = True
352
+
353
+ enc = tec_tok(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
354
+
355
+ bad_ids = get_bad_words_ids(tec_tok)
356
+
357
+ max_new = int((gen_params or {}).get("max_new_tokens", 128))
358
+ min_new = int((gen_params or {}).get("min_tokens", 16))
359
+ no_repeat = int((gen_params or {}).get("no_repeat_ngram_size", 3))
360
+ rep_pen = float((gen_params or {}).get("repetition_penalty", 1.0))
361
+ mode = (gen_params or {}).get("mode", "beam")
362
+
363
+ eos_id = tec_tok.eos_token_id or tec_tok.convert_tokens_to_ids("</s>")
364
+ pad_id = tec_tok.pad_token_id or eos_id
365
+
366
+ if mode == "sampling":
367
+ temperature = float((gen_params or {}).get("temperature", 0.8))
368
+ top_p = float((gen_params or {}).get("top_p", 0.9))
369
+ kwargs = dict(
370
+ do_sample=True, num_beams=1,
371
+ temperature=max(0.1, temperature),
372
+ top_p=min(1.0, max(0.5, top_p)),
373
+ max_new_tokens=max_new,
374
+ min_new_tokens=max(0, min_new),
375
+ no_repeat_ngram_size=no_repeat,
376
+ repetition_penalty=max(1.0, rep_pen),
377
+ eos_token_id=eos_id,
378
+ pad_token_id=pad_id,
379
+ )
380
+ else:
381
+ num_beams = max(2, int((gen_params or {}).get("num_beams", 4)))
382
+ length_penalty = float((gen_params or {}).get("length_penalty", 1.0))
383
+ kwargs = dict(
384
+ do_sample=False, num_beams=num_beams, length_penalty=length_penalty,
385
+ max_new_tokens=max_new,
386
+ min_new_tokens=max(0, min_new),
387
+ no_repeat_ngram_size=no_repeat,
388
+ repetition_penalty=max(1.0, rep_pen),
389
+ eos_token_id=eos_id,
390
+ pad_token_id=pad_id,
391
+ )
392
+
393
+ if bad_ids:
394
+ kwargs["bad_words_ids"] = bad_ids
395
+
396
+ out_ids = tec_model.generate(**enc, **kwargs)
397
+ text = tec_tok.decode(out_ids[0], skip_special_tokens=True)
398
+
399
+ if persona_name == "Mori Normal":
400
+ text = truncate_sentences(text, max_sentences=1)
401
+ text = polish_spanish(text)
402
+
403
+ st.session_state["last_response"] = text
404
+ return text
405
+
406
+ # =========================
407
+ # Persistencia simple
408
+ # =========================
409
+ def saving_interaction(question, response, context, user_id):
410
+ timestamp = dt.datetime.now().isoformat()
411
+ stats_dir = Path("Statistics")
412
+ stats_dir.mkdir(parents=True, exist_ok=True)
413
+
414
+ archivo_csv = stats_dir / "conversaciones_log.csv"
415
+ existe_csv = archivo_csv.exists()
416
+
417
+ with open(archivo_csv, mode="a", encoding="utf-8", newline="") as f_csv:
418
+ writer = csv.writer(f_csv)
419
+ if not existe_csv:
420
+ writer.writerow(["timestamp", "user_id", "contexto", "pregunta", "respuesta"])
421
+ writer.writerow([timestamp, user_id, context, question, response])
422
+
423
+ archivo_jsonl = stats_dir / "conversaciones_log.jsonl"
424
+ with open(archivo_jsonl, mode="a", encoding="utf-8") as f_jsonl:
425
+ registro = {
426
+ "timestamp": timestamp,
427
+ "user_id": user_id,
428
+ "context": context,
429
+ "pregunta": question,
430
+ "respuesta": response
431
+ }
432
+ f_jsonl.write(json.dumps(registro, ensure_ascii=False) + "\n")
433
+
434
+ # =========================
435
+ # Enrutador técnico único
436
+ # =========================
437
+ def answer_technical_only(user_text: str, device, gen_params,
438
+ tec_model, tec_tok):
439
+ # Intentar RAG si está activado
440
+ use_rag = st.session_state.get("use_rag", True)
441
+ if use_rag:
442
+ e5, index, metas = load_rag_assets("cuda" if torch.cuda.is_available() else "cpu")
443
+ if e5 is not None and index is not None and index.ntotal > 0:
444
+ return technical_answer_rag(
445
+ user_text, tec_model, tec_tok, device, gen_params,
446
+ e5=e5, index=index, metas=metas,
447
+ k=st.session_state.get("rag_k", 3), sim_threshold=0.40
448
+ )
449
+ # Fallback sin RAG
450
+ return technical_asnwer(
451
+ question=user_text,
452
+ context="procesamiento de datos",
453
+ model=tec_model, tokenizer=tec_tok, device=device,
454
+ gen_params=gen_params
455
+ )
456
+
457
+ # =========================
458
+ # MAIN
459
+ # =========================
460
+ if __name__ == '__main__':
461
+ # Estado persistente
462
+ ss = st.session_state
463
+ ss.setdefault("historial", [])
464
+ ss.setdefault("last_prompt", "")
465
+ ss.setdefault("last_response", "")
466
+ ss.setdefault("just_generated", False)
467
+
468
+ # Prompt cases y presets (sin sidebar)
469
+ if "PROMPT_CASES" not in ss:
470
+ ss.PROMPT_CASES = load_prompt_cases()
471
+
472
+ ss.setdefault("persona", "Mori Normal")
473
+ ss.setdefault("prompt_type", "Zero-shot")
474
+ ss.setdefault("use_rag", True)
475
+ ss.setdefault("rag_k", 3)
476
+
477
+ GEN_PARAMS = {
478
+ "persona": ss.get("persona", "Mori Normal"),
479
+ "mode": "beam", # 'beam' | 'sampling'
480
+ "max_new_tokens": 128,
481
+ "min_tokens": 16,
482
+ "no_repeat_ngram_size": 3,
483
+ "num_beams": 4,
484
+ "length_penalty": 1.0,
485
+ "temperature": 0.8, # usado solo si mode == "sampling"
486
+ "top_p": 0.9, # usado solo si mode == "sampling"
487
+ "repetition_penalty": 1.0,
488
+ "seed": 42,
489
+ }
490
+
491
+ # ID de sesión
492
+ if "user_id" not in ss:
493
+ ss["user_id"] = str(uuid.uuid4())[:8]
494
+
495
+ # Modelo Técnico
496
+ tec_tok = AutoTokenizer.from_pretrained("tecuhtli/mori-tecnico-model", use_auth_token=HF_TOKEN)
497
+ tec_model = AutoModelForSeq2SeqLM.from_pretrained("tecuhtli/mori-tecnico-model", use_auth_token=HF_TOKEN)
498
+
499
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
500
+
501
+ # Presentación (solo técnico)
502
+ st.title("🤖 Mori — Asistente Técnico en Procesamiento de Datos")
503
+ st.caption("🙋🏽‍ Pregunta sobre: limpieza, features, evaluación, modelos, MLOps, BI, visualización, etc.")
504
+ st.caption("➡️ Ejemplos: Define X, ¿Para qué sirve Y?, Explícame Z, Diferencia entre A y B, ¿Cómo implemento ...?")
505
+ st.markdown("<br>", unsafe_allow_html=True)
506
+ st.caption("✏️ Escribe 'salir' para terminar.")
507
+
508
+ # Limpieza previa del textarea si corresponde
509
+ if ss.pop("_clear_entrada", False):
510
+ if "entrada" in ss:
511
+ del ss["entrada"]
512
+
513
+ # Respuesta flash de ciclo anterior
514
+ _flash = ss.pop("_flash_response", None)
515
+
516
+ # Formulario
517
+ with st.form("formulario_mori"):
518
+ user_question = st.text_area("📝 Escribe tu pregunta aquí", key="entrada", height=100)
519
+ submitted = st.form_submit_button("Responder")
520
+
521
+ if submitted:
522
+ if not user_question:
523
+ st.info("Mori: ¿Podrías repetir eso? No entendí bien 😅")
524
+ else:
525
+ response = answer_technical_only(user_question, device, GEN_PARAMS, tec_model, tec_tok)
526
+
527
+ # Historial
528
+ hora_actual = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
529
+ ss.historial.append(("Tú", user_question, hora_actual))
530
+ hora_actual = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
531
+ ss.historial.append(("Mori", response, hora_actual))
532
+
533
+ # Guardado persistente
534
+ saving_interaction(user_question, response, "procesamiento de datos", ss["user_id"])
535
+
536
+ # Flash y limpieza
537
+ ss["_flash_response"] = response
538
+ ss["_clear_entrada"] = True
539
+ st.rerun()
540
+
541
+ # Mostrar respuesta flash
542
+ if _flash:
543
+ st.success(_flash)
544
+
545
+ # Historial + descarga
546
+ if ss.historial:
547
+ st.markdown("---")
548
+
549
+ lineas = []
550
+ for msg in reversed(ss.historial):
551
+ if len(msg) == 3:
552
+ autor, texto, hora = msg
553
+ lineas.append(f"[{hora}] {autor}: {texto}")
554
+ else:
555
+ autor, texto = msg
556
+ lineas.append(f"{autor}: {texto}")
557
+ texto_chat = "\n\n".join(lineas)
558
+
559
+ st.download_button(
560
+ label="💾 Descargar conversación como .txt",
561
+ data=texto_chat,
562
+ file_name="conversacion_mori.txt",
563
+ mime="text/plain",
564
+ use_container_width=True
565
+ )
566
+
567
+ # Contenedor con estilo
568
+ st.markdown(
569
+ """
570
+ <div id="chat-container" style="
571
+ max-height: 400px;
572
+ overflow-y: auto;
573
+ padding: 10px;
574
+ border: 1px solid #333;
575
+ border-radius: 10px;
576
+ background: linear-gradient(180deg, #0e0e0e 0%, #1b1b1b 100%);
577
+ margin-top: 10px;
578
+ ">
579
+ """,
580
+ unsafe_allow_html=True
581
+ )
582
+
583
+ for msg in reversed(ss.historial):
584
+ if len(msg) == 3:
585
+ autor, texto, _ = msg
586
+ else:
587
+ autor, texto = msg
588
+
589
+ if autor == "Tú":
590
+ st.markdown(
591
+ f"""
592
+ <div style="
593
+ text-align: right;
594
+ background-color: #2d2d2d;
595
+ color: #e6e6e6;
596
+ padding: 10px 14px;
597
+ border-radius: 12px;
598
+ margin: 6px 0;
599
+ border: 1px solid #3a3a3a;
600
+ display: inline-block;
601
+ max-width: 80%;
602
+ float: right;
603
+ clear: both;
604
+ ">
605
+ 🧍‍♂️ <b>{autor}:</b> {texto}
606
+ </div>
607
+ """,
608
+ unsafe_allow_html=True
609
+ )
610
+ else:
611
+ st.markdown(
612
+ f"""
613
+ <div style="
614
+ text-align: left;
615
+ background-color: #162b1f;
616
+ color: #d9ead3;
617
+ padding: 10px 14px;
618
+ border-radius: 12px;
619
+ margin: 6px 0;
620
+ border: 1px solid #264d36;
621
+ display: inline-block;
622
+ max-width: 80%;
623
+ float: left;
624
+ clear: both;
625
+ ">
626
+ 🤖 <b>{autor}:</b> {texto}
627
+ </div>
628
+ """,
629
+ unsafe_allow_html=True
630
+ )
631
+
632
+ st.markdown("</div>", unsafe_allow_html=True)
633
+ #***************************************************************************
634
+ # FIN
635
+ #***************************************************************************