Spaces:
Sleeping
Sleeping
Delete feedback.py
Browse files- feedback.py +0 -168
feedback.py
DELETED
|
@@ -1,168 +0,0 @@
|
|
| 1 |
-
import csv
|
| 2 |
-
import os
|
| 3 |
-
from datetime import datetime, timezone
|
| 4 |
-
|
| 5 |
-
from .google_oauth import GoogleAuthRequiredError, append_feedback_to_sheet, load_credentials
|
| 6 |
-
from .memory import salvar_memoria_negativa
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 10 |
-
LOGS_DIR = os.getenv("LOGS_DIR", "/data/logs")
|
| 11 |
-
FEEDBACK_FILE = os.path.join(LOGS_DIR, "feedback.csv")
|
| 12 |
-
FEEDBACK_HEADERS = [
|
| 13 |
-
"timestamp",
|
| 14 |
-
"query",
|
| 15 |
-
"product_id",
|
| 16 |
-
"product_name",
|
| 17 |
-
"rating",
|
| 18 |
-
"is_helpful",
|
| 19 |
-
"note",
|
| 20 |
-
"categoria_inferida",
|
| 21 |
-
"feedback",
|
| 22 |
-
"motivo",
|
| 23 |
-
"score_final",
|
| 24 |
-
"score_semantico",
|
| 25 |
-
"bonus_lexical",
|
| 26 |
-
"penalidade_feedback",
|
| 27 |
-
"user_message",
|
| 28 |
-
]
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def garantir_pasta_logs():
|
| 32 |
-
os.makedirs(LOGS_DIR, exist_ok=True)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def inicializar_arquivo_feedback():
|
| 36 |
-
garantir_pasta_logs()
|
| 37 |
-
|
| 38 |
-
if not os.path.exists(FEEDBACK_FILE):
|
| 39 |
-
with open(FEEDBACK_FILE, mode="w", newline="", encoding="utf-8") as f:
|
| 40 |
-
writer = csv.writer(f)
|
| 41 |
-
writer.writerow(FEEDBACK_HEADERS)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
def google_sheets_habilitado():
|
| 45 |
-
return load_credentials() is not None and bool(os.getenv("GOOGLE_SPREADSHEET_ID", "").strip())
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
def _append_feedback_csv(row):
|
| 49 |
-
inicializar_arquivo_feedback()
|
| 50 |
-
|
| 51 |
-
with open(FEEDBACK_FILE, mode="a", newline="", encoding="utf-8") as f:
|
| 52 |
-
writer = csv.writer(f)
|
| 53 |
-
writer.writerow([row.get(header, "") for header in FEEDBACK_HEADERS])
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def _build_feedback_payload(
|
| 57 |
-
query,
|
| 58 |
-
product_id,
|
| 59 |
-
product_name,
|
| 60 |
-
rating=None,
|
| 61 |
-
is_helpful=None,
|
| 62 |
-
note=None,
|
| 63 |
-
categoria_inferida=None,
|
| 64 |
-
feedback=None,
|
| 65 |
-
motivo=None,
|
| 66 |
-
score_final=None,
|
| 67 |
-
score_semantico=None,
|
| 68 |
-
bonus_lexical=None,
|
| 69 |
-
penalidade_feedback=None,
|
| 70 |
-
user_message=None,
|
| 71 |
-
):
|
| 72 |
-
return {
|
| 73 |
-
"timestamp": datetime.now(timezone.utc).isoformat(),
|
| 74 |
-
"query": query,
|
| 75 |
-
"product_id": product_id,
|
| 76 |
-
"product_name": product_name,
|
| 77 |
-
"rating": rating if rating is not None else "",
|
| 78 |
-
"is_helpful": is_helpful if is_helpful is not None else "",
|
| 79 |
-
"note": note or "",
|
| 80 |
-
"categoria_inferida": categoria_inferida or "",
|
| 81 |
-
"feedback": feedback or "",
|
| 82 |
-
"motivo": motivo or "",
|
| 83 |
-
"score_final": score_final if score_final is not None else "",
|
| 84 |
-
"score_semantico": score_semantico if score_semantico is not None else "",
|
| 85 |
-
"bonus_lexical": bonus_lexical if bonus_lexical is not None else "",
|
| 86 |
-
"penalidade_feedback": penalidade_feedback if penalidade_feedback is not None else "",
|
| 87 |
-
"user_message": user_message or "",
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
def salvar_feedback(
|
| 92 |
-
query,
|
| 93 |
-
product_id,
|
| 94 |
-
product_name,
|
| 95 |
-
rating=None,
|
| 96 |
-
is_helpful=None,
|
| 97 |
-
note=None,
|
| 98 |
-
categoria_inferida=None,
|
| 99 |
-
feedback=None,
|
| 100 |
-
motivo=None,
|
| 101 |
-
score_final=None,
|
| 102 |
-
score_semantico=None,
|
| 103 |
-
bonus_lexical=None,
|
| 104 |
-
penalidade_feedback=None,
|
| 105 |
-
user_message=None,
|
| 106 |
-
):
|
| 107 |
-
row = _build_feedback_payload(
|
| 108 |
-
query=query,
|
| 109 |
-
product_id=product_id,
|
| 110 |
-
product_name=product_name,
|
| 111 |
-
rating=rating,
|
| 112 |
-
is_helpful=is_helpful,
|
| 113 |
-
note=note,
|
| 114 |
-
categoria_inferida=categoria_inferida,
|
| 115 |
-
feedback=feedback,
|
| 116 |
-
motivo=motivo,
|
| 117 |
-
score_final=score_final,
|
| 118 |
-
score_semantico=score_semantico,
|
| 119 |
-
bonus_lexical=bonus_lexical,
|
| 120 |
-
penalidade_feedback=penalidade_feedback,
|
| 121 |
-
user_message=user_message,
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
_append_feedback_csv(row)
|
| 125 |
-
saved_google_sheets = False
|
| 126 |
-
warning = None
|
| 127 |
-
|
| 128 |
-
try:
|
| 129 |
-
append_feedback_to_sheet(row)
|
| 130 |
-
saved_google_sheets = True
|
| 131 |
-
except GoogleAuthRequiredError:
|
| 132 |
-
warning = "Feedback salvo localmente, mas nao enviado ao Google Sheets. Acesse /auth/google para autorizar."
|
| 133 |
-
except Exception as exc:
|
| 134 |
-
warning = (
|
| 135 |
-
"Feedback salvo localmente, mas nao enviado ao Google Sheets. "
|
| 136 |
-
f"Erro de sincronizacao: {exc}"
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
if rating is not None and rating <= 2:
|
| 140 |
-
salvar_memoria_negativa(
|
| 141 |
-
query=query,
|
| 142 |
-
product_id=product_id,
|
| 143 |
-
product_name=product_name,
|
| 144 |
-
rating=rating,
|
| 145 |
-
motivo="rating_baixo",
|
| 146 |
-
)
|
| 147 |
-
|
| 148 |
-
if is_helpful is False:
|
| 149 |
-
salvar_memoria_negativa(
|
| 150 |
-
query=query,
|
| 151 |
-
product_id=product_id,
|
| 152 |
-
product_name=product_name,
|
| 153 |
-
rating=rating if rating is not None else "",
|
| 154 |
-
motivo="nao_foi_util",
|
| 155 |
-
)
|
| 156 |
-
|
| 157 |
-
response = {
|
| 158 |
-
"ok": True,
|
| 159 |
-
"saved_local": True,
|
| 160 |
-
"saved_google_sheets": saved_google_sheets,
|
| 161 |
-
}
|
| 162 |
-
if warning:
|
| 163 |
-
response["warning"] = warning
|
| 164 |
-
return response
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
def caminho_feedback():
|
| 168 |
-
return FEEDBACK_FILE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|