Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,10 @@ 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 = "
|
| 8 |
genai.configure(api_key=api_key)
|
| 9 |
|
| 10 |
# Configure the Gemini model
|
|
@@ -17,6 +18,7 @@ model = genai.GenerativeModel(
|
|
| 17 |
)
|
| 18 |
|
| 19 |
# Streamlit UI
|
|
|
|
| 20 |
st.title("Lecture Notes Mindmap Generator")
|
| 21 |
|
| 22 |
# Initialize session state variables
|
|
@@ -36,8 +38,8 @@ def chunk_notes(notes, chunk_size=500):
|
|
| 36 |
|
| 37 |
def generate_mindmap_step(notes_chunk, current_mindmap=None):
|
| 38 |
prompt = f"""
|
| 39 |
-
|
| 40 |
-
The structure should be in JSON format, with each node having a 'name' and
|
| 41 |
|
| 42 |
Current mindmap:
|
| 43 |
{json.dumps(current_mindmap) if current_mindmap else "{}"}
|
|
@@ -46,12 +48,12 @@ def generate_mindmap_step(notes_chunk, current_mindmap=None):
|
|
| 46 |
{notes_chunk}
|
| 47 |
|
| 48 |
Follow these guidelines to create a well-structured mindmap:
|
| 49 |
-
1. Create clear,
|
| 50 |
-
2.
|
| 51 |
-
3.
|
| 52 |
-
4.
|
| 53 |
-
5.
|
| 54 |
-
6.
|
| 55 |
|
| 56 |
Return only the JSON structure of the entire updated mindmap, without any additional text or explanation.
|
| 57 |
Ensure the JSON is valid and complete.
|
|
@@ -82,23 +84,39 @@ def generate_mindmap_step(notes_chunk, current_mindmap=None):
|
|
| 82 |
def display_mindmap(data, indent=0):
|
| 83 |
if isinstance(data, dict):
|
| 84 |
if indent == 0:
|
| 85 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
prefix = " " * (indent - 1) + ("└─ " if indent > 0 else "")
|
| 88 |
st.markdown(f"{prefix}**{data['name']}**")
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
for i, child in enumerate(data['children']):
|
| 92 |
-
if i == len(data['children']) - 1:
|
| 93 |
-
display_mindmap(child, indent + 1)
|
| 94 |
-
else:
|
| 95 |
display_mindmap(child, indent + 1)
|
| 96 |
-
if indent == 0:
|
| 97 |
-
st.markdown("---") # Add a horizontal line between main topics
|
| 98 |
elif isinstance(data, list):
|
| 99 |
for item in data:
|
| 100 |
display_mindmap(item, indent)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# Button to generate or continue mindmap
|
| 103 |
if st.button("Generate/Continue Mindmap"):
|
| 104 |
if lecture_notes:
|
|
@@ -136,6 +154,7 @@ if st.button("Clear and Restart"):
|
|
| 136 |
# Display the current state of the mindmap
|
| 137 |
if st.session_state.current_mindmap:
|
| 138 |
st.subheader("Current Mindmap")
|
|
|
|
| 139 |
display_mindmap(st.session_state.current_mindmap)
|
| 140 |
|
| 141 |
# Add an option to download the mindmap as JSON
|
|
@@ -156,6 +175,7 @@ To use this app:
|
|
| 156 |
4. If the process is interrupted, you can continue from where it left off.
|
| 157 |
5. Use the "Clear and Restart" button to start over with new notes.
|
| 158 |
6. You can download the generated mindmap as a JSON file for future reference.
|
|
|
|
| 159 |
|
| 160 |
This app uses Google's Gemini AI to structure your notes into a hierarchical mindmap format.
|
| 161 |
It processes the notes in multiple steps to handle large inputs and complex structures.
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import json
|
| 4 |
import time
|
| 5 |
+
from streamlit.components.v1 import html
|
| 6 |
|
| 7 |
# Set up Gemini API
|
| 8 |
+
api_key = "YOUR_API_KEY_HERE" # Replace with your actual API key
|
| 9 |
genai.configure(api_key=api_key)
|
| 10 |
|
| 11 |
# Configure the Gemini model
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
# Streamlit UI
|
| 21 |
+
st.set_page_config(page_title="Lecture Notes Mindmap Generator", layout="wide")
|
| 22 |
st.title("Lecture Notes Mindmap Generator")
|
| 23 |
|
| 24 |
# Initialize session state variables
|
|
|
|
| 38 |
|
| 39 |
def generate_mindmap_step(notes_chunk, current_mindmap=None):
|
| 40 |
prompt = f"""
|
| 41 |
+
Create or extend a hierarchical mindmap structure based on the following lecture notes.
|
| 42 |
+
The structure should be in JSON format, with each node having a 'name' and 'children' array.
|
| 43 |
|
| 44 |
Current mindmap:
|
| 45 |
{json.dumps(current_mindmap) if current_mindmap else "{}"}
|
|
|
|
| 48 |
{notes_chunk}
|
| 49 |
|
| 50 |
Follow these guidelines to create a well-structured mindmap:
|
| 51 |
+
1. Create a clear hierarchy with main topics, subtopics, and details.
|
| 52 |
+
2. Use concise, descriptive names for each node.
|
| 53 |
+
3. Group related concepts under appropriate parent nodes.
|
| 54 |
+
4. Limit the number of main topics to 3-7 for better organization.
|
| 55 |
+
5. Ensure consistent depth across similar levels of information.
|
| 56 |
+
6. Use parallel structure in naming sibling nodes.
|
| 57 |
|
| 58 |
Return only the JSON structure of the entire updated mindmap, without any additional text or explanation.
|
| 59 |
Ensure the JSON is valid and complete.
|
|
|
|
| 84 |
def display_mindmap(data, indent=0):
|
| 85 |
if isinstance(data, dict):
|
| 86 |
if indent == 0:
|
| 87 |
+
with st.expander(data['name'], expanded=True):
|
| 88 |
+
if 'children' in data and isinstance(data['children'], list):
|
| 89 |
+
for child in data['children']:
|
| 90 |
+
display_mindmap(child, indent + 1)
|
| 91 |
else:
|
| 92 |
prefix = " " * (indent - 1) + ("└─ " if indent > 0 else "")
|
| 93 |
st.markdown(f"{prefix}**{data['name']}**")
|
| 94 |
+
if 'children' in data and isinstance(data['children'], list):
|
| 95 |
+
for child in data['children']:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
display_mindmap(child, indent + 1)
|
|
|
|
|
|
|
| 97 |
elif isinstance(data, list):
|
| 98 |
for item in data:
|
| 99 |
display_mindmap(item, indent)
|
| 100 |
|
| 101 |
+
def add_search_functionality():
|
| 102 |
+
html("""
|
| 103 |
+
<script>
|
| 104 |
+
function searchMindmap() {
|
| 105 |
+
var input = document.getElementById('searchInput').value.toLowerCase();
|
| 106 |
+
var elements = document.getElementsByTagName('*');
|
| 107 |
+
for (var i = 0; i < elements.length; i++) {
|
| 108 |
+
var element = elements[i];
|
| 109 |
+
if (element.textContent.toLowerCase().includes(input)) {
|
| 110 |
+
element.style.backgroundColor = 'yellow';
|
| 111 |
+
} else {
|
| 112 |
+
element.style.backgroundColor = '';
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
</script>
|
| 117 |
+
<input type="text" id="searchInput" onkeyup="searchMindmap()" placeholder="Search mindmap...">
|
| 118 |
+
""")
|
| 119 |
+
|
| 120 |
# Button to generate or continue mindmap
|
| 121 |
if st.button("Generate/Continue Mindmap"):
|
| 122 |
if lecture_notes:
|
|
|
|
| 154 |
# Display the current state of the mindmap
|
| 155 |
if st.session_state.current_mindmap:
|
| 156 |
st.subheader("Current Mindmap")
|
| 157 |
+
add_search_functionality()
|
| 158 |
display_mindmap(st.session_state.current_mindmap)
|
| 159 |
|
| 160 |
# Add an option to download the mindmap as JSON
|
|
|
|
| 175 |
4. If the process is interrupted, you can continue from where it left off.
|
| 176 |
5. Use the "Clear and Restart" button to start over with new notes.
|
| 177 |
6. You can download the generated mindmap as a JSON file for future reference.
|
| 178 |
+
7. Use the search box to quickly find topics in the mindmap.
|
| 179 |
|
| 180 |
This app uses Google's Gemini AI to structure your notes into a hierarchical mindmap format.
|
| 181 |
It processes the notes in multiple steps to handle large inputs and complex structures.
|