inventwithdean commited on
Commit
8d0964c
·
1 Parent(s): 0e1a0ab

add json-repair

Browse files
Files changed (4) hide show
  1. .gitignore +2 -1
  2. app.py +16 -2
  3. helpers.py +22 -1
  4. requirements.txt +3 -1
.gitignore CHANGED
@@ -2,4 +2,5 @@
2
  local_test.py
3
  .env
4
  upload_to_r2.py
5
- Build
 
 
2
  local_test.py
3
  .env
4
  upload_to_r2.py
5
+ Build
6
+ repair_test.py
app.py CHANGED
@@ -18,9 +18,12 @@ import boto3
18
  from typing import Generator
19
 
20
  from prompts import get_system_prompt
21
- from helpers import parse_dialogue_tags, create_episode_zip, postprocess_script
22
  from forced_alignment import forced_align
23
 
 
 
 
24
  llm_model_path = hf_hub_download(
25
  repo_id="unsloth/gemma-4-26B-A4B-it-GGUF",
26
  filename="gemma-4-26B-A4B-it-UD-Q4_K_M.gguf"
@@ -68,6 +71,7 @@ def warmup():
68
 
69
 
70
 
 
71
  @spaces.GPU()
72
  def run_episode_generation_pipeline(character_names: dict[str, str], problem: str, extras: str = "", language="English"):
73
  llm = Llama(model_path=llm_model_path, n_gpu_layers=-1, n_ctx=8192, flash_attn=True, verbose=False)
@@ -93,7 +97,17 @@ def run_episode_generation_pipeline(character_names: dict[str, str], problem: st
93
 
94
  # Episode JSON
95
  try:
96
- script = json.loads(script_raw)
 
 
 
 
 
 
 
 
 
 
97
  except json.JSONDecodeError as e:
98
  print(f"JSON Parse Error: {e}")
99
  yield "Error: LLM generated an invalid script format. Please try again."
 
18
  from typing import Generator
19
 
20
  from prompts import get_system_prompt
21
+ from helpers import parse_dialogue_tags, create_episode_zip, postprocess_script, expected_schema
22
  from forced_alignment import forced_align
23
 
24
+ import json_repair
25
+ import re
26
+
27
  llm_model_path = hf_hub_download(
28
  repo_id="unsloth/gemma-4-26B-A4B-it-GGUF",
29
  filename="gemma-4-26B-A4B-it-UD-Q4_K_M.gguf"
 
71
 
72
 
73
 
74
+
75
  @spaces.GPU()
76
  def run_episode_generation_pipeline(character_names: dict[str, str], problem: str, extras: str = "", language="English"):
77
  llm = Llama(model_path=llm_model_path, n_gpu_layers=-1, n_ctx=8192, flash_attn=True, verbose=False)
 
97
 
98
  # Episode JSON
99
  try:
100
+ # Try fixing the script if broken
101
+ script = json_repair.loads(
102
+ script_raw,
103
+ schema=expected_schema,
104
+ schema_repair_mode="salvage"
105
+ )
106
+ # Fix starting look_at tags
107
+ for item in script.get("dialogues", []):
108
+ original_text = item.get("dialogue", "")
109
+ fixed_text = re.sub(r'(?<!<)look_at:', '<look_at:', original_text)
110
+ item["dialogue"] = fixed_text
111
  except json.JSONDecodeError as e:
112
  print(f"JSON Parse Error: {e}")
113
  yield "Error: LLM generated an invalid script format. Please try again."
helpers.py CHANGED
@@ -64,4 +64,25 @@ def postprocess_script(script, character_names: dict[str, str]):
64
  if target_name in name_to_role:
65
  look_target["character"] = name_to_role[target_name]
66
 
67
- return script
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  if target_name in name_to_role:
65
  look_target["character"] = name_to_role[target_name]
66
 
67
+ return script
68
+
69
+
70
+
71
+ expected_schema = {
72
+ "type": "object",
73
+ "properties": {
74
+ "dialogues": {
75
+ "type": "array",
76
+ "items": {
77
+ "type": "object",
78
+ "properties": {
79
+ "character": {"type": "string"},
80
+ "dialogue": {"type": "string"},
81
+ "expression": {"type": "string"}
82
+ },
83
+ "required": ["character", "dialogue", "expression"]
84
+ }
85
+ }
86
+ },
87
+ "required": ["dialogues"]
88
+ }
requirements.txt CHANGED
@@ -12,4 +12,6 @@ hf_transfer
12
  omnivoice
13
  --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu124
14
  llama-cpp-python
15
- boto3
 
 
 
12
  omnivoice
13
  --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu124
14
  llama-cpp-python
15
+ boto3
16
+ json-repair
17
+ jsonschema