-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt-tree-viz.py
119 lines (100 loc) · 2.78 KB
/
chatgpt-tree-viz.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import json
import dash
import dash_cytoscape as cyto
from dash import html
with open("./conversation_object.json", "r", encoding="utf-8") as file:
conversation_data = json.load(file)
mapping_data = conversation_data.get("mapping", {})
nodes = []
edges = []
for key, value in mapping_data.items():
message = value.get("message", {})
content = message.get("content", {}) if message else {}
content_parts = content.get("parts", []) if content else []
label = content_parts[0] if content_parts else ""
nodes.append({
"data": {
"id": key,
"label": label,
"author": message["author"]["role"] if message else "unknown"
}
})
if "parent" in value and value["parent"]:
edges.append({
"data": {
"source": value["parent"],
"target": key
}
})
graph_data = {
"nodes": nodes,
"edges": edges
}
cyto.load_extra_layouts()
app = dash.Dash(__name__)
app.title = "ChatGPT Conversation Visualization"
for node in graph_data["nodes"]:
label_length = len(node["data"]["label"])
node_width = 500
node_height = max(10, 20 * (label_length ** 0.5))
node["data"]["width"] = node_width
node["data"]["height"] = node_height
stylesheet = [
{
'selector': 'node',
'style': {
'label': 'data(label)',
'text-align': 'justify',
'text-valign': 'center',
'background-color': '#0074D9',
'color': '#FFFFFF',
'font-size': '8px',
'text-wrap': 'wrap',
'text-max-height': '90%',
'text-max-width': '450px',
'shape': 'round-rectangle',
'width': 'data(width)',
'height': 'data(height)'
}
},
{
'selector': '[author = "user"]',
'style': {'background-color': '#2ECC40'}
},
{
'selector': '[author = "system"]',
'style': {'background-color': '#FFDC00'}
},
{
'selector': 'edge',
'style': {
'curve-style': 'bezier',
'target-arrow-shape': 'triangle',
'line-color': '#AAAAAA',
'target-arrow-color': '#AAAAAA'
}
}
]
app.layout = html.Div([
cyto.Cytoscape(
id='cytoscape',
elements=[
*graph_data["nodes"],
*graph_data["edges"]
],
layout={'name': 'dagre'},
style={'width': '100%', 'height': '100vh'},
stylesheet=stylesheet,
boxSelectionEnabled=False,
zoomingEnabled=True,
panningEnabled=True,
zoom=1,
minZoom=0.05,
maxZoom=5,
responsive=True,
autolock=False,
autoungrabify=True,
autounselectify=True,
)
])
app.run(debug=True)