Spaces:
Sleeping
Sleeping
Update app/main.py
Browse files- app/main.py +30 -30
app/main.py
CHANGED
|
@@ -3,12 +3,10 @@ import threading
|
|
| 3 |
from pathlib import Path
|
| 4 |
from typing import Optional
|
| 5 |
|
| 6 |
-
from fastapi import FastAPI, Request
|
| 7 |
from fastapi import FastAPI, Query, Response
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
|
| 10 |
from pydantic import BaseModel
|
| 11 |
-
from app.google_oauth import build_flow, save_credentials, OAUTH_TEMP
|
| 12 |
|
| 13 |
from .agent import ShoppingAgent
|
| 14 |
from .feedback import caminho_feedback, google_sheets_habilitado, salvar_feedback
|
|
@@ -64,13 +62,16 @@ class ChatRequest(BaseModel):
|
|
| 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
|
|
@@ -104,45 +105,41 @@ def favicon():
|
|
| 104 |
return Response(status_code=204)
|
| 105 |
|
| 106 |
|
| 107 |
-
oauth_state = {}
|
| 108 |
-
|
| 109 |
@app.get("/auth/google")
|
| 110 |
def auth_google():
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
|
| 118 |
@app.get("/oauth2callback")
|
| 119 |
-
def oauth2callback(
|
| 120 |
try:
|
| 121 |
-
code = request.query_params.get("code")
|
| 122 |
-
|
| 123 |
-
if not code:
|
| 124 |
-
return HTMLResponse("Erro: código OAuth não recebido.", status_code=400)
|
| 125 |
-
|
| 126 |
flow = build_flow()
|
| 127 |
flow.fetch_token(code=code)
|
| 128 |
-
|
| 129 |
save_credentials(flow.credentials)
|
| 130 |
|
| 131 |
-
return HTMLResponse("""
|
| 132 |
-
<h2>✅ Google Sheets autorizado com sucesso!</h2>
|
| 133 |
-
<p>Agora o backend pode salvar feedbacks na planilha.</p>
|
| 134 |
-
""")
|
| 135 |
-
|
| 136 |
-
except Exception as e:
|
| 137 |
return HTMLResponse(
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
| 140 |
)
|
| 141 |
-
|
| 142 |
-
except Exception as e:
|
| 143 |
return HTMLResponse(
|
| 144 |
-
f"
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
|
|
@@ -239,9 +236,12 @@ def feedback(request: FeedbackRequest):
|
|
| 239 |
|
| 240 |
try:
|
| 241 |
return salvar_feedback(
|
|
|
|
| 242 |
query=request.query,
|
|
|
|
| 243 |
product_id=request.product_id,
|
| 244 |
product_name=request.product_name,
|
|
|
|
| 245 |
rating=request.rating,
|
| 246 |
is_helpful=request.is_helpful,
|
| 247 |
note=request.note,
|
|
@@ -263,4 +263,4 @@ def feedback(request: FeedbackRequest):
|
|
| 263 |
"feedback_file": feedback_file,
|
| 264 |
"logs_dir_exists": os.path.exists(LOGS_DIR),
|
| 265 |
"google_sheets_enabled": google_sheets_habilitado(),
|
| 266 |
-
}
|
|
|
|
| 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 |
from .agent import ShoppingAgent
|
| 12 |
from .feedback import caminho_feedback, google_sheets_habilitado, salvar_feedback
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
class FeedbackRequest(BaseModel):
|
| 65 |
+
search_id: str
|
| 66 |
query: str
|
| 67 |
+
rank: int
|
| 68 |
product_id: str
|
| 69 |
product_name: str
|
| 70 |
+
categoria_produto: Optional[str] = None
|
| 71 |
+
categoria_inferida: Optional[str] = None
|
| 72 |
rating: Optional[int] = None
|
| 73 |
is_helpful: Optional[bool] = None
|
| 74 |
note: Optional[str] = None
|
|
|
|
| 75 |
feedback: Optional[str] = None
|
| 76 |
motivo: Optional[str] = None
|
| 77 |
score_final: Optional[float] = None
|
|
|
|
| 105 |
return Response(status_code=204)
|
| 106 |
|
| 107 |
|
|
|
|
|
|
|
| 108 |
@app.get("/auth/google")
|
| 109 |
def auth_google():
|
| 110 |
+
try:
|
| 111 |
+
authorization_url, _state = get_authorization_url()
|
| 112 |
+
return RedirectResponse(url=authorization_url)
|
| 113 |
+
except Exception as exc:
|
| 114 |
+
return {
|
| 115 |
+
"ok": False,
|
| 116 |
+
"error": str(exc),
|
| 117 |
+
"message": "Nao foi possivel iniciar a autorizacao Google OAuth.",
|
| 118 |
+
}
|
| 119 |
|
| 120 |
|
| 121 |
@app.get("/oauth2callback")
|
| 122 |
+
def oauth2callback(code: str = Query(...)):
|
| 123 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
flow = build_flow()
|
| 125 |
flow.fetch_token(code=code)
|
|
|
|
| 126 |
save_credentials(flow.credentials)
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
return HTMLResponse(
|
| 129 |
+
"""
|
| 130 |
+
<h3>Autorizacao concluida com sucesso.</h3>
|
| 131 |
+
<p>O backend ja pode salvar feedbacks no Google Sheets.</p>
|
| 132 |
+
<p>Voce ja pode fechar esta aba.</p>
|
| 133 |
+
"""
|
| 134 |
)
|
| 135 |
+
except Exception as exc:
|
|
|
|
| 136 |
return HTMLResponse(
|
| 137 |
+
f"""
|
| 138 |
+
<h3>Erro ao concluir autorizacao Google.</h3>
|
| 139 |
+
<p>{str(exc)}</p>
|
| 140 |
+
<p>Verifique GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GOOGLE_REDIRECT_URI e GOOGLE_SPREADSHEET_ID.</p>
|
| 141 |
+
""",
|
| 142 |
+
status_code=500,
|
| 143 |
)
|
| 144 |
|
| 145 |
|
|
|
|
| 236 |
|
| 237 |
try:
|
| 238 |
return salvar_feedback(
|
| 239 |
+
search_id=request.search_id,
|
| 240 |
query=request.query,
|
| 241 |
+
rank=request.rank,
|
| 242 |
product_id=request.product_id,
|
| 243 |
product_name=request.product_name,
|
| 244 |
+
categoria_produto=request.categoria_produto,
|
| 245 |
rating=request.rating,
|
| 246 |
is_helpful=request.is_helpful,
|
| 247 |
note=request.note,
|
|
|
|
| 263 |
"feedback_file": feedback_file,
|
| 264 |
"logs_dir_exists": os.path.exists(LOGS_DIR),
|
| 265 |
"google_sheets_enabled": google_sheets_habilitado(),
|
| 266 |
+
}
|