Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,9 +46,8 @@ def clean_and_format_learning_content(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
|
| 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("}"):
|
|
@@ -59,7 +58,7 @@ def clean_and_format_learning_content(output):
|
|
| 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'.")
|
|
@@ -72,7 +71,7 @@ def clean_and_format_learning_content(output):
|
|
| 72 |
return json_output
|
| 73 |
|
| 74 |
except (json.JSONDecodeError, ValueError) as e:
|
| 75 |
-
# Return error
|
| 76 |
return {
|
| 77 |
"error": "Failed to parse or validate output as JSON",
|
| 78 |
"details": str(e),
|
|
|
|
| 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
|
| 50 |
if not cleaned_output.endswith("]}"):
|
|
|
|
| 51 |
if cleaned_output.endswith("]"):
|
| 52 |
cleaned_output += "}" # Close the root object
|
| 53 |
elif cleaned_output.endswith("}"):
|
|
|
|
| 58 |
# Step 3: Attempt to parse the cleaned JSON
|
| 59 |
json_output = json.loads(cleaned_output)
|
| 60 |
|
| 61 |
+
# Step 4: Validate the required structure
|
| 62 |
required_keys = ["title", "sections"]
|
| 63 |
if "title" not in json_output or "sections" not in json_output:
|
| 64 |
raise ValueError("Missing required keys: 'title' or 'sections'.")
|
|
|
|
| 71 |
return json_output
|
| 72 |
|
| 73 |
except (json.JSONDecodeError, ValueError) as e:
|
| 74 |
+
# Return detailed error information
|
| 75 |
return {
|
| 76 |
"error": "Failed to parse or validate output as JSON",
|
| 77 |
"details": str(e),
|