allenpark commited on
Commit
e899803
·
verified ·
1 Parent(s): 7484cb4

Add cleaning json string function and loading it to json from string

Browse files
Files changed (1) hide show
  1. app.py +10 -1
app.py CHANGED
@@ -86,6 +86,14 @@ HEADER = """
86
  **Getting Started**: Provide a question and document or context given to your model in addition to the answer given by the model and then click submit. The output panel will indicate whether the reponse is a hallucination (Fail) or if it is faithful to the given document or context (Pass) through the score Pass or Fail and provide reasoning behind the score.
87
  """
88
 
 
 
 
 
 
 
 
 
89
  # @spaces.GPU()
90
  # def model_call(question, document, answer, tokenizer, model):
91
  def model_call(question, document, answer):
@@ -97,7 +105,8 @@ def model_call(question, document, answer):
97
  prompt=NEW_FORMAT
98
  )
99
  print("RESPONSE FROM CLIENT:", response)
100
- generated_text = json.loads(response.choices[0].text)
 
101
  print("GENERATED TEXT", generated_text)
102
  print("type of GENERATED TEXT", type(generated_text))
103
  reasoning = generated_text["REASONING"][0]
 
86
  **Getting Started**: Provide a question and document or context given to your model in addition to the answer given by the model and then click submit. The output panel will indicate whether the reponse is a hallucination (Fail) or if it is faithful to the given document or context (Pass) through the score Pass or Fail and provide reasoning behind the score.
87
  """
88
 
89
+ def clean_json_string(json_str):
90
+ # Replace single quotes with double quotes, but not apostrophes within words
91
+ json_str = re.sub(r"(?<!\\)'([^']*)'", r'"\1"', json_str)
92
+ # Add quotes around PASS or FAIL if they're not already quoted
93
+ json_str = re.sub(r'"SCORE":\s*(PASS|FAIL)', r'"SCORE": "\1"', json_str)
94
+
95
+ return json_st
96
+
97
  # @spaces.GPU()
98
  # def model_call(question, document, answer, tokenizer, model):
99
  def model_call(question, document, answer):
 
105
  prompt=NEW_FORMAT
106
  )
107
  print("RESPONSE FROM CLIENT:", response)
108
+ generated_text = clean_json_string(response.choices[0].text)
109
+ generated_text = json.loads(generated_text)
110
  print("GENERATED TEXT", generated_text)
111
  print("type of GENERATED TEXT", type(generated_text))
112
  reasoning = generated_text["REASONING"][0]