Spaces:
Sleeping
Sleeping
Update app/feedback.py
Browse files- app/feedback.py +77 -27
app/feedback.py
CHANGED
|
@@ -8,18 +8,19 @@ from .google_oauth import GoogleAuthRequiredError, append_feedback_to_sheet, loa
|
|
| 8 |
from .memory import salvar_memoria_negativa
|
| 9 |
|
| 10 |
|
| 11 |
-
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 12 |
DEFAULT_LOGS_DIR = Path(os.getenv("LOGS_DIR", "/data/logs"))
|
| 13 |
FALLBACK_LOGS_DIR = Path(tempfile.gettempdir()) / "tcc2_agent" / "logs"
|
| 14 |
FEEDBACK_HEADERS = [
|
| 15 |
"timestamp",
|
|
|
|
| 16 |
"query",
|
|
|
|
| 17 |
"product_id",
|
| 18 |
"product_name",
|
|
|
|
|
|
|
| 19 |
"rating",
|
| 20 |
"is_helpful",
|
| 21 |
-
"note",
|
| 22 |
-
"categoria_inferida",
|
| 23 |
"feedback",
|
| 24 |
"motivo",
|
| 25 |
"score_final",
|
|
@@ -27,6 +28,7 @@ FEEDBACK_HEADERS = [
|
|
| 27 |
"bonus_lexical",
|
| 28 |
"penalidade_feedback",
|
| 29 |
"user_message",
|
|
|
|
| 30 |
]
|
| 31 |
|
| 32 |
|
|
@@ -77,14 +79,43 @@ def _append_feedback_csv(row):
|
|
| 77 |
writer.writerow([row.get(header, "") for header in FEEDBACK_HEADERS])
|
| 78 |
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def _build_feedback_payload(
|
|
|
|
| 81 |
query,
|
|
|
|
| 82 |
product_id,
|
| 83 |
product_name,
|
|
|
|
|
|
|
| 84 |
rating=None,
|
| 85 |
is_helpful=None,
|
| 86 |
-
note=None,
|
| 87 |
-
categoria_inferida=None,
|
| 88 |
feedback=None,
|
| 89 |
motivo=None,
|
| 90 |
score_final=None,
|
|
@@ -92,34 +123,46 @@ def _build_feedback_payload(
|
|
| 92 |
bonus_lexical=None,
|
| 93 |
penalidade_feedback=None,
|
| 94 |
user_message=None,
|
|
|
|
| 95 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
return {
|
| 97 |
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
|
|
| 98 |
"query": query,
|
|
|
|
| 99 |
"product_id": product_id,
|
| 100 |
"product_name": product_name,
|
| 101 |
-
"rating": rating if rating is not None else "",
|
| 102 |
-
"is_helpful": is_helpful if is_helpful is not None else "",
|
| 103 |
-
"note": note or "",
|
| 104 |
"categoria_inferida": categoria_inferida or "",
|
| 105 |
-
"
|
|
|
|
|
|
|
|
|
|
| 106 |
"motivo": motivo or "",
|
| 107 |
"score_final": score_final if score_final is not None else "",
|
| 108 |
"score_semantico": score_semantico if score_semantico is not None else "",
|
| 109 |
"bonus_lexical": bonus_lexical if bonus_lexical is not None else "",
|
| 110 |
"penalidade_feedback": penalidade_feedback if penalidade_feedback is not None else "",
|
| 111 |
-
"user_message": user_message or "",
|
|
|
|
| 112 |
}
|
| 113 |
|
| 114 |
|
| 115 |
def salvar_feedback(
|
|
|
|
| 116 |
query,
|
|
|
|
| 117 |
product_id,
|
| 118 |
product_name,
|
|
|
|
|
|
|
| 119 |
rating=None,
|
| 120 |
is_helpful=None,
|
| 121 |
-
note=None,
|
| 122 |
-
categoria_inferida=None,
|
| 123 |
feedback=None,
|
| 124 |
motivo=None,
|
| 125 |
score_final=None,
|
|
@@ -127,15 +170,18 @@ def salvar_feedback(
|
|
| 127 |
bonus_lexical=None,
|
| 128 |
penalidade_feedback=None,
|
| 129 |
user_message=None,
|
|
|
|
| 130 |
):
|
| 131 |
row = _build_feedback_payload(
|
|
|
|
| 132 |
query=query,
|
|
|
|
| 133 |
product_id=product_id,
|
| 134 |
product_name=product_name,
|
|
|
|
|
|
|
| 135 |
rating=rating,
|
| 136 |
is_helpful=is_helpful,
|
| 137 |
-
note=note,
|
| 138 |
-
categoria_inferida=categoria_inferida,
|
| 139 |
feedback=feedback,
|
| 140 |
motivo=motivo,
|
| 141 |
score_final=score_final,
|
|
@@ -143,6 +189,7 @@ def salvar_feedback(
|
|
| 143 |
bonus_lexical=bonus_lexical,
|
| 144 |
penalidade_feedback=penalidade_feedback,
|
| 145 |
user_message=user_message,
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
_append_feedback_csv(row)
|
|
@@ -160,22 +207,26 @@ def salvar_feedback(
|
|
| 160 |
f"Erro de sincronizacao: {exc}"
|
| 161 |
)
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
rating=rating,
|
| 169 |
-
motivo="rating_baixo",
|
| 170 |
-
)
|
| 171 |
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
salvar_memoria_negativa(
|
| 174 |
query=query,
|
| 175 |
product_id=product_id,
|
| 176 |
product_name=product_name,
|
| 177 |
-
rating=
|
| 178 |
-
motivo="
|
|
|
|
|
|
|
| 179 |
)
|
| 180 |
|
| 181 |
response = {
|
|
@@ -185,5 +236,4 @@ def salvar_feedback(
|
|
| 185 |
}
|
| 186 |
if warning:
|
| 187 |
response["warning"] = warning
|
| 188 |
-
return response
|
| 189 |
-
|
|
|
|
| 8 |
from .memory import salvar_memoria_negativa
|
| 9 |
|
| 10 |
|
|
|
|
| 11 |
DEFAULT_LOGS_DIR = Path(os.getenv("LOGS_DIR", "/data/logs"))
|
| 12 |
FALLBACK_LOGS_DIR = Path(tempfile.gettempdir()) / "tcc2_agent" / "logs"
|
| 13 |
FEEDBACK_HEADERS = [
|
| 14 |
"timestamp",
|
| 15 |
+
"search_id",
|
| 16 |
"query",
|
| 17 |
+
"rank",
|
| 18 |
"product_id",
|
| 19 |
"product_name",
|
| 20 |
+
"categoria_inferida",
|
| 21 |
+
"categoria_produto",
|
| 22 |
"rating",
|
| 23 |
"is_helpful",
|
|
|
|
|
|
|
| 24 |
"feedback",
|
| 25 |
"motivo",
|
| 26 |
"score_final",
|
|
|
|
| 28 |
"bonus_lexical",
|
| 29 |
"penalidade_feedback",
|
| 30 |
"user_message",
|
| 31 |
+
"note",
|
| 32 |
]
|
| 33 |
|
| 34 |
|
|
|
|
| 79 |
writer.writerow([row.get(header, "") for header in FEEDBACK_HEADERS])
|
| 80 |
|
| 81 |
|
| 82 |
+
def _derive_feedback_fields(rating=None, is_helpful=None, feedback=None):
|
| 83 |
+
derived_feedback = feedback
|
| 84 |
+
derived_is_helpful = is_helpful
|
| 85 |
+
|
| 86 |
+
if derived_feedback is None:
|
| 87 |
+
if rating is not None and rating >= 4:
|
| 88 |
+
derived_feedback = "positivo"
|
| 89 |
+
elif rating is not None and rating <= 2:
|
| 90 |
+
derived_feedback = "negativo"
|
| 91 |
+
elif rating == 3:
|
| 92 |
+
derived_feedback = "neutro"
|
| 93 |
+
else:
|
| 94 |
+
derived_feedback = ""
|
| 95 |
+
|
| 96 |
+
if derived_is_helpful is None:
|
| 97 |
+
if rating is not None and rating >= 4:
|
| 98 |
+
derived_is_helpful = True
|
| 99 |
+
elif rating is not None and rating <= 2:
|
| 100 |
+
derived_is_helpful = False
|
| 101 |
+
elif rating == 3:
|
| 102 |
+
derived_is_helpful = ""
|
| 103 |
+
else:
|
| 104 |
+
derived_is_helpful = ""
|
| 105 |
+
|
| 106 |
+
return derived_feedback, derived_is_helpful
|
| 107 |
+
|
| 108 |
+
|
| 109 |
def _build_feedback_payload(
|
| 110 |
+
search_id,
|
| 111 |
query,
|
| 112 |
+
rank,
|
| 113 |
product_id,
|
| 114 |
product_name,
|
| 115 |
+
categoria_inferida=None,
|
| 116 |
+
categoria_produto=None,
|
| 117 |
rating=None,
|
| 118 |
is_helpful=None,
|
|
|
|
|
|
|
| 119 |
feedback=None,
|
| 120 |
motivo=None,
|
| 121 |
score_final=None,
|
|
|
|
| 123 |
bonus_lexical=None,
|
| 124 |
penalidade_feedback=None,
|
| 125 |
user_message=None,
|
| 126 |
+
note=None,
|
| 127 |
):
|
| 128 |
+
derived_feedback, derived_is_helpful = _derive_feedback_fields(
|
| 129 |
+
rating=rating,
|
| 130 |
+
is_helpful=is_helpful,
|
| 131 |
+
feedback=feedback,
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
return {
|
| 135 |
"timestamp": datetime.now(timezone.utc).isoformat(),
|
| 136 |
+
"search_id": search_id or "",
|
| 137 |
"query": query,
|
| 138 |
+
"rank": rank if rank is not None else "",
|
| 139 |
"product_id": product_id,
|
| 140 |
"product_name": product_name,
|
|
|
|
|
|
|
|
|
|
| 141 |
"categoria_inferida": categoria_inferida or "",
|
| 142 |
+
"categoria_produto": categoria_produto or "",
|
| 143 |
+
"rating": rating if rating is not None else "",
|
| 144 |
+
"is_helpful": derived_is_helpful,
|
| 145 |
+
"feedback": derived_feedback,
|
| 146 |
"motivo": motivo or "",
|
| 147 |
"score_final": score_final if score_final is not None else "",
|
| 148 |
"score_semantico": score_semantico if score_semantico is not None else "",
|
| 149 |
"bonus_lexical": bonus_lexical if bonus_lexical is not None else "",
|
| 150 |
"penalidade_feedback": penalidade_feedback if penalidade_feedback is not None else "",
|
| 151 |
+
"user_message": user_message or "", # ajuste aqui
|
| 152 |
+
"note": note or "",
|
| 153 |
}
|
| 154 |
|
| 155 |
|
| 156 |
def salvar_feedback(
|
| 157 |
+
search_id,
|
| 158 |
query,
|
| 159 |
+
rank,
|
| 160 |
product_id,
|
| 161 |
product_name,
|
| 162 |
+
categoria_inferida=None,
|
| 163 |
+
categoria_produto=None,
|
| 164 |
rating=None,
|
| 165 |
is_helpful=None,
|
|
|
|
|
|
|
| 166 |
feedback=None,
|
| 167 |
motivo=None,
|
| 168 |
score_final=None,
|
|
|
|
| 170 |
bonus_lexical=None,
|
| 171 |
penalidade_feedback=None,
|
| 172 |
user_message=None,
|
| 173 |
+
note=None,
|
| 174 |
):
|
| 175 |
row = _build_feedback_payload(
|
| 176 |
+
search_id=search_id,
|
| 177 |
query=query,
|
| 178 |
+
rank=rank,
|
| 179 |
product_id=product_id,
|
| 180 |
product_name=product_name,
|
| 181 |
+
categoria_inferida=categoria_inferida,
|
| 182 |
+
categoria_produto=categoria_produto,
|
| 183 |
rating=rating,
|
| 184 |
is_helpful=is_helpful,
|
|
|
|
|
|
|
| 185 |
feedback=feedback,
|
| 186 |
motivo=motivo,
|
| 187 |
score_final=score_final,
|
|
|
|
| 189 |
bonus_lexical=bonus_lexical,
|
| 190 |
penalidade_feedback=penalidade_feedback,
|
| 191 |
user_message=user_message,
|
| 192 |
+
note=note,
|
| 193 |
)
|
| 194 |
|
| 195 |
_append_feedback_csv(row)
|
|
|
|
| 207 |
f"Erro de sincronizacao: {exc}"
|
| 208 |
)
|
| 209 |
|
| 210 |
+
# 🔥 ajuste aqui (tratamento de string → int)
|
| 211 |
+
try:
|
| 212 |
+
rating_num = int(row.get("rating"))
|
| 213 |
+
except (TypeError, ValueError):
|
| 214 |
+
rating_num = None
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
+
is_helpful_value = row.get("is_helpful")
|
| 217 |
+
|
| 218 |
+
if (
|
| 219 |
+
(isinstance(rating_num, int) and rating_num <= 2)
|
| 220 |
+
or is_helpful_value is False
|
| 221 |
+
):
|
| 222 |
salvar_memoria_negativa(
|
| 223 |
query=query,
|
| 224 |
product_id=product_id,
|
| 225 |
product_name=product_name,
|
| 226 |
+
rating=rating_num,
|
| 227 |
+
motivo=row.get("motivo") or row.get("feedback") or "feedback_negativo",
|
| 228 |
+
search_id=row.get("search_id"),
|
| 229 |
+
rank=row.get("rank"),
|
| 230 |
)
|
| 231 |
|
| 232 |
response = {
|
|
|
|
| 236 |
}
|
| 237 |
if warning:
|
| 238 |
response["warning"] = warning
|
| 239 |
+
return response
|
|
|