Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,16 +4,16 @@ import json
|
|
| 4 |
import time
|
| 5 |
from streamlit.components.v1 import html
|
| 6 |
|
| 7 |
-
# Set up Gemini API
|
| 8 |
-
api_key = "AIzaSyBbusXSbq7JTI8Sa7vjGqu-h2zluKhDpX8"
|
| 9 |
genai.configure(api_key=api_key)
|
| 10 |
|
| 11 |
-
# Configure the Gemini model
|
| 12 |
model = genai.GenerativeModel(
|
| 13 |
model_name="gemini-1.5-pro-latest",
|
| 14 |
generation_config=genai.GenerationConfig(
|
| 15 |
-
temperature=0.2,
|
| 16 |
-
max_output_tokens=2048
|
| 17 |
)
|
| 18 |
)
|
| 19 |
|
|
@@ -21,114 +21,82 @@ model = genai.GenerativeModel(
|
|
| 21 |
st.set_page_config(page_title="Lecture Notes Mindmap Generator", layout="wide")
|
| 22 |
st.title("Lecture Notes Mindmap Generator")
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
if
|
| 26 |
st.session_state.lecture_notes = ""
|
| 27 |
-
if
|
| 28 |
st.session_state.current_mindmap = None
|
| 29 |
-
if
|
| 30 |
st.session_state.processed_chunks = 0
|
| 31 |
|
| 32 |
-
#
|
| 33 |
lecture_notes = st.text_area("Paste your lecture notes here:", height=300, value=st.session_state.lecture_notes)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 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 "{}"}
|
| 46 |
|
| 47 |
New lecture notes chunk:
|
| 48 |
{notes_chunk}
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
1.
|
| 52 |
-
2. Use concise,
|
| 53 |
-
3.
|
| 54 |
-
4.
|
| 55 |
-
5.
|
| 56 |
-
6.
|
|
|
|
| 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.
|
| 60 |
"""
|
| 61 |
-
|
| 62 |
try:
|
| 63 |
response = model.generate_content(prompt)
|
| 64 |
response_text = response.text.strip()
|
| 65 |
-
|
| 66 |
-
#
|
| 67 |
try:
|
| 68 |
-
|
| 69 |
-
except json.JSONDecodeError:
|
| 70 |
-
|
| 71 |
-
start = response_text.find('{')
|
| 72 |
-
end = response_text.rfind('}') + 1
|
| 73 |
-
if start != -1 and end != -1:
|
| 74 |
-
json_content = response_text[start:end]
|
| 75 |
-
mindmap_step = json.loads(json_content)
|
| 76 |
-
else:
|
| 77 |
-
raise ValueError("Unable to extract valid JSON from the response")
|
| 78 |
-
|
| 79 |
-
return mindmap_step
|
| 80 |
except Exception as e:
|
| 81 |
-
st.error(f"An error occurred
|
| 82 |
-
return None
|
| 83 |
|
|
|
|
| 84 |
def display_mindmap(data, indent=0):
|
| 85 |
if isinstance(data, dict):
|
| 86 |
-
if indent == 0:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 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:
|
| 123 |
st.session_state.lecture_notes = lecture_notes
|
| 124 |
chunks = chunk_notes(lecture_notes)
|
| 125 |
-
|
| 126 |
with st.spinner("Generating mindmap... This may take a few minutes."):
|
| 127 |
for i in range(st.session_state.processed_chunks, len(chunks)):
|
| 128 |
st.text(f"Processing chunk {i+1} of {len(chunks)}...")
|
| 129 |
-
|
| 130 |
step_mindmap = generate_mindmap_step(chunks[i], st.session_state.current_mindmap)
|
| 131 |
-
|
| 132 |
if step_mindmap:
|
| 133 |
st.session_state.current_mindmap = step_mindmap
|
| 134 |
st.session_state.processed_chunks = i + 1
|
|
@@ -136,7 +104,7 @@ if st.button("Generate/Continue Mindmap"):
|
|
| 136 |
else:
|
| 137 |
st.warning(f"Failed to process chunk {i+1}. You can try continuing from this point.")
|
| 138 |
break
|
| 139 |
-
|
| 140 |
if st.session_state.current_mindmap:
|
| 141 |
st.success(f"Mindmap generated successfully! Processed {st.session_state.processed_chunks} out of {len(chunks)} chunks.")
|
| 142 |
else:
|
|
@@ -144,19 +112,18 @@ if st.button("Generate/Continue Mindmap"):
|
|
| 144 |
else:
|
| 145 |
st.warning("Please enter your lecture notes first.")
|
| 146 |
|
| 147 |
-
#
|
| 148 |
if st.button("Clear and Restart"):
|
| 149 |
st.session_state.lecture_notes = ""
|
| 150 |
st.session_state.current_mindmap = None
|
| 151 |
st.session_state.processed_chunks = 0
|
| 152 |
st.success("Cleared all data. You can start a new mindmap generation.")
|
| 153 |
|
| 154 |
-
# Display
|
| 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
|
| 161 |
if st.button("Download Mindmap as JSON"):
|
| 162 |
json_string = json.dumps(st.session_state.current_mindmap, indent=2)
|
|
@@ -175,8 +142,4 @@ To use this app:
|
|
| 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 |
-
|
| 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.
|
| 182 |
-
""")
|
|
|
|
| 4 |
import time
|
| 5 |
from streamlit.components.v1 import html
|
| 6 |
|
| 7 |
+
# Set up Gemini API (using your provided key)
|
| 8 |
+
api_key = "AIzaSyBbusXSbq7JTI8Sa7vjGqu-h2zluKhDpX8"
|
| 9 |
genai.configure(api_key=api_key)
|
| 10 |
|
| 11 |
+
# Configure the Gemini model (as you specified)
|
| 12 |
model = genai.GenerativeModel(
|
| 13 |
model_name="gemini-1.5-pro-latest",
|
| 14 |
generation_config=genai.GenerationConfig(
|
| 15 |
+
temperature=0.2,
|
| 16 |
+
max_output_tokens=2048
|
| 17 |
)
|
| 18 |
)
|
| 19 |
|
|
|
|
| 21 |
st.set_page_config(page_title="Lecture Notes Mindmap Generator", layout="wide")
|
| 22 |
st.title("Lecture Notes Mindmap Generator")
|
| 23 |
|
| 24 |
+
# Session State
|
| 25 |
+
if "lecture_notes" not in st.session_state:
|
| 26 |
st.session_state.lecture_notes = ""
|
| 27 |
+
if "current_mindmap" not in st.session_state:
|
| 28 |
st.session_state.current_mindmap = None
|
| 29 |
+
if "processed_chunks" not in st.session_state:
|
| 30 |
st.session_state.processed_chunks = 0
|
| 31 |
|
| 32 |
+
# Lecture Notes Input
|
| 33 |
lecture_notes = st.text_area("Paste your lecture notes here:", height=300, value=st.session_state.lecture_notes)
|
| 34 |
|
| 35 |
+
# Chunking (with explanation)
|
| 36 |
+
def chunk_notes(notes, chunk_size=800):
|
| 37 |
+
"""Breaks down lecture notes into smaller chunks for efficient processing."""
|
| 38 |
+
return [notes[i:i+chunk_size] for i in range(0, len(notes), chunk_size)]
|
| 39 |
|
| 40 |
+
# Mindmap Generation Step (with detailed instructions)
|
| 41 |
def generate_mindmap_step(notes_chunk, current_mindmap=None):
|
| 42 |
prompt = f"""
|
| 43 |
+
Create or extend a hierarchical mindmap structure based on the following lecture notes.
|
| 44 |
The structure should be in JSON format, with each node having a 'name' and 'children' array.
|
| 45 |
|
| 46 |
+
Current mindmap (if any):
|
| 47 |
{json.dumps(current_mindmap) if current_mindmap else "{}"}
|
| 48 |
|
| 49 |
New lecture notes chunk:
|
| 50 |
{notes_chunk}
|
| 51 |
+
|
| 52 |
+
Specific formatting instructions:
|
| 53 |
+
1. Prioritize a clear, bulletin-style hierarchy (main topics, subtopics, supporting details).
|
| 54 |
+
2. Use concise, informative names for each node to capture the essence of the concept.
|
| 55 |
+
3. Ensure logical grouping: all children of a node should be directly related to its topic.
|
| 56 |
+
4. Aim for 3-5 main topics to maintain a good overview and prevent overwhelming the user.
|
| 57 |
+
5. Strive for consistency in depth and detail across similar levels of the mindmap.
|
| 58 |
+
6. If a concept appears multiple times, find a way to consolidate or cross-reference it.
|
| 59 |
+
7. **Important:** If you're unsure about the structure, err on the side of creating separate nodes for clarity.
|
| 60 |
|
| 61 |
Return only the JSON structure of the entire updated mindmap, without any additional text or explanation.
|
|
|
|
| 62 |
"""
|
| 63 |
+
|
| 64 |
try:
|
| 65 |
response = model.generate_content(prompt)
|
| 66 |
response_text = response.text.strip()
|
| 67 |
+
|
| 68 |
+
# Robust JSON Parsing
|
| 69 |
try:
|
| 70 |
+
return json.loads(response_text)
|
| 71 |
+
except json.JSONDecodeError as e:
|
| 72 |
+
st.error(f"Error parsing JSON: {e}. Raw response: {response_text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
+
st.error(f"An error occurred: {e}")
|
|
|
|
| 75 |
|
| 76 |
+
# Display Mindmap with Bulletin Style
|
| 77 |
def display_mindmap(data, indent=0):
|
| 78 |
if isinstance(data, dict):
|
| 79 |
+
if indent == 0: # Main topic
|
| 80 |
+
st.markdown(f"### {data['name']}")
|
| 81 |
+
else: # Subtopic or detail
|
| 82 |
+
st.markdown(f"{' ' * indent}- {data['name']}")
|
| 83 |
+
|
| 84 |
+
if 'children' in data and isinstance(data['children'], list):
|
| 85 |
+
for child in data['children']:
|
| 86 |
+
display_mindmap(child, indent + 1)
|
| 87 |
+
|
| 88 |
+
# Button Actions and UI Logic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
if st.button("Generate/Continue Mindmap"):
|
| 90 |
if lecture_notes:
|
| 91 |
st.session_state.lecture_notes = lecture_notes
|
| 92 |
chunks = chunk_notes(lecture_notes)
|
| 93 |
+
|
| 94 |
with st.spinner("Generating mindmap... This may take a few minutes."):
|
| 95 |
for i in range(st.session_state.processed_chunks, len(chunks)):
|
| 96 |
st.text(f"Processing chunk {i+1} of {len(chunks)}...")
|
| 97 |
+
|
| 98 |
step_mindmap = generate_mindmap_step(chunks[i], st.session_state.current_mindmap)
|
| 99 |
+
|
| 100 |
if step_mindmap:
|
| 101 |
st.session_state.current_mindmap = step_mindmap
|
| 102 |
st.session_state.processed_chunks = i + 1
|
|
|
|
| 104 |
else:
|
| 105 |
st.warning(f"Failed to process chunk {i+1}. You can try continuing from this point.")
|
| 106 |
break
|
| 107 |
+
|
| 108 |
if st.session_state.current_mindmap:
|
| 109 |
st.success(f"Mindmap generated successfully! Processed {st.session_state.processed_chunks} out of {len(chunks)} chunks.")
|
| 110 |
else:
|
|
|
|
| 112 |
else:
|
| 113 |
st.warning("Please enter your lecture notes first.")
|
| 114 |
|
| 115 |
+
# Clear and Restart
|
| 116 |
if st.button("Clear and Restart"):
|
| 117 |
st.session_state.lecture_notes = ""
|
| 118 |
st.session_state.current_mindmap = None
|
| 119 |
st.session_state.processed_chunks = 0
|
| 120 |
st.success("Cleared all data. You can start a new mindmap generation.")
|
| 121 |
|
| 122 |
+
# Display Mindmap
|
| 123 |
if st.session_state.current_mindmap:
|
| 124 |
st.subheader("Current Mindmap")
|
|
|
|
| 125 |
display_mindmap(st.session_state.current_mindmap)
|
| 126 |
+
|
| 127 |
# Add an option to download the mindmap as JSON
|
| 128 |
if st.button("Download Mindmap as JSON"):
|
| 129 |
json_string = json.dumps(st.session_state.current_mindmap, indent=2)
|
|
|
|
| 142 |
4. If the process is interrupted, you can continue from where it left off.
|
| 143 |
5. Use the "Clear and Restart" button to start over with new notes.
|
| 144 |
6. You can download the generated mindmap as a JSON file for future reference.
|
| 145 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|