Spaces:
Sleeping
Sleeping
File size: 1,426 Bytes
33203e9 | 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 | import csv
import os
from datetime import datetime
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Usa volume persistente do Fly.io montado em /data
# Garante que feedback não seja perdido após deploy/restart
LOGS_DIR = os.getenv("LOGS_DIR", "/data/logs")
NEGATIVE_MEMORY_FILE = os.path.join(LOGS_DIR, "negative_memory.csv")
def garantir_pasta_logs():
os.makedirs(LOGS_DIR, exist_ok=True)
def inicializar_memoria_negativa():
garantir_pasta_logs()
if not os.path.exists(NEGATIVE_MEMORY_FILE):
with open(NEGATIVE_MEMORY_FILE, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow([
"timestamp",
"query",
"product_id",
"product_name",
"rating",
"motivo"
])
def salvar_memoria_negativa(query, product_id, product_name, rating, motivo="feedback_negativo"):
inicializar_memoria_negativa()
with open(NEGATIVE_MEMORY_FILE, "a", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow([
datetime.now().isoformat(),
query,
product_id,
product_name,
rating,
motivo
])
return {"status": "ok"}
def caminho_memoria_negativa():
return NEGATIVE_MEMORY_FILE
|