Spaces:
Sleeping

PERDYPTO commited on
Commit
e815922
·
verified ·
1 Parent(s): f025ee2

Update app/services/nim.py

Browse files
Files changed (1) hide show
  1. app/services/nim.py +44 -11
app/services/nim.py CHANGED
@@ -1,6 +1,8 @@
1
  import base64
2
  import logging
 
3
  import httpx
 
4
  from app.config import settings
5
  from app.prompts import JUDGEME_BASE_PROMPT, TONE_INSTRUCTIONS
6
  from app.utils.validation import extract_and_validate_json
@@ -14,35 +16,57 @@ class NIMService:
14
  self.model = settings.nvidia_model
15
  self.timeout = settings.nvidia_timeout
16
 
17
- def _build_messages(self, mode: str, scenario: str | None, tone: str, image_b64: str | None) -> list[dict]:
 
 
 
 
 
 
 
18
  tone_instruction = TONE_INSTRUCTIONS.get(tone, TONE_INSTRUCTIONS["normal"])
19
- user_content = []
20
 
21
- # Build multimodal content array per NVIDIA NIM spec
22
  if mode == "text":
23
- user_content.append({"type": "text", "text": f"Tone: {tone_instruction}\n\nScenario: {scenario}"})
 
 
 
24
 
25
  elif mode == "image":
26
  user_content.append({
27
  "type": "image_url",
28
- "image_url": {"url": f"data:image/jpeg;base64,{image_b64}"}
 
 
 
 
29
  })
30
- user_content.append({"type": "text", "text": f"Tone: {tone_instruction}\n\nJudge this image."})
31
 
32
  elif mode == "image_text":
33
  user_content.append({
34
  "type": "image_url",
35
- "image_url": {"url": f"data:image/jpeg;base64,{image_b64}"}
 
 
 
 
36
  })
37
- user_content.append({"type": "text", "text": f"Tone: {tone_instruction}\n\nScenario: {scenario}"})
38
 
39
  return [
40
  {"role": "system", "content": JUDGEME_BASE_PROMPT},
41
  {"role": "user", "content": user_content},
42
  ]
43
 
44
- async def judge(self, mode: str, scenario: str | None, tone: str, image_b64: str | None) -> dict:
45
- messages = self._build_messages(mode, scenario, tone, image_b64)
 
 
 
 
 
 
 
46
 
47
  payload = {
48
  "model": self.model,
@@ -68,4 +92,13 @@ class NIMService:
68
  raw_content = data["choices"][0]["message"]["content"]
69
  logger.info("NIM raw response received (length=%d)", len(raw_content))
70
 
71
- return extract_and_validate_json(raw_content)
 
 
 
 
 
 
 
 
 
 
1
  import base64
2
  import logging
3
+
4
  import httpx
5
+
6
  from app.config import settings
7
  from app.prompts import JUDGEME_BASE_PROMPT, TONE_INSTRUCTIONS
8
  from app.utils.validation import extract_and_validate_json
 
16
  self.model = settings.nvidia_model
17
  self.timeout = settings.nvidia_timeout
18
 
19
+ def _build_messages(
20
+ self,
21
+ mode: str,
22
+ scenario: str | None,
23
+ tone: str,
24
+ image_b64: str | None,
25
+ image_mime: str = "image/png",
26
+ ) -> list[dict]:
27
  tone_instruction = TONE_INSTRUCTIONS.get(tone, TONE_INSTRUCTIONS["normal"])
28
+ user_content: list[dict] = []
29
 
 
30
  if mode == "text":
31
+ user_content.append({
32
+ "type": "text",
33
+ "text": f"Tone: {tone_instruction}\n\nScenario: {scenario}",
34
+ })
35
 
36
  elif mode == "image":
37
  user_content.append({
38
  "type": "image_url",
39
+ "image_url": {"url": f"data:{image_mime};base64,{image_b64}"},
40
+ })
41
+ user_content.append({
42
+ "type": "text",
43
+ "text": f"Tone: {tone_instruction}\n\nJudge this image.",
44
  })
 
45
 
46
  elif mode == "image_text":
47
  user_content.append({
48
  "type": "image_url",
49
+ "image_url": {"url": f"data:{image_mime};base64,{image_b64}"},
50
+ })
51
+ user_content.append({
52
+ "type": "text",
53
+ "text": f"Tone: {tone_instruction}\n\nScenario: {scenario}",
54
  })
 
55
 
56
  return [
57
  {"role": "system", "content": JUDGEME_BASE_PROMPT},
58
  {"role": "user", "content": user_content},
59
  ]
60
 
61
+ async def judge(
62
+ self,
63
+ mode: str,
64
+ scenario: str | None,
65
+ tone: str,
66
+ image_b64: str | None,
67
+ image_mime: str = "image/png",
68
+ ) -> dict:
69
+ messages = self._build_messages(mode, scenario, tone, image_b64, image_mime)
70
 
71
  payload = {
72
  "model": self.model,
 
92
  raw_content = data["choices"][0]["message"]["content"]
93
  logger.info("NIM raw response received (length=%d)", len(raw_content))
94
 
95
+ try:
96
+ return extract_and_validate_json(raw_content)
97
+ except ValueError as e:
98
+ # Log the actual model output so you can see refusals/truncation
99
+ logger.warning(
100
+ "NIM returned non-JSON response (length=%d): %.500s",
101
+ len(raw_content),
102
+ raw_content,
103
+ )
104
+ raise ValueError(f"Model returned invalid JSON: {e}") from e