Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,17 +46,19 @@ 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: Fix
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
cleaned_output = re.sub(r'(\})(\s*{)', r'\1,\2', cleaned_output)
|
| 58 |
|
| 59 |
-
# Step
|
| 60 |
open_braces = cleaned_output.count('{')
|
| 61 |
close_braces = cleaned_output.count('}')
|
| 62 |
open_brackets = cleaned_output.count('[')
|
|
@@ -66,10 +68,10 @@ def clean_and_format_learning_content(output):
|
|
| 66 |
if open_brackets > close_brackets:
|
| 67 |
cleaned_output += ']' * (open_brackets - close_brackets)
|
| 68 |
|
| 69 |
-
# Step
|
| 70 |
json_output = json.loads(cleaned_output)
|
| 71 |
|
| 72 |
-
# Step
|
| 73 |
required_keys = ["title", "sections"]
|
| 74 |
if "title" not in json_output or "sections" not in json_output:
|
| 75 |
raise ValueError("Missing required keys: 'title' or 'sections'.")
|
|
@@ -89,6 +91,7 @@ def clean_and_format_learning_content(output):
|
|
| 89 |
"output": cleaned_output
|
| 90 |
}
|
| 91 |
|
|
|
|
| 92 |
# Function to generate learning content
|
| 93 |
def generate_learning_content(topic, description, difficulty, temperature=0.9, max_new_tokens=2000, top_p=0.95, repetition_penalty=1.2):
|
| 94 |
"""
|
|
|
|
| 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 the 'sections' structure
|
| 50 |
+
# Ensure 'sections' is an array, not a single object
|
| 51 |
+
sections_pattern = r'"sections":\s*{'
|
| 52 |
+
if re.search(sections_pattern, cleaned_output):
|
| 53 |
+
# Replace the invalid opening brace with an array opening
|
| 54 |
+
cleaned_output = re.sub(sections_pattern, '"sections": [ {', cleaned_output)
|
| 55 |
+
# Add a closing array bracket at the end of 'sections'
|
| 56 |
+
cleaned_output = re.sub(r'(}\s*,?\s*)]}', r'} ] }', cleaned_output)
|
| 57 |
+
|
| 58 |
+
# Step 3: Fix missing commas between JSON objects
|
| 59 |
cleaned_output = re.sub(r'(\})(\s*{)', r'\1,\2', cleaned_output)
|
| 60 |
|
| 61 |
+
# Step 4: Fix unbalanced brackets and braces
|
| 62 |
open_braces = cleaned_output.count('{')
|
| 63 |
close_braces = cleaned_output.count('}')
|
| 64 |
open_brackets = cleaned_output.count('[')
|
|
|
|
| 68 |
if open_brackets > close_brackets:
|
| 69 |
cleaned_output += ']' * (open_brackets - close_brackets)
|
| 70 |
|
| 71 |
+
# Step 5: Attempt to parse JSON
|
| 72 |
json_output = json.loads(cleaned_output)
|
| 73 |
|
| 74 |
+
# Step 6: Validate JSON structure
|
| 75 |
required_keys = ["title", "sections"]
|
| 76 |
if "title" not in json_output or "sections" not in json_output:
|
| 77 |
raise ValueError("Missing required keys: 'title' or 'sections'.")
|
|
|
|
| 91 |
"output": cleaned_output
|
| 92 |
}
|
| 93 |
|
| 94 |
+
|
| 95 |
# Function to generate learning content
|
| 96 |
def generate_learning_content(topic, description, difficulty, temperature=0.9, max_new_tokens=2000, top_p=0.95, repetition_penalty=1.2):
|
| 97 |
"""
|