ziyadsuper2017 commited on
Commit
9321000
·
verified ·
1 Parent(s): 4663d01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -19
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  import google.generativeai as genai
3
  import json
 
4
 
5
  # Set up Gemini API
6
  api_key = "AIzaSyBbusXSbq7JTI8Sa7vjGqu-h2zluKhDpX8"
@@ -11,7 +12,7 @@ model = genai.GenerativeModel(
11
  model_name="gemini-1.5-pro-latest",
12
  generation_config=genai.GenerationConfig(
13
  temperature=0.2,
14
- max_output_tokens=8192
15
  )
16
  )
17
 
@@ -21,23 +22,50 @@ st.title("Lecture Notes Mindmap Generator")
21
  # Text area for input
22
  lecture_notes = st.text_area("Paste your lecture notes here:", height=300)
23
 
24
- # Function to generate mindmap data
25
- def generate_mindmap(notes):
26
  prompt = f"""
27
- Create a hierarchical mindmap structure from the following lecture notes.
28
  The structure should be in JSON format, with each node having a 'name' and optional 'children' array.
29
- Ensure all content from the notes is included in the mindmap.
30
- Here are the lecture notes:
31
-
 
 
 
 
 
32
  {notes}
33
-
34
  Return only the JSON structure, without any additional text or explanation.
 
35
  """
36
 
37
- response = model.generate_content(prompt)
38
- return response.text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- # Function to display mindmap
41
  def display_mindmap(data, indent=0):
42
  if isinstance(data, str):
43
  try:
@@ -45,7 +73,7 @@ def display_mindmap(data, indent=0):
45
  except json.JSONDecodeError:
46
  st.error("Failed to parse the generated mindmap data. Please try again.")
47
  return
48
-
49
  if isinstance(data, dict):
50
  st.markdown(' ' * indent + f"- **{data['name']}**")
51
  if 'children' in data and isinstance(data['children'], list):
@@ -58,12 +86,28 @@ def display_mindmap(data, indent=0):
58
  # Button to generate mindmap
59
  if st.button("Generate Mindmap"):
60
  if lecture_notes:
61
- with st.spinner("Generating mindmap..."):
62
- try:
63
- mindmap_data = generate_mindmap(lecture_notes)
64
- st.session_state.mindmap_data = mindmap_data
65
- except Exception as e:
66
- st.error(f"An error occurred while generating the mindmap: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  else:
68
  st.warning("Please enter your lecture notes first.")
69
 
@@ -76,7 +120,9 @@ st.write("""
76
  To use this app:
77
  1. Paste your lecture notes into the text area.
78
  2. Click the "Generate Mindmap" button.
79
- 3. The generated mindmap will be displayed below.
 
80
 
81
  This app uses Google's Gemini AI to structure your notes into a hierarchical mindmap format.
 
82
  """)
 
1
  import streamlit as st
2
  import google.generativeai as genai
3
  import json
4
+ import time
5
 
6
  # Set up Gemini API
7
  api_key = "AIzaSyBbusXSbq7JTI8Sa7vjGqu-h2zluKhDpX8"
 
12
  model_name="gemini-1.5-pro-latest",
13
  generation_config=genai.GenerationConfig(
14
  temperature=0.2,
15
+ max_output_tokens=2048
16
  )
17
  )
18
 
 
22
  # Text area for input
23
  lecture_notes = st.text_area("Paste your lecture notes here:", height=300)
24
 
25
+ def generate_mindmap_step(notes, current_mindmap=None):
 
26
  prompt = f"""
27
+ Create or extend a hierarchical mindmap structure from the following lecture notes.
28
  The structure should be in JSON format, with each node having a 'name' and optional 'children' array.
29
+
30
+ If a current mindmap is provided, extend it. Otherwise, start a new one.
31
+ Focus on creating a comprehensive structure, but limit your response to about 1500 tokens.
32
+
33
+ Current mindmap (if any):
34
+ {json.dumps(current_mindmap) if current_mindmap else "None"}
35
+
36
+ Lecture notes:
37
  {notes}
38
+
39
  Return only the JSON structure, without any additional text or explanation.
40
+ If you haven't covered all the content, include a node named "MORE_CONTENT_PENDING" at the end.
41
  """
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)}")
49
+ return None, False
50
+
51
+ def merge_mindmaps(base, extension):
52
+ if not base:
53
+ return extension
54
+
55
+ def merge_nodes(node1, node2):
56
+ if 'children' not in node1:
57
+ node1['children'] = []
58
+ if 'children' in node2:
59
+ for child in node2['children']:
60
+ existing = next((n for n in node1['children'] if n['name'] == child['name']), None)
61
+ if existing:
62
+ merge_nodes(existing, child)
63
+ else:
64
+ node1['children'].append(child)
65
+
66
+ merge_nodes(base, extension)
67
+ return base
68
 
 
69
  def display_mindmap(data, indent=0):
70
  if isinstance(data, str):
71
  try:
 
73
  except json.JSONDecodeError:
74
  st.error("Failed to parse the generated mindmap data. Please try again.")
75
  return
76
+
77
  if isinstance(data, dict):
78
  st.markdown(' ' * indent + f"- **{data['name']}**")
79
  if 'children' in data and isinstance(data['children'], list):
 
86
  # Button to generate mindmap
87
  if st.button("Generate Mindmap"):
88
  if lecture_notes:
89
+ with st.spinner("Generating mindmap... This may take a few minutes."):
90
+ full_mindmap = None
91
+ more_content = True
92
+ iteration = 0
93
+
94
+ while more_content and iteration < 10: # Limit to 10 iterations as a safeguard
95
+ iteration += 1
96
+ st.text(f"Processing iteration {iteration}...")
97
+
98
+ step_mindmap, more_content = generate_mindmap_step(lecture_notes, full_mindmap)
99
+
100
+ if step_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)
108
+ st.success("Mindmap generated successfully!")
109
+ else:
110
+ st.error("Failed to generate the complete mindmap. Please try again.")
111
  else:
112
  st.warning("Please enter your lecture notes first.")
113
 
 
120
  To use this app:
121
  1. Paste your lecture notes into the text area.
122
  2. Click the "Generate Mindmap" button.
123
+ 3. Wait for the mindmap to be generated (this may take a few minutes).
124
+ 4. The generated mindmap will be displayed below.
125
 
126
  This app uses Google's Gemini AI to structure your notes into a hierarchical mindmap format.
127
+ It processes the notes in multiple steps to handle large inputs and complex structures.
128
  """)