Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -22,12 +22,32 @@ REMOTE_DESTINATION_FOLDER = "Conversations" # Carpeta en MEGA donde se guardar
|
|
22 |
def get_mega_client():
|
23 |
"""Obtiene un cliente autenticado de MEGA"""
|
24 |
try:
|
|
|
|
|
|
|
|
|
|
|
25 |
mega = Mega()
|
|
|
|
|
26 |
m = mega.login(MEGA_EMAIL, MEGA_PASSWORD)
|
|
|
27 |
return m
|
|
|
28 |
except Exception as e:
|
29 |
-
print(f"Error al conectar con MEGA: {e}")
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def ensure_conversations_folder(mega_client):
|
33 |
"""Asegura que existe la carpeta de conversaciones en MEGA"""
|
@@ -58,12 +78,16 @@ def persist_data(session_data):
|
|
58 |
# Conectarse a MEGA
|
59 |
mega_client = get_mega_client()
|
60 |
if not mega_client:
|
|
|
|
|
|
|
61 |
return
|
62 |
|
63 |
# Asegurar que existe la carpeta de conversaciones
|
64 |
conversations_folder_id = ensure_conversations_folder(mega_client)
|
65 |
if not conversations_folder_id:
|
66 |
print("No se pudo crear/acceder a la carpeta de conversaciones")
|
|
|
67 |
return
|
68 |
|
69 |
# Formatear el log de la conversaci贸n
|
@@ -98,6 +122,36 @@ def persist_data(session_data):
|
|
98 |
|
99 |
except Exception as e:
|
100 |
print(f"Error durante la persistencia de datos en MEGA: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
def respond(message, history: list[tuple[str, str]]):
|
103 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
|
|
22 |
def get_mega_client():
|
23 |
"""Obtiene un cliente autenticado de MEGA"""
|
24 |
try:
|
25 |
+
if not MEGA_EMAIL or not MEGA_PASSWORD:
|
26 |
+
print("Error: Credenciales de MEGA no configuradas")
|
27 |
+
return None
|
28 |
+
|
29 |
+
print(f"Intentando conectar a MEGA con email: {MEGA_EMAIL[:5]}...")
|
30 |
mega = Mega()
|
31 |
+
|
32 |
+
# Intentar login con m谩s informaci贸n de debug
|
33 |
m = mega.login(MEGA_EMAIL, MEGA_PASSWORD)
|
34 |
+
print("Conexi贸n exitosa a MEGA")
|
35 |
return m
|
36 |
+
|
37 |
except Exception as e:
|
38 |
+
print(f"Error al conectar con MEGA: {str(e)}")
|
39 |
+
print(f"Tipo de error: {type(e).__name__}")
|
40 |
+
|
41 |
+
# Intentar con un enfoque alternativo
|
42 |
+
try:
|
43 |
+
print("Intentando conexi贸n alternativa...")
|
44 |
+
mega = Mega({'verbose': False})
|
45 |
+
m = mega.login(MEGA_EMAIL, MEGA_PASSWORD)
|
46 |
+
print("Conexi贸n alternativa exitosa")
|
47 |
+
return m
|
48 |
+
except Exception as e2:
|
49 |
+
print(f"Error en conexi贸n alternativa: {str(e2)}")
|
50 |
+
return None
|
51 |
|
52 |
def ensure_conversations_folder(mega_client):
|
53 |
"""Asegura que existe la carpeta de conversaciones en MEGA"""
|
|
|
78 |
# Conectarse a MEGA
|
79 |
mega_client = get_mega_client()
|
80 |
if not mega_client:
|
81 |
+
print("No se pudo establecer conexi贸n con MEGA. Guardando localmente como respaldo.")
|
82 |
+
# Guardar localmente como respaldo
|
83 |
+
save_locally_as_backup(session_data)
|
84 |
return
|
85 |
|
86 |
# Asegurar que existe la carpeta de conversaciones
|
87 |
conversations_folder_id = ensure_conversations_folder(mega_client)
|
88 |
if not conversations_folder_id:
|
89 |
print("No se pudo crear/acceder a la carpeta de conversaciones")
|
90 |
+
save_locally_as_backup(session_data)
|
91 |
return
|
92 |
|
93 |
# Formatear el log de la conversaci贸n
|
|
|
122 |
|
123 |
except Exception as e:
|
124 |
print(f"Error durante la persistencia de datos en MEGA: {e}")
|
125 |
+
save_locally_as_backup(session_data)
|
126 |
+
|
127 |
+
def save_locally_as_backup(session_data):
|
128 |
+
"""Guarda las conversaciones localmente como respaldo cuando MEGA falla"""
|
129 |
+
try:
|
130 |
+
# Formatear el log de la conversaci贸n
|
131 |
+
formatted_log = ""
|
132 |
+
for user_msg, assistant_msg in session_data:
|
133 |
+
formatted_log += f"User: {user_msg}\n"
|
134 |
+
if assistant_msg:
|
135 |
+
formatted_log += f"Assistant: {assistant_msg}\n"
|
136 |
+
formatted_log += "-----\n"
|
137 |
+
|
138 |
+
# Generar nombre 煤nico para el archivo
|
139 |
+
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
140 |
+
log_name = f"backup_session_{timestamp}.log"
|
141 |
+
|
142 |
+
# Crear directorio de respaldo si no existe
|
143 |
+
backup_dir = "/tmp/chat_backups"
|
144 |
+
os.makedirs(backup_dir, exist_ok=True)
|
145 |
+
|
146 |
+
# Guardar archivo local
|
147 |
+
local_path = os.path.join(backup_dir, log_name)
|
148 |
+
with open(local_path, 'w', encoding='utf-8') as f:
|
149 |
+
f.write(formatted_log)
|
150 |
+
|
151 |
+
print(f"Conversaci贸n guardada localmente como respaldo: {local_path}")
|
152 |
+
|
153 |
+
except Exception as e:
|
154 |
+
print(f"Error al guardar respaldo local: {e}")
|
155 |
|
156 |
def respond(message, history: list[tuple[str, str]]):
|
157 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|