abhiimanyu commited on
Commit
55240da
·
verified ·
1 Parent(s): 8b39ec9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -14
app.py CHANGED
@@ -31,12 +31,9 @@ def format_prompt(topic, description, difficulty):
31
  return prompt
32
 
33
  # Function to clean and format the AI output
34
- import json
35
- import re
36
-
37
  def clean_and_format_learning_content(output):
38
  """
39
- Cleans, validates, and repairs JSON output for learning content.
40
  """
41
  try:
42
  # Step 1: Clean the raw output
@@ -48,24 +45,26 @@ def clean_and_format_learning_content(output):
48
  cleaned_output = cleaned_output.replace('\\"', '"') # Fix improperly escaped quotes
49
  cleaned_output = re.sub(r',\s*(\}|\])', r'\1', cleaned_output) # Remove trailing commas
50
 
51
- # Step 2: Detect truncation and auto-repair
52
- if not cleaned_output.endswith("]}"):
53
- # Count unmatched braces and brackets
 
 
 
 
 
54
  open_braces = cleaned_output.count('{')
55
  close_braces = cleaned_output.count('}')
56
  open_brackets = cleaned_output.count('[')
57
  close_brackets = cleaned_output.count(']')
58
-
59
- # Add missing closing braces or brackets
60
  if open_braces > close_braces:
61
  cleaned_output += '}' * (open_braces - close_braces)
62
  if open_brackets > close_brackets:
63
  cleaned_output += ']' * (open_brackets - close_brackets)
 
 
64
 
65
- # Step 3: Attempt to parse the cleaned JSON
66
- json_output = json.loads(cleaned_output)
67
-
68
- # Step 4: Validate the required structure
69
  required_keys = ["title", "sections"]
70
  if "title" not in json_output or "sections" not in json_output:
71
  raise ValueError("Missing required keys: 'title' or 'sections'.")
@@ -78,7 +77,7 @@ def clean_and_format_learning_content(output):
78
  return json_output
79
 
80
  except (json.JSONDecodeError, ValueError) as e:
81
- # Return detailed error information for debugging
82
  return {
83
  "error": "Failed to parse or validate output as JSON",
84
  "details": str(e),
 
31
  return prompt
32
 
33
  # Function to clean and format the AI output
 
 
 
34
  def clean_and_format_learning_content(output):
35
  """
36
+ Cleans, validates, and attempts to repair JSON output for learning content.
37
  """
38
  try:
39
  # Step 1: Clean the raw 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 and repair malformed JSON
49
+ try:
50
+ json_output = json.loads(cleaned_output) # First attempt to parse as-is
51
+ except json.JSONDecodeError:
52
+ # Attempt repair by handling common issues
53
+ # Fix missing commas between sections
54
+ cleaned_output = re.sub(r'(\})(\s*{)', r'\1,\2', cleaned_output)
55
+ # Fix missing closing brackets or braces
56
  open_braces = cleaned_output.count('{')
57
  close_braces = cleaned_output.count('}')
58
  open_brackets = cleaned_output.count('[')
59
  close_brackets = cleaned_output.count(']')
 
 
60
  if open_braces > close_braces:
61
  cleaned_output += '}' * (open_braces - close_braces)
62
  if open_brackets > close_brackets:
63
  cleaned_output += ']' * (open_brackets - close_brackets)
64
+ # Retry parsing
65
+ json_output = json.loads(cleaned_output)
66
 
67
+ # Step 3: Validate the required structure
 
 
 
68
  required_keys = ["title", "sections"]
69
  if "title" not in json_output or "sections" not in json_output:
70
  raise ValueError("Missing required keys: 'title' or 'sections'.")
 
77
  return json_output
78
 
79
  except (json.JSONDecodeError, ValueError) as e:
80
+ # Return detailed error information
81
  return {
82
  "error": "Failed to parse or validate output as JSON",
83
  "details": str(e),