Unfaithful commited on
Commit
f4edfd4
·
verified ·
1 Parent(s): 31b857d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -6
app.py CHANGED
@@ -5,6 +5,8 @@ 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
 
@@ -66,8 +68,15 @@ class Network:
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.close()
70
- return "network.png"
 
 
71
 
72
  @staticmethod
73
  def get_color(value):
@@ -95,7 +104,7 @@ def reset_network():
95
  return network.render()
96
 
97
  with gr.Blocks() as demo:
98
- gr.Markdown("# Compassion Network (Docker Example)")
99
  gr.Markdown("Add nodes and watch them grow and connect. Click Reset to start over.")
100
  with gr.Row():
101
  with gr.Column():
@@ -105,15 +114,15 @@ with gr.Blocks() as demo:
105
  random_button = gr.Button("Add Random Node")
106
  reset_button = gr.Button("Reset Network")
107
  with gr.Column():
108
- graph_output = gr.Image("network.png", interactive=False)
109
 
110
- # Render an initial blank network image
111
- network.render()
112
 
113
  add_button.click(interact_network, inputs=[x_input, y_input], outputs=graph_output)
114
  random_button.click(random_node, inputs=None, outputs=graph_output)
115
  reset_button.click(reset_network, inputs=None, outputs=graph_output)
116
 
117
  if __name__ == "__main__":
118
- # Run on port 7860; required for Spaces
119
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
5
  import networkx as nx
6
  from matplotlib.colors import to_hex
7
  import threading
8
+ from io import BytesIO
9
+ from PIL import Image
10
 
11
  lock = threading.Lock()
12
 
 
68
  nx.draw(G, pos, with_labels=False, node_size=sizes, node_color=colors, edge_color="gray")
69
  plt.axis('off')
70
  plt.tight_layout()
71
+
72
+ # Save the plot to an in-memory buffer
73
+ buf = BytesIO()
74
+ plt.savefig(buf, format='png')
75
+ buf.seek(0)
76
  plt.close()
77
+
78
+ # Return a PIL image
79
+ return Image.open(buf)
80
 
81
  @staticmethod
82
  def get_color(value):
 
104
  return network.render()
105
 
106
  with gr.Blocks() as demo:
107
+ gr.Markdown("# Compassion Network (In-Memory Image Example)")
108
  gr.Markdown("Add nodes and watch them grow and connect. Click Reset to start over.")
109
  with gr.Row():
110
  with gr.Column():
 
114
  random_button = gr.Button("Add Random Node")
115
  reset_button = gr.Button("Reset Network")
116
  with gr.Column():
117
+ graph_output = gr.Image(type="pil", interactive=False)
118
 
119
+ # Initialize the graph with an empty image
120
+ graph_output.update(value=network.render())
121
 
122
  add_button.click(interact_network, inputs=[x_input, y_input], outputs=graph_output)
123
  random_button.click(random_node, inputs=None, outputs=graph_output)
124
  reset_button.click(reset_network, inputs=None, outputs=graph_output)
125
 
126
  if __name__ == "__main__":
127
+ # Required for Hugging Face Spaces Docker
128
  demo.launch(server_name="0.0.0.0", server_port=7860)