Unfaithful commited on
Commit
b49bf63
·
verified ·
1 Parent(s): 0acc466

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import gradio as gr
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ import networkx as nx
6
+ from matplotlib.colors import to_hex
7
+ import threading
8
+
9
+ lock = threading.Lock()
10
+
11
+ class Node:
12
+ def __init__(self, x, y, value=1.0):
13
+ self.x = x
14
+ self.y = y
15
+ self.value = value
16
+ self.connections = []
17
+
18
+ def connect(self, other_node):
19
+ if other_node not in self.connections:
20
+ self.connections.append(other_node)
21
+
22
+ def grow(self, increment=0.1):
23
+ self.value = min(self.value + increment, 5.0)
24
+
25
+ class Network:
26
+ def __init__(self):
27
+ self.nodes = []
28
+
29
+ def add_node(self, x, y):
30
+ with lock:
31
+ new_node = Node(x, y, value=random.uniform(1.0, 2.0))
32
+ self.nodes.append(new_node)
33
+ return new_node
34
+
35
+ def connect_nodes(self, distance_threshold=75):
36
+ with lock:
37
+ for node1 in self.nodes:
38
+ for node2 in self.nodes:
39
+ if node1 != node2 and self.distance(node1, node2) < distance_threshold:
40
+ node1.connect(node2)
41
+
42
+ def grow_nodes(self):
43
+ with lock:
44
+ for node in self.nodes:
45
+ node.grow()
46
+
47
+ def reset(self):
48
+ with lock:
49
+ self.nodes = []
50
+
51
+ def render(self):
52
+ with lock:
53
+ G = nx.Graph()
54
+ for i, node in enumerate(self.nodes):
55
+ G.add_node(i, pos=(node.x, node.y), size=node.value*100, color=self.get_color(node.value))
56
+ for i, node in enumerate(self.nodes):
57
+ for conn in node.connections:
58
+ j = self.nodes.index(conn)
59
+ G.add_edge(i, j)
60
+
61
+ pos = nx.get_node_attributes(G, 'pos')
62
+ sizes = [data['size'] for _, data in G.nodes(data=True)]
63
+ colors = [data['color'] for _, data in G.nodes(data=True)]
64
+
65
+ plt.figure(figsize=(8,8))
66
+ nx.draw(G, pos, with_labels=False, node_size=sizes, node_color=colors, edge_color="gray")
67
+ plt.axis('off')
68
+ plt.tight_layout()
69
+ plt.savefig("network.png")
70
+ plt.close()
71
+ return "network.png"
72
+
73
+ @staticmethod
74
+ def get_color(value):
75
+ gradient = plt.cm.coolwarm
76
+ return to_hex(gradient(value / 5))
77
+
78
+ @staticmethod
79
+ def distance(node1, node2):
80
+ return np.sqrt((node1.x - node2.x)**2 + (node1.y - node2.y)**2)
81
+
82
+ network = Network()
83
+
84
+ def interact_network(x, y):
85
+ network.add_node(x, y)
86
+ network.connect_nodes()
87
+ network.grow_nodes()
88
+ return network.render()
89
+
90
+ def random_node():
91
+ x, y = random.randint(50, 450), random.randint(50, 450)
92
+ return interact_network(x, y)
93
+
94
+ def reset_network():
95
+ network.reset()
96
+ return network.render()
97
+
98
+ with gr.Blocks() as demo:
99
+ gr.Markdown("# Compassion Network (Docker Example)")
100
+ gr.Markdown("Add nodes and watch them grow and connect. Click Reset to start over.")
101
+ with gr.Row():
102
+ with gr.Column():
103
+ x_input = gr.Number(label="X Position", value=random.randint(50,450))
104
+ y_input = gr.Number(label="Y Position", value=random.randint(50,450))
105
+ add_button = gr.Button("Add Node")
106
+ random_button = gr.Button("Add Random Node")
107
+ reset_button = gr.Button("Reset Network")
108
+ with gr.Column():
109
+ graph_output = gr.Image("network.png", interactive=False)
110
+
111
+ # Render an initial blank network image
112
+ network.render()
113
+
114
+ add_button.click(interact_network, inputs=[x_input, y_input], outputs=graph_output)
115
+ random_button.click(random_node, inputs=None, outputs=graph_output)
116
+ reset_button.click(reset_network, inputs=None, outputs=graph_output)
117
+
118
+ if __name__ == "__main__":
119
+ # Run on port 7860; required for Spaces
120
+ demo.launch(server_name="0.0.0.0", server_port=7860)