ziyadsuper2017 commited on
Commit
8a96712
·
verified ·
1 Parent(s): 9321000

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -2
app.py CHANGED
@@ -42,7 +42,25 @@ def generate_mindmap_step(notes, current_mindmap=None):
42
 
43
  try:
44
  response = model.generate_content(prompt)
45
- mindmap_step = json.loads(response.text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  return mindmap_step, "MORE_CONTENT_PENDING" in str(mindmap_step)
47
  except Exception as e:
48
  st.error(f"An error occurred while generating the mindmap: {str(e)}")
@@ -101,7 +119,8 @@ if st.button("Generate Mindmap"):
101
  full_mindmap = merge_mindmaps(full_mindmap, step_mindmap)
102
  time.sleep(20) # Respect API rate limit
103
  else:
104
- break
 
105
 
106
  if full_mindmap:
107
  st.session_state.mindmap_data = json.dumps(full_mindmap)
 
42
 
43
  try:
44
  response = model.generate_content(prompt)
45
+ response_text = response.text.strip()
46
+
47
+ # Debug: Print raw response
48
+ st.text("Raw API Response:")
49
+ st.text(response_text)
50
+
51
+ # Attempt to parse JSON, handling potential leading/trailing characters
52
+ try:
53
+ mindmap_step = json.loads(response_text)
54
+ except json.JSONDecodeError:
55
+ # Try to find and extract JSON content
56
+ start = response_text.find('{')
57
+ end = response_text.rfind('}') + 1
58
+ if start != -1 and end != -1:
59
+ json_content = response_text[start:end]
60
+ mindmap_step = json.loads(json_content)
61
+ else:
62
+ raise ValueError("Unable to extract valid JSON from the response")
63
+
64
  return mindmap_step, "MORE_CONTENT_PENDING" in str(mindmap_step)
65
  except Exception as e:
66
  st.error(f"An error occurred while generating the mindmap: {str(e)}")
 
119
  full_mindmap = merge_mindmaps(full_mindmap, step_mindmap)
120
  time.sleep(20) # Respect API rate limit
121
  else:
122
+ st.warning(f"Iteration {iteration} failed to generate a valid mindmap step. Continuing to next iteration.")
123
+ continue
124
 
125
  if full_mindmap:
126
  st.session_state.mindmap_data = json.dumps(full_mindmap)