abhiimanyu commited on
Commit
cc97dfc
·
verified ·
1 Parent(s): 9bdb1f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -33,7 +33,8 @@ def format_prompt(topic, description, difficulty):
33
  # Function to clean and format the AI output
34
  def clean_and_format_learning_content(output):
35
  """
36
- Cleans, validates, and parses JSON output for learning content.
 
37
  """
38
  try:
39
  # Step 1: Clean the raw output
@@ -45,16 +46,23 @@ def clean_and_format_learning_content(output):
45
  cleaned_output = cleaned_output.replace('\\"', '"') # Fix improperly escaped quotes
46
  cleaned_output = re.sub(r',\s*(\}|\])', r'\1', cleaned_output) # Remove trailing commas
47
 
48
- # Step 2: Attempt to parse the cleaned JSON
 
 
 
 
 
 
 
 
 
 
49
  json_output = json.loads(cleaned_output)
50
 
51
- # Step 3: Validate required keys
52
  required_keys = ["title", "sections"]
53
- for key in required_keys:
54
- if key not in json_output:
55
- raise ValueError(f"Missing key: {key}")
56
-
57
- # Ensure "sections" is a list and has the required structure
58
  if not isinstance(json_output["sections"], list):
59
  raise ValueError("'sections' must be a list.")
60
  for section in json_output["sections"]:
@@ -64,14 +72,13 @@ def clean_and_format_learning_content(output):
64
  return json_output
65
 
66
  except (json.JSONDecodeError, ValueError) as e:
67
- # Return error details if parsing or validation fails
68
  return {
69
  "error": "Failed to parse or validate output as JSON",
70
  "details": str(e),
71
  "output": cleaned_output
72
  }
73
 
74
-
75
  # Function to generate learning content
76
  def generate_learning_content(topic, description, difficulty, temperature=0.9, max_new_tokens=2000, top_p=0.95, repetition_penalty=1.2):
77
  """
 
33
  # Function to clean and format the AI output
34
  def clean_and_format_learning_content(output):
35
  """
36
+ Cleans, validates, and attempts to auto-repair JSON output for learning content.
37
+ Handles truncated and malformed JSON gracefully.
38
  """
39
  try:
40
  # Step 1: Clean the raw output
 
46
  cleaned_output = cleaned_output.replace('\\"', '"') # Fix improperly escaped quotes
47
  cleaned_output = re.sub(r',\s*(\}|\])', r'\1', cleaned_output) # Remove trailing commas
48
 
49
+ # Step 2: Detect truncation and auto-repair if necessary
50
+ if not cleaned_output.endswith("]}"):
51
+ # Check for open array or object and close them
52
+ if cleaned_output.endswith("]"):
53
+ cleaned_output += "}" # Close the root object
54
+ elif cleaned_output.endswith("}"):
55
+ cleaned_output = cleaned_output[:-1] + "]}" # Close the sections array and root object
56
+ else:
57
+ cleaned_output += "]}" # Append both array and object closures
58
+
59
+ # Step 3: Attempt to parse the cleaned JSON
60
  json_output = json.loads(cleaned_output)
61
 
62
+ # Step 4: Validate the structure
63
  required_keys = ["title", "sections"]
64
+ if "title" not in json_output or "sections" not in json_output:
65
+ raise ValueError("Missing required keys: 'title' or 'sections'.")
 
 
 
66
  if not isinstance(json_output["sections"], list):
67
  raise ValueError("'sections' must be a list.")
68
  for section in json_output["sections"]:
 
72
  return json_output
73
 
74
  except (json.JSONDecodeError, ValueError) as e:
75
+ # Return error details and cleaned output for debugging
76
  return {
77
  "error": "Failed to parse or validate output as JSON",
78
  "details": str(e),
79
  "output": cleaned_output
80
  }
81
 
 
82
  # Function to generate learning content
83
  def generate_learning_content(topic, description, difficulty, temperature=0.9, max_new_tokens=2000, top_p=0.95, repetition_penalty=1.2):
84
  """