aika42 commited on
Commit
0a8a30a
·
verified ·
1 Parent(s): 24c72cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -2
app.py CHANGED
@@ -2,6 +2,8 @@ import streamlit as st
2
  import requests
3
  import json
4
  import os
 
 
5
 
6
  HF_API_URL = "https://router.huggingface.co/novita/v3/openai/chat/completions"
7
  HF_TOKEN = os.environ["HF_PROJECT_TOKEN"]
@@ -60,8 +62,13 @@ def evaluate_prompt(user_prompt):
60
  if isinstance(result, dict) and "choices" in result:
61
  raw_text = result["choices"][0]["message"]["content"]
62
  try:
63
- json_part = raw_text[raw_text.index("{"):]
64
- return json.loads(json_part)
 
 
 
 
 
65
  except (ValueError, json.JSONDecodeError) as e:
66
  return {"error": f"Failed to parse model output: {str(e)}"}
67
  return result
 
2
  import requests
3
  import json
4
  import os
5
+ import re
6
+
7
 
8
  HF_API_URL = "https://router.huggingface.co/novita/v3/openai/chat/completions"
9
  HF_TOKEN = os.environ["HF_PROJECT_TOKEN"]
 
62
  if isinstance(result, dict) and "choices" in result:
63
  raw_text = result["choices"][0]["message"]["content"]
64
  try:
65
+ # This regex finds the first complete JSON block
66
+ json_match = re.search(r'\{[\s\S]*\}', raw_text)
67
+ if json_match:
68
+ json_str = json_match.group()
69
+ return json.loads(json_str)
70
+ else:
71
+ return {"error": "No valid JSON object found in model output."}
72
  except (ValueError, json.JSONDecodeError) as e:
73
  return {"error": f"Failed to parse model output: {str(e)}"}
74
  return result