PERDYPTO commited on
Update app/main.py
Browse files- app/main.py +39 -9
app/main.py
CHANGED
|
@@ -8,7 +8,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 8 |
from fastapi.responses import JSONResponse
|
| 9 |
|
| 10 |
from app.config import settings
|
| 11 |
-
from app.schemas import Mode, Tone, SuccessResponse, ErrorResponse
|
| 12 |
from app.services.nim import NIMService
|
| 13 |
from app.utils.validation import validate_scenario, validate_image
|
| 14 |
|
|
@@ -20,8 +20,6 @@ logger = logging.getLogger(__name__)
|
|
| 20 |
|
| 21 |
nim_service = NIMService()
|
| 22 |
|
| 23 |
-
VALID_JUDGE_MODES = {"judge_me", "judge_them"}
|
| 24 |
-
|
| 25 |
|
| 26 |
@asynccontextmanager
|
| 27 |
async def lifespan(app: FastAPI):
|
|
@@ -78,6 +76,34 @@ async def request_validation_exception_handler(request, exc):
|
|
| 78 |
)
|
| 79 |
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
@app.get("/health")
|
| 82 |
async def health():
|
| 83 |
return {"status": "ok"}
|
|
@@ -100,8 +126,10 @@ async def judge(
|
|
| 100 |
"message": f"Invalid mode: {mode}. Must be one of: text, image, image_text"
|
| 101 |
})
|
| 102 |
|
| 103 |
-
# Validate judge_mode
|
| 104 |
-
|
|
|
|
|
|
|
| 105 |
raise HTTPException(status_code=400, detail={
|
| 106 |
"code": "INVALID_REQUEST",
|
| 107 |
"message": f"Invalid judge_mode: {judge_mode}. Must be one of: judge_me, judge_them"
|
|
@@ -149,10 +177,12 @@ async def judge(
|
|
| 149 |
)
|
| 150 |
except ValueError as e:
|
| 151 |
if str(e) == "REFUSED":
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
| 156 |
logger.error("JSON validation failed: %s", str(e))
|
| 157 |
raise HTTPException(status_code=502, detail={
|
| 158 |
"code": "INVALID_AI_RESPONSE",
|
|
|
|
| 8 |
from fastapi.responses import JSONResponse
|
| 9 |
|
| 10 |
from app.config import settings
|
| 11 |
+
from app.schemas import Mode, JudgeMode, Tone, JudgeResult, SuccessResponse, ErrorResponse
|
| 12 |
from app.services.nim import NIMService
|
| 13 |
from app.utils.validation import validate_scenario, validate_image
|
| 14 |
|
|
|
|
| 20 |
|
| 21 |
nim_service = NIMService()
|
| 22 |
|
|
|
|
|
|
|
| 23 |
|
| 24 |
@asynccontextmanager
|
| 25 |
async def lifespan(app: FastAPI):
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
|
| 79 |
+
def _build_unjudged_result(mode: str, judge_mode: str) -> JudgeResult:
|
| 80 |
+
"""Backend-authored fallback result when the AI declines to judge the input.
|
| 81 |
+
|
| 82 |
+
We never rely on the model to explain its own refusal — refusal text is
|
| 83 |
+
short and unstructured. Instead we build a valid, schema-correct result
|
| 84 |
+
ourselves so the frontend renders exactly the same way it always does.
|
| 85 |
+
"""
|
| 86 |
+
if mode == "image":
|
| 87 |
+
reason = "The photo alone didn't give enough to go on for a fair judgment."
|
| 88 |
+
elif judge_mode == "judge_them":
|
| 89 |
+
reason = "This one touches on something the AI isn't comfortable weighing in on for someone else."
|
| 90 |
+
else:
|
| 91 |
+
reason = "The AI wasn't able to reach a verdict on this one."
|
| 92 |
+
|
| 93 |
+
return JudgeResult(
|
| 94 |
+
verdict="UNJUDGED",
|
| 95 |
+
score=0,
|
| 96 |
+
title="No verdict this time",
|
| 97 |
+
summary=reason,
|
| 98 |
+
reasons=[
|
| 99 |
+
"Try adding more written context about the situation.",
|
| 100 |
+
"If you used a photo, a different photo or adding text may help.",
|
| 101 |
+
],
|
| 102 |
+
vibe="unclear",
|
| 103 |
+
advice="Rephrase the scenario with more detail, or try a different photo, and judge again.",
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
@app.get("/health")
|
| 108 |
async def health():
|
| 109 |
return {"status": "ok"}
|
|
|
|
| 126 |
"message": f"Invalid mode: {mode}. Must be one of: text, image, image_text"
|
| 127 |
})
|
| 128 |
|
| 129 |
+
# Validate judge_mode enum
|
| 130 |
+
try:
|
| 131 |
+
JudgeMode(judge_mode)
|
| 132 |
+
except ValueError:
|
| 133 |
raise HTTPException(status_code=400, detail={
|
| 134 |
"code": "INVALID_REQUEST",
|
| 135 |
"message": f"Invalid judge_mode: {judge_mode}. Must be one of: judge_me, judge_them"
|
|
|
|
| 177 |
)
|
| 178 |
except ValueError as e:
|
| 179 |
if str(e) == "REFUSED":
|
| 180 |
+
# Always return a valid, schema-correct JudgeResult -- never a
|
| 181 |
+
# generic error -- so the user sees a real (if unjudged) card
|
| 182 |
+
# explaining why, and the frontend needs no separate error path.
|
| 183 |
+
fallback = _build_unjudged_result(mode, judge_mode)
|
| 184 |
+
return SuccessResponse(result=fallback)
|
| 185 |
+
|
| 186 |
logger.error("JSON validation failed: %s", str(e))
|
| 187 |
raise HTTPException(status_code=502, detail={
|
| 188 |
"code": "INVALID_AI_RESPONSE",
|