neuralworm commited on
Commit
bc70f4a
Β·
1 Parent(s): f5f9a1b

initial commit

Browse files
Files changed (1) hide show
  1. gen.py +9 -13
gen.py CHANGED
@@ -181,25 +181,21 @@ def generate(event):
181
 
182
  output_text = tokenizer.decode(tokens[0], skip_special_tokens=False)
183
  print(output_text)
184
- user_prompt_length = len(f"<bos><start_of_turn>user\n{prompt}\n{event}<end_of_turn>\n<start_of_turn>model\n")
185
-
186
  json_start_index = output_text.find("<json>")
187
  json_end_index = output_text.find("</json>")
188
 
189
  if json_start_index != -1 and json_end_index != -1:
190
- json_string = output_text[max(json_start_index + 6, user_prompt_length):json_end_index].strip()
191
 
192
- # Remove any leading/trailing non-JSON characters (if present)
193
- if not json_string.startswith("{"):
194
- first_brace_index = json_string.find("{")
195
- if first_brace_index != -1:
196
- json_string = json_string[first_brace_index:]
197
 
198
- if not json_string.endswith("}"):
199
- last_brace_index = json_string.rfind("}")
200
- if last_brace_index != -1:
201
- json_string = json_string[:last_brace_index + 1]
 
 
202
 
203
- return json_string
204
  else:
205
  return "Error: <json> or </json> not found in generated output"
 
181
 
182
  output_text = tokenizer.decode(tokens[0], skip_special_tokens=False)
183
  print(output_text)
 
 
184
  json_start_index = output_text.find("<json>")
185
  json_end_index = output_text.find("</json>")
186
 
187
  if json_start_index != -1 and json_end_index != -1:
188
+ json_string = output_text[json_start_index + 6:json_end_index].strip()
189
 
190
+ # Debugging: Print the extracted JSON string to check its contents
191
+ print("Extracted JSON String:", json_string)
 
 
 
192
 
193
+ # Load and return the JSON data
194
+ try:
195
+ data = json.loads(json_string)
196
+ return data
197
+ except json.JSONDecodeError as e:
198
+ return f"Error: Invalid JSON - {e}"
199
 
 
200
  else:
201
  return "Error: <json> or </json> not found in generated output"