Spaces:
Sleeping
Sleeping
Update app/feedback.py
Browse files- app/feedback.py +140 -171
app/feedback.py
CHANGED
|
@@ -1,171 +1,140 @@
|
|
| 1 |
-
import csv
|
| 2 |
-
import os
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
from
|
| 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 |
-
def
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
"
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
if rating is not None and rating <= 2:
|
| 143 |
-
salvar_memoria_negativa(
|
| 144 |
-
query=query,
|
| 145 |
-
product_id=product_id,
|
| 146 |
-
product_name=product_name,
|
| 147 |
-
rating=rating,
|
| 148 |
-
motivo="rating_baixo",
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
if is_helpful is False:
|
| 152 |
-
salvar_memoria_negativa(
|
| 153 |
-
query=query,
|
| 154 |
-
product_id=product_id,
|
| 155 |
-
product_name=product_name,
|
| 156 |
-
rating=rating if rating is not None else "",
|
| 157 |
-
motivo="nao_foi_util",
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
response = {
|
| 161 |
-
"ok": True,
|
| 162 |
-
"saved_local": True,
|
| 163 |
-
"saved_google_sheets": saved_google_sheets,
|
| 164 |
-
}
|
| 165 |
-
if warning:
|
| 166 |
-
response["warning"] = warning
|
| 167 |
-
return response
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
def caminho_feedback():
|
| 171 |
-
return FEEDBACK_FILE
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
from datetime import datetime, timezone
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
from .google_oauth import GoogleAuthRequiredError, append_feedback_to_sheet, load_credentials
|
| 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",
|
| 26 |
+
"score_semantico",
|
| 27 |
+
"bonus_lexical",
|
| 28 |
+
"penalidade_feedback",
|
| 29 |
+
"user_message",
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _resolve_logs_dir():
|
| 34 |
+
preferred_dir = os.environ.get("LOGS_DIR", "").strip()
|
| 35 |
+
candidates = [Path(preferred_dir)] if preferred_dir else []
|
| 36 |
+
candidates.extend([DEFAULT_LOGS_DIR, FALLBACK_LOGS_DIR])
|
| 37 |
+
|
| 38 |
+
for directory in candidates:
|
| 39 |
+
try:
|
| 40 |
+
directory.mkdir(parents=True, exist_ok=True)
|
| 41 |
+
return directory
|
| 42 |
+
except OSError:
|
| 43 |
+
continue
|
| 44 |
+
|
| 45 |
+
raise OSError(
|
| 46 |
+
"Nao foi possivel criar um diretorio para armazenar os logs de feedback. "
|
| 47 |
+
"Defina LOGS_DIR para um caminho gravavel."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def caminho_feedback():
|
| 52 |
+
return str(_resolve_logs_dir() / "feedback.csv")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def inicializar_arquivo_feedback():
|
| 56 |
+
feedback_file = caminho_feedback()
|
| 57 |
+
|
| 58 |
+
if not os.path.exists(feedback_file):
|
| 59 |
+
with open(feedback_file, mode="w", newline="", encoding="utf-8") as f:
|
| 60 |
+
writer = csv.writer(f)
|
| 61 |
+
writer.writerow(FEEDBACK_HEADERS)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def google_sheets_habilitado():
|
| 65 |
+
try:
|
| 66 |
+
return load_credentials() is not None and bool(os.getenv("GOOGLE_SPREADSHEET_ID", "").strip())
|
| 67 |
+
except Exception:
|
| 68 |
+
return False
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _append_feedback_csv(row):
|
| 72 |
+
inicializar_arquivo_feedback()
|
| 73 |
+
feedback_file = caminho_feedback()
|
| 74 |
+
|
| 75 |
+
with open(feedback_file, mode="a", newline="", encoding="utf-8") as f:
|
| 76 |
+
writer = csv.writer(f)
|
| 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,
|
| 91 |
+
score_semantico=None,
|
| 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 |
+
"feedback": feedback or "",
|
| 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,
|
| 126 |
+
score_semantico=None,
|
| 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 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|