final_project / app /memory.py
Ana2012
Deploy backend FastAPI para HF Spaces
614aa6b
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