ziyadsuper2017 commited on
Commit
5be0b07
·
verified ·
1 Parent(s): d497ada

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -77
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
2
  import google.generativeai as genai
3
- from streamlit_react import ReactComponent
4
 
5
  # Set up Gemini API
6
  api_key = "AIzaSyAHD0FwX-Ds6Y3eI-i5Oz7IdbJqR6rN7pg"
@@ -37,93 +37,46 @@ def generate_mindmap(notes):
37
  response = model.generate_content(prompt)
38
  return response.text
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  # Button to generate mindmap
41
  if st.button("Generate Mindmap"):
42
  if lecture_notes:
43
  with st.spinner("Generating mindmap..."):
44
- mindmap_data = generate_mindmap(lecture_notes)
45
- st.session_state.mindmap_data = mindmap_data
 
 
 
46
  else:
47
  st.warning("Please enter your lecture notes first.")
48
 
49
  # Display the mindmap if data is available
50
  if 'mindmap_data' in st.session_state:
51
  st.subheader("Generated Mindmap")
52
-
53
- # React component for mindmap visualization
54
- ReactComponent(
55
- component_name="MindMap",
56
- props={"data": st.session_state.mindmap_data},
57
- key="mindmap_viewer"
58
- )
59
-
60
- # React component definition (you'll need to save this in a separate file)
61
- st.write("""
62
- To use this app, you'll need to create a file named `mindmap_component.jsx` in your Streamlit app's directory with the following content:
63
-
64
- ```jsx
65
- import React, { useState } from 'react';
66
- import { ChevronRight, ChevronDown } from 'lucide-react';
67
-
68
- const TreeNode = ({ node }) => {
69
- const [isOpen, setIsOpen] = useState(false);
70
- const hasChildren = node.children && node.children.length > 0;
71
-
72
- return (
73
- <div className="ml-4">
74
- <div
75
- className="flex items-center cursor-pointer hover:bg-gray-100 p-2 rounded"
76
- onClick={() => setIsOpen(!isOpen)}
77
- >
78
- {hasChildren && (
79
- <span className="mr-2">
80
- {isOpen ? <ChevronDown size={16} /> : <ChevronRight size={16} />}
81
- </span>
82
- )}
83
- <span className={`${hasChildren ? 'font-semibold' : ''}`}>{node.name}</span>
84
- </div>
85
- {isOpen && hasChildren && (
86
- <div className="ml-4 border-l-2 border-gray-200">
87
- {node.children.map((child, index) => (
88
- <TreeNode key={index} node={child} />
89
- ))}
90
- </div>
91
- )}
92
- </div>
93
- );
94
- };
95
-
96
- const MindMap = ({ data }) => {
97
- const parsedData = typeof data === 'string' ? JSON.parse(data) : data;
98
-
99
- return (
100
- <div className="p-4 bg-white rounded-lg shadow-lg max-w-4xl mx-auto overflow-auto max-h-[600px]">
101
- <h2 className="text-2xl font-bold mb-4 text-center">Lecture Notes Mindmap</h2>
102
- <TreeNode node={parsedData} />
103
- </div>
104
- );
105
- };
106
-
107
- export default MindMap;
108
- ```
109
- """)
110
 
111
  st.write("""
112
- To set up this Streamlit app with the React component:
113
-
114
- 1. Install required packages:
115
- ```
116
- pip install streamlit google-generativeai streamlit-react
117
- ```
118
-
119
- 2. Save the main Python code in a file named `app.py`.
120
-
121
- 3. Create a file named `mindmap_component.jsx` with the React component code provided above.
122
-
123
- 4. Run the Streamlit app:
124
- ```
125
- streamlit run app.py
126
- ```
127
 
128
- This setup will allow you to paste your lecture notes, generate a mindmap using Gemini, and visualize it using the React component.
129
  """)
 
1
  import streamlit as st
2
  import google.generativeai as genai
3
+ import json
4
 
5
  # Set up Gemini API
6
  api_key = "AIzaSyAHD0FwX-Ds6Y3eI-i5Oz7IdbJqR6rN7pg"
 
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:
44
+ data = json.loads(data)
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):
52
+ for child in data['children']:
53
+ display_mindmap(child, indent + 1)
54
+ elif isinstance(data, list):
55
+ for item in data:
56
+ display_mindmap(item, indent)
57
+
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
 
70
  # Display the mindmap if data is available
71
  if 'mindmap_data' in st.session_state:
72
  st.subheader("Generated Mindmap")
73
+ display_mindmap(st.session_state.mindmap_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  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
  """)