Britania commited on
Commit
fb2a5f9
路
verified 路
1 Parent(s): c703b48

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +18 -28
handler.py CHANGED
@@ -17,31 +17,6 @@ schema = {
17
  "required": ["objetivoDistancia", "calentamiento", "drills", "sprints", "enfriamiento"]
18
  }
19
 
20
- def parse_and_validate_json(output):
21
- output = output.replace("\n\n", "\n").strip()
22
- outputs = output.split("\n")
23
-
24
- for single_output in outputs:
25
- try:
26
- corrected_json = single_output.replace('}\n"', '}, "')
27
- data = json.loads(corrected_json)
28
- validate(instance=data, schema=schema)
29
- return True, corrected_json
30
- except (json.JSONDecodeError, ValidationError):
31
- continue
32
-
33
- return False, "No se encontr贸 ning煤n JSON v谩lido seg煤n el esquema requerido."
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
  class EndpointHandler():
46
  def __init__(self, path=""):
47
  self.model = AutoModelForCausalLM.from_pretrained(
@@ -52,6 +27,21 @@ class EndpointHandler():
52
  )
53
  self.my_tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  def model_inference(model, tokenizer, text, **parameters):
56
  default_parameters = {
57
  "max_new_tokens": 300,
@@ -69,8 +59,8 @@ class EndpointHandler():
69
 
70
  def model_inference_with_validation(filled_template, **parameters):
71
  while True:
72
- result = model_inference(self.model, self.my_tokenizer, filled_template, **parameters)
73
- is_valid, response_or_error = parse_and_validate_json(result)
74
  if is_valid:
75
  return {'generated_text': [result]}
76
 
@@ -88,6 +78,6 @@ class EndpointHandler():
88
  data_text = json.dumps(input_data, indent=4)
89
  filled_template = template.replace("{data}", data_text)
90
 
91
- return model_inference_with_validation(filled_template, **parameters)
92
 
93
 
 
17
  "required": ["objetivoDistancia", "calentamiento", "drills", "sprints", "enfriamiento"]
18
  }
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  class EndpointHandler():
21
  def __init__(self, path=""):
22
  self.model = AutoModelForCausalLM.from_pretrained(
 
27
  )
28
  self.my_tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
29
 
30
+ def parse_and_validate_json(output):
31
+ output = output.replace("\n\n", "\n").strip()
32
+ outputs = output.split("\n")
33
+
34
+ for single_output in outputs:
35
+ try:
36
+ corrected_json = single_output.replace('}\n"', '}, "')
37
+ data = json.loads(corrected_json)
38
+ validate(instance=data, schema=schema)
39
+ return True, corrected_json
40
+ except (json.JSONDecodeError, ValidationError):
41
+ continue
42
+
43
+ return False, "No se encontr贸 ning煤n JSON v谩lido seg煤n el esquema requerido."
44
+
45
  def model_inference(model, tokenizer, text, **parameters):
46
  default_parameters = {
47
  "max_new_tokens": 300,
 
59
 
60
  def model_inference_with_validation(filled_template, **parameters):
61
  while True:
62
+ result = self.model_inference(self.model, self.my_tokenizer, filled_template, **parameters)
63
+ is_valid, response_or_error = self.parse_and_validate_json(result)
64
  if is_valid:
65
  return {'generated_text': [result]}
66
 
 
78
  data_text = json.dumps(input_data, indent=4)
79
  filled_template = template.replace("{data}", data_text)
80
 
81
+ return self.model_inference_with_validation(filled_template, **parameters)
82
 
83