JaceWei commited on
Commit
4469ee0
·
1 Parent(s): a3e00a9
Paper2Poster/PosterAgent/parse_raw.py CHANGED
@@ -1,8 +1,9 @@
1
  from dotenv import load_dotenv
2
- from utils.src.utils import get_json_from_response
3
  from utils.src.model_utils import parse_pdf
4
  import json
5
  import random
 
6
 
7
  from camel.models import ModelFactory
8
  from camel.agents import ChatAgent
@@ -46,7 +47,17 @@ def account_token(response):
46
  output_token = response.info['usage']['completion_tokens']
47
 
48
  return input_token, output_token
49
-
 
 
 
 
 
 
 
 
 
 
50
  @retry(stop=stop_after_attempt(5))
51
  def parse_raw(args, actor_config, version=2):
52
  raw_source = args.poster_path
 
1
  from dotenv import load_dotenv
2
+ # from utils.src.utils import get_json_from_response
3
  from utils.src.model_utils import parse_pdf
4
  import json
5
  import random
6
+ import json_repair
7
 
8
  from camel.models import ModelFactory
9
  from camel.agents import ChatAgent
 
47
  output_token = response.info['usage']['completion_tokens']
48
 
49
  return input_token, output_token
50
+ def get_json_from_response(raw_response: str):
51
+ response = raw_response.strip()
52
+ l, r = response.rfind("```json"), response.rfind("```")
53
+ try:
54
+ if l == -1 or r == -1:
55
+ response = json_repair.loads(response)
56
+ else:
57
+ response = json_repair.loads(response[l + 7 : r].strip())
58
+ return response
59
+ except Exception as e:
60
+ raise RuntimeError("Failed to parse JSON from response", e)
61
  @retry(stop=stop_after_attempt(5))
62
  def parse_raw(args, actor_config, version=2):
63
  raw_source = args.poster_path