Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,7 +34,7 @@ def format_prompt(topic, description, difficulty):
|
|
| 34 |
def clean_and_format_output(output):
|
| 35 |
"""
|
| 36 |
Cleans, validates, and attempts to auto-repair JSON output from the AI model.
|
| 37 |
-
Handles
|
| 38 |
"""
|
| 39 |
try:
|
| 40 |
# Step 1: Clean the raw output
|
|
@@ -45,12 +45,13 @@ def clean_and_format_output(output):
|
|
| 45 |
cleaned_output = re.sub(r'\s+', ' ', cleaned_output).strip() # Normalize whitespace
|
| 46 |
cleaned_output = re.sub(r',\s*(\}|\])', r'\1', cleaned_output) # Remove trailing commas
|
| 47 |
|
| 48 |
-
# Step 2:
|
| 49 |
if not cleaned_output.endswith("]}"):
|
|
|
|
| 50 |
if cleaned_output.endswith("]"):
|
| 51 |
-
cleaned_output += "}" # Close the
|
| 52 |
elif cleaned_output.endswith("}"):
|
| 53 |
-
cleaned_output = cleaned_output[:-1] + "]}" # Close the
|
| 54 |
else:
|
| 55 |
cleaned_output += "]}" # Append both array and object closures
|
| 56 |
|
|
@@ -72,12 +73,14 @@ def clean_and_format_output(output):
|
|
| 72 |
return json_output
|
| 73 |
|
| 74 |
except ValueError as e:
|
|
|
|
| 75 |
return {
|
| 76 |
"error": "Failed to parse or validate output as JSON",
|
| 77 |
"details": str(e),
|
| 78 |
"output": cleaned_output
|
| 79 |
}
|
| 80 |
|
|
|
|
| 81 |
# Function to generate learning content
|
| 82 |
def generate_learning_content(topic, description, difficulty, temperature=0.9, max_new_tokens=2000, top_p=0.95, repetition_penalty=1.2):
|
| 83 |
"""
|
|
|
|
| 34 |
def clean_and_format_output(output):
|
| 35 |
"""
|
| 36 |
Cleans, validates, and attempts to auto-repair JSON output from the AI model.
|
| 37 |
+
Handles truncated, malformed JSON and missing delimiters.
|
| 38 |
"""
|
| 39 |
try:
|
| 40 |
# Step 1: Clean the raw output
|
|
|
|
| 45 |
cleaned_output = re.sub(r'\s+', ' ', cleaned_output).strip() # Normalize whitespace
|
| 46 |
cleaned_output = re.sub(r',\s*(\}|\])', r'\1', cleaned_output) # Remove trailing commas
|
| 47 |
|
| 48 |
+
# Step 2: Attempt to auto-repair JSON
|
| 49 |
if not cleaned_output.endswith("]}"):
|
| 50 |
+
# Close the sections array and root object if needed
|
| 51 |
if cleaned_output.endswith("]"):
|
| 52 |
+
cleaned_output += "}" # Close the root object
|
| 53 |
elif cleaned_output.endswith("}"):
|
| 54 |
+
cleaned_output = cleaned_output[:-1] + "]}" # Close the array and root object
|
| 55 |
else:
|
| 56 |
cleaned_output += "]}" # Append both array and object closures
|
| 57 |
|
|
|
|
| 73 |
return json_output
|
| 74 |
|
| 75 |
except ValueError as e:
|
| 76 |
+
# Return detailed error information
|
| 77 |
return {
|
| 78 |
"error": "Failed to parse or validate output as JSON",
|
| 79 |
"details": str(e),
|
| 80 |
"output": cleaned_output
|
| 81 |
}
|
| 82 |
|
| 83 |
+
|
| 84 |
# Function to generate learning content
|
| 85 |
def generate_learning_content(topic, description, difficulty, temperature=0.9, max_new_tokens=2000, top_p=0.95, repetition_penalty=1.2):
|
| 86 |
"""
|