Spaces:
Sleeping
Sleeping
| import csv | |
| import os | |
| from datetime import datetime | |
| from .memory import salvar_memoria_negativa | |
| 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") | |
| FEEDBACK_FILE = os.path.join(LOGS_DIR, "feedback.csv") | |
| def garantir_pasta_logs(): | |
| os.makedirs(LOGS_DIR, exist_ok=True) | |
| def inicializar_arquivo_feedback(): | |
| garantir_pasta_logs() | |
| if not os.path.exists(FEEDBACK_FILE): | |
| with open(FEEDBACK_FILE, mode="w", newline="", encoding="utf-8") as f: | |
| writer = csv.writer(f) | |
| writer.writerow([ | |
| "timestamp", | |
| "query", | |
| "product_id", | |
| "product_name", | |
| "rating", | |
| "is_helpful" | |
| ]) | |
| def salvar_feedback(query, product_id, product_name, rating=None, is_helpful=None): | |
| inicializar_arquivo_feedback() | |
| with open(FEEDBACK_FILE, mode="a", newline="", encoding="utf-8") as f: | |
| writer = csv.writer(f) | |
| writer.writerow([ | |
| datetime.now().isoformat(), | |
| query, | |
| product_id, | |
| product_name, | |
| rating if rating is not None else "", | |
| is_helpful if is_helpful is not None else "" | |
| ]) | |
| # Regra simples para criar mem贸ria negativa | |
| if rating is not None and rating <= 2: | |
| salvar_memoria_negativa( | |
| query=query, | |
| product_id=product_id, | |
| product_name=product_name, | |
| rating=rating, | |
| motivo="rating_baixo" | |
| ) | |
| if is_helpful is False: | |
| salvar_memoria_negativa( | |
| query=query, | |
| product_id=product_id, | |
| product_name=product_name, | |
| rating=rating if rating is not None else "", | |
| motivo="nao_foi_util" | |
| ) | |
| return { | |
| "status": "ok", | |
| "message": "Feedback salvo com sucesso." | |
| } | |
| def caminho_feedback(): | |
| return FEEDBACK_FILE | |