File size: 1,890 Bytes
2f0df9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9c0dad
2f0df9b
 
 
 
a9c0dad
2f0df9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import streamlit as st
import networkx as nx
import matplotlib.pyplot as plt
import openai
import os

# Set OpenAI API key from Hugging Face secrets
openai.api_key = os.getenv("OPENAI_API_KEY")

st.set_page_config(page_title="AI Mind Map Generator", layout="wide")
st.title("🧠 AI Mind Map Generator")
st.write("Enter a topic and generate a mind map using GPT-3.5!")

topic = st.text_input("Enter a topic to generate mind map:", "")

def generate_mindmap(topic):
    prompt = f"Create a simple, structured mind map for the topic '{topic}'. Use bullet points to show sub-topics and indentation for hierarchy."
    response = openai.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.7
    )
    return response.choices[0].message.content

def build_graph_from_text(text):
    G = nx.Graph()
    parents = []
    prev_indent = -1

    for line in text.splitlines():
        if not line.strip():
            continue

        indent = len(line) - len(line.lstrip(" "))
        node = line.strip("-• ").strip()

        if indent == 0:
            parents = [node]
        else:
            level = indent // 2
            if level < len(parents):
                parents = parents[:level]
            parents.append(node)

        G.add_node(node)
        if len(parents) > 1:
            G.add_edge(parents[-2], node)

    return G

if topic:
    with st.spinner("Generating mind map..."):
        mindmap_text = generate_mindmap(topic)
        G = build_graph_from_text(mindmap_text)

        fig, ax = plt.subplots(figsize=(10, 6))
        pos = nx.spring_layout(G, seed=42)
        nx.draw(G, pos, with_labels=True, node_color="skyblue", edge_color="gray", node_size=2000, font_size=10)
        st.pyplot(fig)

        with st.expander("🔍 View raw mind map text"):
            st.text(mindmap_text)