oscarwang2 commited on
Commit
0f255ad
1 Parent(s): 93a340f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -17
app.py CHANGED
@@ -1,12 +1,12 @@
1
  import gradio as gr
2
- import requests
3
  import time
4
  from threading import Thread
5
 
6
- # URLs for the APIs
7
- client1_url = 'https://orionai-training-data-collection-2.hf.space/api/update_token_display'
8
- client2_url = 'https://orionai-training-data-collection-3.hf.space/api/update_token_display'
9
- client3_url = 'https://orionai-training-data-collection.hf.space/api/update_token_display'
10
 
11
  state = {
12
  "prev_count1": 0,
@@ -16,22 +16,20 @@ state = {
16
  "total_growth_speed": 0
17
  }
18
 
19
- def get_token_count(url):
20
  try:
21
- response = requests.post(url, headers={'Content-Type': 'application/json'})
22
- response.raise_for_status() # Raise an error for bad status codes
23
- result = response.json()
24
- return int(result.get('token_count', 0)) # Adjust according to API response
25
- except (requests.RequestException, ValueError) as e:
26
- print(f"Error fetching token count from {url}: {e}")
27
  return 0
28
 
29
  def monitor_growth():
30
  while True:
31
  try:
32
- curr_count1 = get_token_count(client1_url)
33
- curr_count2 = get_token_count(client2_url)
34
- curr_count3 = get_token_count(client3_url)
35
  growth_speed1 = curr_count1 - state["prev_count1"]
36
  growth_speed2 = curr_count2 - state["prev_count2"]
37
  growth_speed3 = curr_count3 - state["prev_count3"]
@@ -62,9 +60,10 @@ with gr.Blocks() as demo:
62
 
63
  def update_dashboard():
64
  tokens, speed = get_dashboard_metrics()
65
- return tokens, speed
 
66
 
67
  # Automatically update the metrics every 5 seconds
68
- gr.Timer(5000, update_dashboard, [total_tokens, growth_speed], every=5000)
69
 
70
  demo.launch()
 
1
  import gradio as gr
2
+ from gradio_client import Client
3
  import time
4
  from threading import Thread
5
 
6
+ # Initialize clients
7
+ client1 = Client("orionai/training-data-collection-2")
8
+ client2 = Client("orionai/training-data-collection-3")
9
+ client3 = Client("orionai/training-data-collection")
10
 
11
  state = {
12
  "prev_count1": 0,
 
16
  "total_growth_speed": 0
17
  }
18
 
19
+ def get_token_count(client):
20
  try:
21
+ result = client.predict(api_name="/update_token_display")
22
+ return int(result)
23
+ except Exception as e:
24
+ print(f"Error fetching token count: {e}")
 
 
25
  return 0
26
 
27
  def monitor_growth():
28
  while True:
29
  try:
30
+ curr_count1 = get_token_count(client1)
31
+ curr_count2 = get_token_count(client2)
32
+ curr_count3 = get_token_count(client3)
33
  growth_speed1 = curr_count1 - state["prev_count1"]
34
  growth_speed2 = curr_count2 - state["prev_count2"]
35
  growth_speed3 = curr_count3 - state["prev_count3"]
 
60
 
61
  def update_dashboard():
62
  tokens, speed = get_dashboard_metrics()
63
+ total_tokens.update(value=tokens)
64
+ growth_speed.update(value=speed)
65
 
66
  # Automatically update the metrics every 5 seconds
67
+ gr.Timer(5, update_dashboard)
68
 
69
  demo.launch()