ziyadsuper2017 commited on
Commit
e2b249d
·
verified ·
1 Parent(s): 2f7d8d4

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +129 -0
main.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"
7
+ genai.configure(api_key=api_key)
8
+
9
+ # Configure the Gemini model
10
+ 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
+
18
+ # Streamlit UI
19
+ st.title("Lecture Notes Mindmap Generator")
20
+
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
+ # 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
+ """)