alex n commited on
Commit
32253ac
·
1 Parent(s): 5936870

connection fix

Browse files
Files changed (1) hide show
  1. app.py +37 -20
app.py CHANGED
@@ -83,16 +83,29 @@ custom_css = """
83
  }
84
  """
85
 
86
- # Initialize bittensor objects
87
- subtensor = bt.subtensor()
88
- metagraph = bt.metagraph(netuid=36)
 
 
 
 
 
89
 
90
  def get_validator_data() -> pd.DataFrame:
91
- validator_ids = list(set([i for i in range(len(metagraph.validator_permit))
92
- if metagraph.validator_permit[i] and
93
- metagraph.active[i] and
94
- str(metagraph.axons[i].ip) != "0.0.0.0"]))
95
 
 
 
 
 
 
 
 
 
 
96
  current_block = subtensor.block
97
  results = []
98
 
@@ -121,21 +134,24 @@ def get_validator_data() -> pd.DataFrame:
121
  # Get Step and Range from endpoints
122
  try:
123
  axon_endpoint = f"http://{validator_info['Axon']}"
124
- step_response = requests.get(f"{axon_endpoint}/step")
125
  step_response.raise_for_status()
126
  validator_info['Step'] = step_response.json()
127
 
128
  bits_response = requests.get(
129
  f"{axon_endpoint}/bits",
130
- headers={"range": "bytes=0-1"}
 
131
  )
132
  bits_response.raise_for_status()
133
  binary_string = ''.join(format(byte, '08b') for byte in bits_response.content)
134
  validator_info['Recent Bits'] = binary_string
135
- validator_info['API'] = '<span class="api-status api-up">✅</span>' if response_ok else '<span class="api-status api-down">❌</span>'
136
 
 
 
137
  except Exception as e:
138
- print(f"Error getting Step/Range for UID {uid}: {str(e)}")
139
 
140
  try:
141
  last_update = metagraph.last_update[uid]
@@ -157,7 +173,7 @@ def get_validator_data() -> pd.DataFrame:
157
  return df.sort_values('Step', ascending=False)[['Name', 'UID', 'Axon', 'API', 'Step', 'Recent Bits', 'Updated', 'VTrust']]
158
 
159
  # Create the Gradio interface
160
- demo = gr.Blocks(css=custom_css)
161
 
162
  # Update the HTML template
163
  header_html = """
@@ -167,7 +183,7 @@ header_html = """
167
  </div>
168
  """
169
 
170
- with demo:
171
  gr.HTML(header_html)
172
 
173
  with gr.Tabs() as tabs:
@@ -215,21 +231,22 @@ with demo:
215
 
216
  refresh_button.click(
217
  fn=update_leaderboard,
218
- outputs=[leaderboard, status_message]
 
219
  )
220
 
221
  # Auto-refresh logic
222
  def setup_auto_refresh():
223
- demo.scheduler = BackgroundScheduler()
224
- demo.scheduler.add_job(
225
- lambda: demo.queue(update_leaderboard),
226
  'interval',
227
  minutes=5
228
  )
229
- demo.scheduler.start()
230
 
231
  # Initial data load
232
- demo.load(
233
  fn=update_leaderboard,
234
  outputs=[leaderboard, status_message]
235
  )
@@ -237,4 +254,4 @@ with demo:
237
  setup_auto_refresh()
238
 
239
  # Launch the interface
240
- demo.launch()
 
83
  }
84
  """
85
 
86
+ # Add error handling for bittensor initialization
87
+ try:
88
+ subtensor = bt.subtensor()
89
+ metagraph = bt.metagraph(netuid=36)
90
+ except Exception as e:
91
+ print(f"Failed to initialize bittensor: {e}")
92
+ subtensor = None
93
+ metagraph = None
94
 
95
  def get_validator_data() -> pd.DataFrame:
96
+ if subtensor is None or metagraph is None:
97
+ # Return empty DataFrame with correct columns if initialization failed
98
+ return pd.DataFrame(columns=['Name', 'UID', 'Axon', 'API', 'Step', 'Recent Bits', 'Updated', 'VTrust'])
 
99
 
100
+ try:
101
+ validator_ids = list(set([i for i in range(len(metagraph.validator_permit))
102
+ if metagraph.validator_permit[i] and
103
+ metagraph.active[i] and
104
+ str(metagraph.axons[i].ip) != "0.0.0.0"]))
105
+ except Exception as e:
106
+ print(f"Error getting validator IDs: {e}")
107
+ validator_ids = []
108
+
109
  current_block = subtensor.block
110
  results = []
111
 
 
134
  # Get Step and Range from endpoints
135
  try:
136
  axon_endpoint = f"http://{validator_info['Axon']}"
137
+ step_response = requests.get(f"{axon_endpoint}/step", timeout=5)
138
  step_response.raise_for_status()
139
  validator_info['Step'] = step_response.json()
140
 
141
  bits_response = requests.get(
142
  f"{axon_endpoint}/bits",
143
+ headers={"range": "bytes=0-1"},
144
+ timeout=5
145
  )
146
  bits_response.raise_for_status()
147
  binary_string = ''.join(format(byte, '08b') for byte in bits_response.content)
148
  validator_info['Recent Bits'] = binary_string
149
+ validator_info['API'] = '<span class="api-status api-up">✅</span>' if bits_response.ok else '<span class="api-status api-down">❌</span>'
150
 
151
+ except requests.Timeout:
152
+ print(f"Timeout while connecting to {axon_endpoint}")
153
  except Exception as e:
154
+ print(f"Error connecting to {axon_endpoint}: {e}")
155
 
156
  try:
157
  last_update = metagraph.last_update[uid]
 
173
  return df.sort_values('Step', ascending=False)[['Name', 'UID', 'Axon', 'API', 'Step', 'Recent Bits', 'Updated', 'VTrust']]
174
 
175
  # Create the Gradio interface
176
+ app = gr.Blocks(css=custom_css)
177
 
178
  # Update the HTML template
179
  header_html = """
 
183
  </div>
184
  """
185
 
186
+ with app:
187
  gr.HTML(header_html)
188
 
189
  with gr.Tabs() as tabs:
 
231
 
232
  refresh_button.click(
233
  fn=update_leaderboard,
234
+ outputs=[leaderboard, status_message],
235
+ queue=False
236
  )
237
 
238
  # Auto-refresh logic
239
  def setup_auto_refresh():
240
+ app.scheduler = BackgroundScheduler()
241
+ app.scheduler.add_job(
242
+ lambda: app.queue(update_leaderboard),
243
  'interval',
244
  minutes=5
245
  )
246
+ app.scheduler.start()
247
 
248
  # Initial data load
249
+ app.load(
250
  fn=update_leaderboard,
251
  outputs=[leaderboard, status_message]
252
  )
 
254
  setup_auto_refresh()
255
 
256
  # Launch the interface
257
+ app.launch()