Spaces:
Sleeping
Sleeping
Update app/memory.py
Browse files- app/memory.py +78 -47
app/memory.py
CHANGED
|
@@ -1,51 +1,82 @@
|
|
| 1 |
-
import csv
|
| 2 |
-
import os
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def garantir_pasta_logs():
|
| 14 |
-
os.makedirs(LOGS_DIR, exist_ok=True)
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def inicializar_memoria_negativa():
|
| 18 |
-
garantir_pasta_logs()
|
| 19 |
-
|
| 20 |
-
if not os.path.exists(NEGATIVE_MEMORY_FILE):
|
| 21 |
-
with open(NEGATIVE_MEMORY_FILE, "w", newline="", encoding="utf-8") as f:
|
| 22 |
-
writer = csv.writer(f)
|
| 23 |
-
writer.writerow([
|
| 24 |
-
"timestamp",
|
| 25 |
-
"query",
|
| 26 |
-
"product_id",
|
| 27 |
-
"product_name",
|
| 28 |
-
"rating",
|
| 29 |
-
"motivo"
|
| 30 |
-
])
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def salvar_memoria_negativa(query, product_id, product_name, rating, motivo="feedback_negativo"):
|
| 34 |
-
inicializar_memoria_negativa()
|
| 35 |
-
|
| 36 |
-
with open(NEGATIVE_MEMORY_FILE, "a", newline="", encoding="utf-8") as f:
|
| 37 |
-
writer = csv.writer(f)
|
| 38 |
-
writer.writerow([
|
| 39 |
-
datetime.now().isoformat(),
|
| 40 |
-
query,
|
| 41 |
-
product_id,
|
| 42 |
-
product_name,
|
| 43 |
-
rating,
|
| 44 |
-
motivo
|
| 45 |
-
])
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
def caminho_memoria_negativa():
|
| 51 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
from datetime import datetime, timezone
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
DEFAULT_LOGS_DIR = Path(os.getenv("LOGS_DIR", "/data/logs"))
|
| 9 |
+
FALLBACK_LOGS_DIR = Path(tempfile.gettempdir()) / "tcc2_agent" / "logs"
|
| 10 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def _resolve_logs_dir():
|
| 13 |
+
preferred_dir = os.environ.get("LOGS_DIR", "").strip()
|
| 14 |
+
candidates = [Path(preferred_dir)] if preferred_dir else []
|
| 15 |
+
candidates.extend([DEFAULT_LOGS_DIR, FALLBACK_LOGS_DIR])
|
| 16 |
+
|
| 17 |
+
for directory in candidates:
|
| 18 |
+
try:
|
| 19 |
+
directory.mkdir(parents=True, exist_ok=True)
|
| 20 |
+
return directory
|
| 21 |
+
except OSError:
|
| 22 |
+
continue
|
| 23 |
+
|
| 24 |
+
raise OSError(
|
| 25 |
+
"Nao foi possivel criar um diretorio para armazenar a memoria negativa. "
|
| 26 |
+
"Defina LOGS_DIR para um caminho gravavel."
|
| 27 |
+
)
|
| 28 |
|
| 29 |
|
| 30 |
def caminho_memoria_negativa():
|
| 31 |
+
return str(_resolve_logs_dir() / "negative_memory.csv")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def inicializar_memoria_negativa():
|
| 35 |
+
memory_file = caminho_memoria_negativa()
|
| 36 |
+
|
| 37 |
+
if not os.path.exists(memory_file):
|
| 38 |
+
with open(memory_file, "w", newline="", encoding="utf-8") as f:
|
| 39 |
+
writer = csv.writer(f)
|
| 40 |
+
writer.writerow([
|
| 41 |
+
"timestamp",
|
| 42 |
+
"search_id",
|
| 43 |
+
"rank",
|
| 44 |
+
"query",
|
| 45 |
+
"product_id",
|
| 46 |
+
"product_name",
|
| 47 |
+
"rating",
|
| 48 |
+
"motivo",
|
| 49 |
+
])
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def salvar_memoria_negativa(
|
| 53 |
+
query,
|
| 54 |
+
product_id,
|
| 55 |
+
product_name,
|
| 56 |
+
rating,
|
| 57 |
+
motivo="feedback_negativo",
|
| 58 |
+
search_id=None,
|
| 59 |
+
rank=None,
|
| 60 |
+
):
|
| 61 |
+
inicializar_memoria_negativa()
|
| 62 |
+
memory_file = caminho_memoria_negativa()
|
| 63 |
+
|
| 64 |
+
row = [
|
| 65 |
+
datetime.now(timezone.utc).isoformat(), # padrão UTC
|
| 66 |
+
search_id or "",
|
| 67 |
+
rank if rank is not None else "",
|
| 68 |
+
query or "",
|
| 69 |
+
product_id or "",
|
| 70 |
+
product_name or "",
|
| 71 |
+
rating if rating is not None else "",
|
| 72 |
+
motivo or "feedback_negativo",
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
try:
|
| 76 |
+
with open(memory_file, "a", newline="", encoding="utf-8") as f:
|
| 77 |
+
writer = csv.writer(f)
|
| 78 |
+
writer.writerow(row)
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print("Erro ao salvar memoria negativa:", e)
|
| 81 |
+
|
| 82 |
+
return {"status": "ok"}
|