Spaces:
Sleeping
Sleeping
Update app/main.py
Browse files- app/main.py +250 -238
app/main.py
CHANGED
|
@@ -1,238 +1,250 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import threading
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
from typing import Optional
|
| 5 |
-
|
| 6 |
-
from fastapi import FastAPI, Query, Response
|
| 7 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
-
from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
|
| 9 |
-
from pydantic import BaseModel
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
from .
|
| 14 |
-
from .
|
| 15 |
-
from .
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
app
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 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 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
"
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
@app.post("/
|
| 199 |
-
def
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
{
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import threading
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from typing import Optional
|
| 5 |
+
|
| 6 |
+
from fastapi import FastAPI, Query, Response
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
|
| 9 |
+
from pydantic import BaseModel
|
| 10 |
+
from app.google_oauth import build_flow, save_credentials
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
from .agent import ShoppingAgent
|
| 14 |
+
from .feedback import caminho_feedback, google_sheets_habilitado, salvar_feedback
|
| 15 |
+
from .google_oauth import build_flow, get_authorization_url, load_credentials, save_credentials
|
| 16 |
+
from .logger import salvar_log_busca
|
| 17 |
+
from .memory import caminho_memoria_negativa
|
| 18 |
+
|
| 19 |
+
EMBEDDING_PROVIDER = os.getenv("EMBEDDING_PROVIDER", "transformers").strip().lower()
|
| 20 |
+
HF_MODEL_REPO = os.getenv("HF_MODEL_REPO", "Ana2012/bertimbau-buscador").strip()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _env_flag(name, default="true"):
|
| 24 |
+
return os.getenv(name, default).strip().lower() in {"1", "true", "yes", "on"}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
PRELOAD_AGENT = _env_flag("PRELOAD_AGENT", "true")
|
| 28 |
+
LOGS_DIR = os.getenv("LOGS_DIR", "/data/logs")
|
| 29 |
+
DATA_DIR = "/data"
|
| 30 |
+
|
| 31 |
+
app = FastAPI(title="TCC2 Agent API")
|
| 32 |
+
|
| 33 |
+
app.add_middleware(
|
| 34 |
+
CORSMiddleware,
|
| 35 |
+
allow_origins=["*"],
|
| 36 |
+
allow_credentials=False,
|
| 37 |
+
allow_methods=["*"],
|
| 38 |
+
allow_headers=["*"],
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
agent = None
|
| 42 |
+
agent_lock = threading.Lock()
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def get_agent():
|
| 46 |
+
global agent
|
| 47 |
+
if agent is None:
|
| 48 |
+
with agent_lock:
|
| 49 |
+
if agent is None:
|
| 50 |
+
agent = ShoppingAgent()
|
| 51 |
+
return agent
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@app.on_event("startup")
|
| 55 |
+
def preload_agent():
|
| 56 |
+
if PRELOAD_AGENT:
|
| 57 |
+
get_agent()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class ChatRequest(BaseModel):
|
| 61 |
+
query: Optional[str] = None
|
| 62 |
+
message: Optional[str] = None
|
| 63 |
+
top_k: int = 5
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class FeedbackRequest(BaseModel):
|
| 67 |
+
query: str
|
| 68 |
+
product_id: str
|
| 69 |
+
product_name: str
|
| 70 |
+
rating: Optional[int] = None
|
| 71 |
+
is_helpful: Optional[bool] = None
|
| 72 |
+
note: Optional[str] = None
|
| 73 |
+
categoria_inferida: Optional[str] = None
|
| 74 |
+
feedback: Optional[str] = None
|
| 75 |
+
motivo: Optional[str] = None
|
| 76 |
+
score_final: Optional[float] = None
|
| 77 |
+
score_semantico: Optional[float] = None
|
| 78 |
+
bonus_lexical: Optional[float] = None
|
| 79 |
+
penalidade_feedback: Optional[float] = None
|
| 80 |
+
user_message: Optional[str] = None
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
@app.get("/health")
|
| 84 |
+
def health():
|
| 85 |
+
runtime = get_agent().runtime_info() if agent is not None else None
|
| 86 |
+
return {
|
| 87 |
+
"status": "ok",
|
| 88 |
+
"agent_ready": agent is not None,
|
| 89 |
+
"embedding_provider": EMBEDDING_PROVIDER,
|
| 90 |
+
"model_repo": HF_MODEL_REPO,
|
| 91 |
+
"preload_agent": PRELOAD_AGENT,
|
| 92 |
+
"runtime": runtime,
|
| 93 |
+
"feedback_storage": "google_sheets" if google_sheets_habilitado() else "csv",
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
@app.get("/", include_in_schema=False)
|
| 98 |
+
def root():
|
| 99 |
+
return RedirectResponse(url="/docs")
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
@app.get("/favicon.ico", include_in_schema=False)
|
| 103 |
+
def favicon():
|
| 104 |
+
return Response(status_code=204)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
@app.get("/auth/google")
|
| 108 |
+
def auth_google():
|
| 109 |
+
authorization_url, _state = get_authorization_url()
|
| 110 |
+
return RedirectResponse(url=authorization_url)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
@app.get("/oauth2callback")
|
| 114 |
+
def oauth2callback(request: Request):
|
| 115 |
+
flow = build_flow()
|
| 116 |
+
|
| 117 |
+
code = request.query_params.get("code")
|
| 118 |
+
|
| 119 |
+
if not code:
|
| 120 |
+
return HTMLResponse("Erro: código OAuth não recebido.")
|
| 121 |
+
|
| 122 |
+
flow.fetch_token(code=code)
|
| 123 |
+
|
| 124 |
+
credentials = flow.credentials
|
| 125 |
+
save_credentials(credentials)
|
| 126 |
+
|
| 127 |
+
return HTMLResponse("""
|
| 128 |
+
<h2>✅ Google Sheets autorizado com sucesso!</h2>
|
| 129 |
+
<p>Agora o backend já pode salvar feedbacks na planilha.</p>
|
| 130 |
+
""")
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
@app.get("/auth/status")
|
| 134 |
+
def auth_status():
|
| 135 |
+
credentials = load_credentials()
|
| 136 |
+
connected = credentials is not None and credentials.valid
|
| 137 |
+
return {
|
| 138 |
+
"google_sheets_connected": connected,
|
| 139 |
+
"message": (
|
| 140 |
+
"Google Sheets autorizado e pronto para uso."
|
| 141 |
+
if connected
|
| 142 |
+
else "Google Sheets ainda nao autorizado. Acesse /auth/google para conectar."
|
| 143 |
+
),
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
@app.get("/debug/files")
|
| 148 |
+
def debug_files():
|
| 149 |
+
data_path = Path(DATA_DIR)
|
| 150 |
+
logs_path = Path(LOGS_DIR)
|
| 151 |
+
feedback_path = Path(caminho_feedback())
|
| 152 |
+
memory_path = Path(caminho_memoria_negativa())
|
| 153 |
+
|
| 154 |
+
return {
|
| 155 |
+
"data_exists": data_path.exists(),
|
| 156 |
+
"logs_exists": logs_path.exists(),
|
| 157 |
+
"feedback_exists": feedback_path.exists(),
|
| 158 |
+
"negative_memory_exists": memory_path.exists(),
|
| 159 |
+
"data_files": sorted(p.name for p in data_path.iterdir()) if data_path.exists() else [],
|
| 160 |
+
"logs_files": sorted(p.name for p in logs_path.iterdir()) if logs_path.exists() else [],
|
| 161 |
+
"feedback_file": str(feedback_path),
|
| 162 |
+
"negative_memory_file": str(memory_path),
|
| 163 |
+
"feedback_storage": "google_sheets" if google_sheets_habilitado() else "csv",
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
@app.get("/debug/feedback")
|
| 168 |
+
def debug_feedback():
|
| 169 |
+
feedback_path = Path(caminho_feedback())
|
| 170 |
+
if not feedback_path.exists():
|
| 171 |
+
return {"error": "arquivo nao existe"}
|
| 172 |
+
|
| 173 |
+
return {"conteudo": feedback_path.read_text(encoding="utf-8")}
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
@app.get("/download/feedback")
|
| 177 |
+
def download_feedback():
|
| 178 |
+
feedback_path = caminho_feedback()
|
| 179 |
+
if not os.path.exists(feedback_path):
|
| 180 |
+
return {"error": "arquivo nao existe"}
|
| 181 |
+
|
| 182 |
+
return FileResponse(feedback_path, filename="feedback.csv")
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
@app.get("/debug/memory")
|
| 186 |
+
def debug_memory():
|
| 187 |
+
memory_path = Path(caminho_memoria_negativa())
|
| 188 |
+
if not memory_path.exists():
|
| 189 |
+
return {"status": "missing", "file": str(memory_path)}
|
| 190 |
+
|
| 191 |
+
return {
|
| 192 |
+
"status": "ok",
|
| 193 |
+
"file": str(memory_path),
|
| 194 |
+
"content": memory_path.read_text(encoding="utf-8"),
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
@app.post("/chat")
|
| 199 |
+
def chat(request: ChatRequest):
|
| 200 |
+
texto = request.query or request.message
|
| 201 |
+
|
| 202 |
+
if not texto:
|
| 203 |
+
return {"error": "query ou message deve ser informado"}
|
| 204 |
+
|
| 205 |
+
resultado = get_agent().responder(texto, top_k=request.top_k)
|
| 206 |
+
salvar_log_busca(resultado)
|
| 207 |
+
return resultado
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
@app.post("/feedback")
|
| 211 |
+
def feedback(request: FeedbackRequest):
|
| 212 |
+
feedback_file = caminho_feedback()
|
| 213 |
+
print(
|
| 214 |
+
"Salvando feedback:",
|
| 215 |
+
{
|
| 216 |
+
"query": request.query,
|
| 217 |
+
"product_id": request.product_id,
|
| 218 |
+
"feedback_file": feedback_file,
|
| 219 |
+
"logs_dir_exists": os.path.exists(LOGS_DIR),
|
| 220 |
+
"google_sheets_enabled": google_sheets_habilitado(),
|
| 221 |
+
},
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
try:
|
| 225 |
+
return salvar_feedback(
|
| 226 |
+
query=request.query,
|
| 227 |
+
product_id=request.product_id,
|
| 228 |
+
product_name=request.product_name,
|
| 229 |
+
rating=request.rating,
|
| 230 |
+
is_helpful=request.is_helpful,
|
| 231 |
+
note=request.note,
|
| 232 |
+
categoria_inferida=request.categoria_inferida,
|
| 233 |
+
feedback=request.feedback,
|
| 234 |
+
motivo=request.motivo,
|
| 235 |
+
score_final=request.score_final,
|
| 236 |
+
score_semantico=request.score_semantico,
|
| 237 |
+
bonus_lexical=request.bonus_lexical,
|
| 238 |
+
penalidade_feedback=request.penalidade_feedback,
|
| 239 |
+
user_message=request.user_message,
|
| 240 |
+
)
|
| 241 |
+
except Exception as exc:
|
| 242 |
+
return {
|
| 243 |
+
"ok": False,
|
| 244 |
+
"saved_local": False,
|
| 245 |
+
"saved_google_sheets": False,
|
| 246 |
+
"detail": str(exc),
|
| 247 |
+
"feedback_file": feedback_file,
|
| 248 |
+
"logs_dir_exists": os.path.exists(LOGS_DIR),
|
| 249 |
+
"google_sheets_enabled": google_sheets_habilitado(),
|
| 250 |
+
}
|