Spaces:
Sleeping
Sleeping
Upload educational_content_visualization.py
Browse files
educational_content_visualization.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from graphviz import Digraph
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
genai.configure(api_key=("AIzaSyDziGvuT1woHnH4_S3L_zQZV55Yj-123A8"))
|
| 10 |
+
|
| 11 |
+
# gemini function for generating mind maps and diagrams
|
| 12 |
+
def get_gemini_response(input):
|
| 13 |
+
model = genai.GenerativeModel('gemini-pro')
|
| 14 |
+
response = model.generate_content(input)
|
| 15 |
+
return response
|
| 16 |
+
|
| 17 |
+
# Function to generate mind map from content
|
| 18 |
+
def generate_mind_map(content):
|
| 19 |
+
mind_map_prompt = f"""
|
| 20 |
+
### Generate a mind map from the following educational content:
|
| 21 |
+
### Content:
|
| 22 |
+
{content}
|
| 23 |
+
### Mind Map:
|
| 24 |
+
"""
|
| 25 |
+
response = get_gemini_response(mind_map_prompt)
|
| 26 |
+
return response
|
| 27 |
+
|
| 28 |
+
# Function to parse mind map data
|
| 29 |
+
def parse_mind_map_data(data):
|
| 30 |
+
lines = data.split('\n')
|
| 31 |
+
mind_map_structure = {}
|
| 32 |
+
current_parent = None
|
| 33 |
+
current_sub_parent = None
|
| 34 |
+
|
| 35 |
+
for line in lines:
|
| 36 |
+
stripped_line = line.strip()
|
| 37 |
+
if stripped_line.startswith('**') and stripped_line.endswith('**'):
|
| 38 |
+
current_parent = stripped_line.strip('**').strip()
|
| 39 |
+
mind_map_structure[current_parent] = {}
|
| 40 |
+
elif stripped_line.startswith('- '):
|
| 41 |
+
current_sub_parent = stripped_line.strip('- ').strip()
|
| 42 |
+
if current_parent:
|
| 43 |
+
mind_map_structure[current_parent][current_sub_parent] = []
|
| 44 |
+
elif stripped_line.startswith(' - '):
|
| 45 |
+
sub_child = stripped_line.strip(' - ').strip()
|
| 46 |
+
if current_parent and current_sub_parent:
|
| 47 |
+
mind_map_structure[current_parent][current_sub_parent].append(sub_child)
|
| 48 |
+
|
| 49 |
+
return mind_map_structure
|
| 50 |
+
|
| 51 |
+
# Function to display mind map using Graphviz
|
| 52 |
+
def display_mind_map(mind_map_response):
|
| 53 |
+
st.subheader("Generated Mind Map")
|
| 54 |
+
if mind_map_response and mind_map_response.candidates:
|
| 55 |
+
mind_map_data = mind_map_response.candidates[0].content.parts[0].text if mind_map_response.candidates[0].content.parts else ""
|
| 56 |
+
st.write("Mind Map Data:", mind_map_data) # Debugging output
|
| 57 |
+
|
| 58 |
+
# Parse the mind map data into a hierarchical structure
|
| 59 |
+
mind_map_structure = parse_mind_map_data(mind_map_data)
|
| 60 |
+
|
| 61 |
+
# Initialize a new Digraph
|
| 62 |
+
dot = Digraph()
|
| 63 |
+
|
| 64 |
+
# Add nodes and edges to the graph based on the structure
|
| 65 |
+
for parent, children in mind_map_structure.items():
|
| 66 |
+
dot.node(parent, parent)
|
| 67 |
+
for child, sub_children in children.items():
|
| 68 |
+
dot.node(child, child)
|
| 69 |
+
dot.edge(parent, child)
|
| 70 |
+
for sub_child in sub_children:
|
| 71 |
+
dot.node(sub_child, sub_child)
|
| 72 |
+
dot.edge(child, sub_child)
|
| 73 |
+
|
| 74 |
+
# Render the graph
|
| 75 |
+
st.graphviz_chart(dot.source)
|
| 76 |
+
else:
|
| 77 |
+
st.write("No response received from the model.")
|
| 78 |
+
|
| 79 |
+
## Streamlit App
|
| 80 |
+
st.title("AI-Powered Educational Content Visualization")
|
| 81 |
+
st.text("Paste your engineering educational content to generate a mind map and diagram.")
|
| 82 |
+
|
| 83 |
+
content_input = st.text_area("Paste your educational content here", height=300)
|
| 84 |
+
submit_content = st.button('Generate Mind Map and Diagram')
|
| 85 |
+
|
| 86 |
+
if submit_content:
|
| 87 |
+
if content_input:
|
| 88 |
+
mind_map_response = generate_mind_map(content_input)
|
| 89 |
+
|
| 90 |
+
# Display the generated mind map
|
| 91 |
+
display_mind_map(mind_map_response)
|