salomonsky commited on
Commit
c0f5a6c
verified
1 Parent(s): b179e87

Upload chat_log.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_log.py +44 -0
chat_log.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ from datetime import datetime
3
+ import os
4
+
5
+ class ChatLogger:
6
+ def __init__(self, csv_file='chats.csv'):
7
+ self.csv_file = csv_file
8
+ self.current_chat_id = None
9
+ self.check_csv_file()
10
+
11
+ def check_csv_file(self):
12
+ """Crear archivo CSV si no existe"""
13
+ if not os.path.exists(self.csv_file):
14
+ with open(self.csv_file, 'w', newline='', encoding='utf-8') as f:
15
+ writer = csv.writer(f)
16
+ writer.writerow(['ChatID', 'Timestamp', 'Modo', 'Modelo_IA', 'Rol', 'Mensaje'])
17
+
18
+ def start_new_chat(self, mode, ai_model):
19
+ """Iniciar nueva conversaci贸n"""
20
+ self.current_chat_id = datetime.now().strftime("%Y%m%d_%H%M%S")
21
+ return self.current_chat_id
22
+
23
+ def log_message(self, message, is_user=False, mode=None, ai_model=None):
24
+ """Registrar mensaje en CSV"""
25
+ if not self.current_chat_id:
26
+ self.current_chat_id = self.start_new_chat(mode, ai_model)
27
+
28
+ try:
29
+ with open(self.csv_file, 'a', newline='', encoding='utf-8') as f:
30
+ writer = csv.writer(f)
31
+ writer.writerow([
32
+ self.current_chat_id,
33
+ datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
34
+ mode,
35
+ ai_model,
36
+ 'Usuario' if is_user else 'Asistente',
37
+ message.replace('\n', ' ')
38
+ ])
39
+ except Exception as e:
40
+ print(f"Error guardando mensaje en log: {e}")
41
+
42
+ def end_chat(self):
43
+ """Finalizar chat actual"""
44
+ self.current_chat_id = None