Spaces:
Sleeping

PERDYPTO commited on
Commit
d4c9742
·
verified ·
1 Parent(s): 0431a0c

Update app/services/nim.py

Browse files
Files changed (1) hide show
  1. app/services/nim.py +34 -2
app/services/nim.py CHANGED
@@ -10,6 +10,36 @@ from app.utils.validation import extract_and_validate_json, is_refusal
10
  logger = logging.getLogger(__name__)
11
 
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  class NIMService:
14
  def __init__(self):
15
  self.base_url = settings.nvidia_base_url
@@ -101,7 +131,7 @@ class NIMService:
101
 
102
  if is_refusal(raw_content):
103
  logger.warning("NIM declined the request (length=%d): %s", len(raw_content), raw_content)
104
- raise ValueError("REFUSED")
105
 
106
  try:
107
  return extract_and_validate_json(raw_content)
@@ -111,4 +141,6 @@ class NIMService:
111
  len(raw_content),
112
  raw_content,
113
  )
114
- raise ValueError(f"Model returned invalid JSON: {e}") from e
 
 
 
10
  logger = logging.getLogger(__name__)
11
 
12
 
13
+ def _decline_result(mode: str, judge_mode: str) -> dict:
14
+ """Build a valid JudgeResult-shaped dict for a declined request.
15
+
16
+ Always returns the same schema as a normal judgment so the frontend
17
+ can render it with the same code path, just flagged as declined.
18
+ """
19
+ if mode == "image":
20
+ reason = (
21
+ "The AI wasn't comfortable making a judgment call from this photo alone. "
22
+ "Try adding a written scenario for context, or use a different photo."
23
+ )
24
+ else:
25
+ reason = (
26
+ "The AI wasn't comfortable judging this particular input. "
27
+ "Try rephrasing the scenario or using different evidence."
28
+ )
29
+
30
+ return {
31
+ "verdict": "YELLOW_FLAG",
32
+ "score": 50,
33
+ "title": "Couldn't reach a verdict",
34
+ "summary": reason,
35
+ "reasons": ["The AI declined to evaluate this input."],
36
+ "vibe": "unclear",
37
+ "advice": "Try again with a different photo or a clearer written scenario.",
38
+ "declined": True,
39
+ "decline_reason": reason,
40
+ }
41
+
42
+
43
  class NIMService:
44
  def __init__(self):
45
  self.base_url = settings.nvidia_base_url
 
131
 
132
  if is_refusal(raw_content):
133
  logger.warning("NIM declined the request (length=%d): %s", len(raw_content), raw_content)
134
+ return _decline_result(mode, judge_mode)
135
 
136
  try:
137
  return extract_and_validate_json(raw_content)
 
141
  len(raw_content),
142
  raw_content,
143
  )
144
+ # Could not parse AND wasn't a clean refusal — still degrade gracefully
145
+ # rather than 502ing, since the frontend now expects JSON either way.
146
+ return _decline_result(mode, judge_mode)