Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import networkx as nx
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import openai
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Set OpenAI API key from Hugging Face secrets
|
| 8 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
+
|
| 10 |
+
st.set_page_config(page_title="AI Mind Map Generator", layout="wide")
|
| 11 |
+
st.title("🧠 AI Mind Map Generator")
|
| 12 |
+
st.write("Enter a topic and generate a mind map using GPT-3.5!")
|
| 13 |
+
|
| 14 |
+
topic = st.text_input("Enter a topic to generate mind map:", "")
|
| 15 |
+
|
| 16 |
+
def generate_mindmap(topic):
|
| 17 |
+
prompt = f"Create a simple, structured mind map for the topic '{topic}'. Use bullet points to show sub-topics and indentation for hierarchy."
|
| 18 |
+
response = openai.ChatCompletion.create(
|
| 19 |
+
model="gpt-3.5-turbo",
|
| 20 |
+
messages=[{"role": "user", "content": prompt}],
|
| 21 |
+
temperature=0.7
|
| 22 |
+
)
|
| 23 |
+
return response["choices"][0]["message"]["content"]
|
| 24 |
+
|
| 25 |
+
def build_graph_from_text(text):
|
| 26 |
+
G = nx.Graph()
|
| 27 |
+
parents = []
|
| 28 |
+
prev_indent = -1
|
| 29 |
+
|
| 30 |
+
for line in text.splitlines():
|
| 31 |
+
if not line.strip():
|
| 32 |
+
continue
|
| 33 |
+
|
| 34 |
+
indent = len(line) - len(line.lstrip(" "))
|
| 35 |
+
node = line.strip("-• ").strip()
|
| 36 |
+
|
| 37 |
+
if indent == 0:
|
| 38 |
+
parents = [node]
|
| 39 |
+
else:
|
| 40 |
+
level = indent // 2
|
| 41 |
+
if level < len(parents):
|
| 42 |
+
parents = parents[:level]
|
| 43 |
+
parents.append(node)
|
| 44 |
+
|
| 45 |
+
G.add_node(node)
|
| 46 |
+
if len(parents) > 1:
|
| 47 |
+
G.add_edge(parents[-2], node)
|
| 48 |
+
|
| 49 |
+
return G
|
| 50 |
+
|
| 51 |
+
if topic:
|
| 52 |
+
with st.spinner("Generating mind map..."):
|
| 53 |
+
mindmap_text = generate_mindmap(topic)
|
| 54 |
+
G = build_graph_from_text(mindmap_text)
|
| 55 |
+
|
| 56 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 57 |
+
pos = nx.spring_layout(G, seed=42)
|
| 58 |
+
nx.draw(G, pos, with_labels=True, node_color="skyblue", edge_color="gray", node_size=2000, font_size=10)
|
| 59 |
+
st.pyplot(fig)
|
| 60 |
+
|
| 61 |
+
with st.expander("🔍 View raw mind map text"):
|
| 62 |
+
st.text(mindmap_text)
|