PERDYPTO commited on
Update app/main.py
Browse files- app/main.py +22 -1
app/main.py
CHANGED
|
@@ -20,6 +20,8 @@ logger = logging.getLogger(__name__)
|
|
| 20 |
|
| 21 |
nim_service = NIMService()
|
| 22 |
|
|
|
|
|
|
|
| 23 |
|
| 24 |
@asynccontextmanager
|
| 25 |
async def lifespan(app: FastAPI):
|
|
@@ -84,6 +86,7 @@ async def health():
|
|
| 84 |
@app.post("/judge", response_model=SuccessResponse)
|
| 85 |
async def judge(
|
| 86 |
mode: str = Form(...),
|
|
|
|
| 87 |
tone: str = Form(...),
|
| 88 |
scenario: str | None = Form(None),
|
| 89 |
image: UploadFile | None = File(None),
|
|
@@ -97,6 +100,13 @@ async def judge(
|
|
| 97 |
"message": f"Invalid mode: {mode}. Must be one of: text, image, image_text"
|
| 98 |
})
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# Validate tone enum
|
| 101 |
try:
|
| 102 |
Tone(tone)
|
|
@@ -130,8 +140,19 @@ async def judge(
|
|
| 130 |
|
| 131 |
# Call NVIDIA NIM
|
| 132 |
try:
|
| 133 |
-
result = await nim_service.judge(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
except ValueError as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
logger.error("JSON validation failed: %s", str(e))
|
| 136 |
raise HTTPException(status_code=502, detail={
|
| 137 |
"code": "INVALID_AI_RESPONSE",
|
|
|
|
| 20 |
|
| 21 |
nim_service = NIMService()
|
| 22 |
|
| 23 |
+
VALID_JUDGE_MODES = {"judge_me", "judge_them"}
|
| 24 |
+
|
| 25 |
|
| 26 |
@asynccontextmanager
|
| 27 |
async def lifespan(app: FastAPI):
|
|
|
|
| 86 |
@app.post("/judge", response_model=SuccessResponse)
|
| 87 |
async def judge(
|
| 88 |
mode: str = Form(...),
|
| 89 |
+
judge_mode: str = Form("judge_me"),
|
| 90 |
tone: str = Form(...),
|
| 91 |
scenario: str | None = Form(None),
|
| 92 |
image: UploadFile | None = File(None),
|
|
|
|
| 100 |
"message": f"Invalid mode: {mode}. Must be one of: text, image, image_text"
|
| 101 |
})
|
| 102 |
|
| 103 |
+
# Validate judge_mode
|
| 104 |
+
if judge_mode not in VALID_JUDGE_MODES:
|
| 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"
|
| 108 |
+
})
|
| 109 |
+
|
| 110 |
# Validate tone enum
|
| 111 |
try:
|
| 112 |
Tone(tone)
|
|
|
|
| 140 |
|
| 141 |
# Call NVIDIA NIM
|
| 142 |
try:
|
| 143 |
+
result = await nim_service.judge(
|
| 144 |
+
mode=mode,
|
| 145 |
+
judge_mode=judge_mode,
|
| 146 |
+
scenario=scenario,
|
| 147 |
+
tone=tone,
|
| 148 |
+
image_b64=image_b64,
|
| 149 |
+
)
|
| 150 |
except ValueError as e:
|
| 151 |
+
if str(e) == "REFUSED":
|
| 152 |
+
raise HTTPException(status_code=422, detail={
|
| 153 |
+
"code": "AI_DECLINED",
|
| 154 |
+
"message": "The AI declined to judge this input. Try a different photo or rephrase your scenario."
|
| 155 |
+
})
|
| 156 |
logger.error("JSON validation failed: %s", str(e))
|
| 157 |
raise HTTPException(status_code=502, detail={
|
| 158 |
"code": "INVALID_AI_RESPONSE",
|