Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,7 +46,10 @@ 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:
|
|
|
|
|
|
|
|
|
|
| 50 |
# Fix missing commas between objects in arrays
|
| 51 |
cleaned_output = re.sub(r'(\})(\s*{)', r'\1,\2', cleaned_output)
|
| 52 |
|
|
@@ -60,23 +63,10 @@ def clean_and_format_learning_content(output):
|
|
| 60 |
if open_brackets > close_brackets:
|
| 61 |
cleaned_output += ']' * (open_brackets - close_brackets)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
cleaned_output = cleaned_output.rstrip('}') + ']}'
|
| 66 |
-
|
| 67 |
-
# Handle misplaced or extra braces/brackets
|
| 68 |
-
cleaned_output = re.sub(r'}\s*}\s*]', '}]', cleaned_output) # Extra closing brace in arrays
|
| 69 |
-
|
| 70 |
-
# Step 3: Attempt to parse and repair malformed JSON
|
| 71 |
-
try:
|
| 72 |
-
json_output = json.loads(cleaned_output) # Attempt to parse as-is
|
| 73 |
-
except json.JSONDecodeError as e:
|
| 74 |
-
# Debugging logs for detailed errors (can be commented out in production)
|
| 75 |
-
print(f"JSON Decode Error at char {e.pos}: {e.msg}")
|
| 76 |
-
print("Cleaned Output:", cleaned_output)
|
| 77 |
-
raise e # Re-raise the exception after logging
|
| 78 |
|
| 79 |
-
# Step 4: Validate
|
| 80 |
required_keys = ["title", "sections"]
|
| 81 |
if "title" not in json_output or "sections" not in json_output:
|
| 82 |
raise ValueError("Missing required keys: 'title' or 'sections'.")
|
|
@@ -96,6 +86,14 @@ def clean_and_format_learning_content(output):
|
|
| 96 |
"output": cleaned_output
|
| 97 |
}
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
# Function to generate learning content
|
| 101 |
def generate_learning_content(topic, description, difficulty, temperature=0.9, max_new_tokens=2000, top_p=0.95, repetition_penalty=1.2):
|
|
|
|
| 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: Fix common structural issues
|
| 50 |
+
# Remove extra closing brackets in arrays
|
| 51 |
+
cleaned_output = re.sub(r'\]\]', ']', cleaned_output)
|
| 52 |
+
|
| 53 |
# Fix missing commas between objects in arrays
|
| 54 |
cleaned_output = re.sub(r'(\})(\s*{)', r'\1,\2', cleaned_output)
|
| 55 |
|
|
|
|
| 63 |
if open_brackets > close_brackets:
|
| 64 |
cleaned_output += ']' * (open_brackets - close_brackets)
|
| 65 |
|
| 66 |
+
# Step 3: Attempt to parse the cleaned output as JSON
|
| 67 |
+
json_output = json.loads(cleaned_output) # Parse JSON
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# Step 4: Validate required structure
|
| 70 |
required_keys = ["title", "sections"]
|
| 71 |
if "title" not in json_output or "sections" not in json_output:
|
| 72 |
raise ValueError("Missing required keys: 'title' or 'sections'.")
|
|
|
|
| 86 |
"output": cleaned_output
|
| 87 |
}
|
| 88 |
|
| 89 |
+
except (json.JSONDecodeError, ValueError) as e:
|
| 90 |
+
# Provide detailed error information for debugging
|
| 91 |
+
return {
|
| 92 |
+
"error": "Failed to parse or validate output as JSON",
|
| 93 |
+
"details": str(e),
|
| 94 |
+
"output": cleaned_output
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
|
| 98 |
# Function to generate learning content
|
| 99 |
def generate_learning_content(topic, description, difficulty, temperature=0.9, max_new_tokens=2000, top_p=0.95, repetition_penalty=1.2):
|