abhiimanyu commited on
Commit
f154ffe
·
verified ·
1 Parent(s): 0d0a2a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -5
app.py CHANGED
@@ -55,10 +55,13 @@ def clean_and_format_learning_content(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,10 +71,10 @@ def clean_and_format_learning_content(output):
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,7 +94,6 @@ def clean_and_format_learning_content(output):
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
  """
 
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: Remove invalid syntax in 'content' fields
59
+ cleaned_output = re.sub(r'\[.*?\]\(.*?\)', '', cleaned_output) # Remove Markdown-like links
60
+
61
+ # Step 4: Fix missing commas between JSON objects
62
  cleaned_output = re.sub(r'(\})(\s*{)', r'\1,\2', cleaned_output)
63
 
64
+ # Step 5: Fix unbalanced brackets and braces
65
  open_braces = cleaned_output.count('{')
66
  close_braces = cleaned_output.count('}')
67
  open_brackets = cleaned_output.count('[')
 
71
  if open_brackets > close_brackets:
72
  cleaned_output += ']' * (open_brackets - close_brackets)
73
 
74
+ # Step 6: Attempt to parse JSON
75
  json_output = json.loads(cleaned_output)
76
 
77
+ # Step 7: Validate JSON structure
78
  required_keys = ["title", "sections"]
79
  if "title" not in json_output or "sections" not in json_output:
80
  raise ValueError("Missing required keys: 'title' or 'sections'.")
 
94
  "output": cleaned_output
95
  }
96
 
 
97
  # Function to generate learning content
98
  def generate_learning_content(topic, description, difficulty, temperature=0.9, max_new_tokens=2000, top_p=0.95, repetition_penalty=1.2):
99
  """