oscarwang2 commited on
Commit
93a340f
·
verified ·
1 Parent(s): 654be7b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,
13
+ "prev_count2": 0,
14
+ "prev_count3": 0,
15
+ "prev_total_tokens": 0,
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"]
38
+ total_tokens = curr_count1 + curr_count2 + curr_count3
39
+ total_growth_speed = growth_speed1 + growth_speed2 + growth_speed3
40
+
41
+ state["prev_count1"] = curr_count1
42
+ state["prev_count2"] = curr_count2
43
+ state["prev_count3"] = curr_count3
44
+ state["prev_total_tokens"] = total_tokens
45
+ state["total_growth_speed"] = total_growth_speed
46
+
47
+ time.sleep(5)
48
+ except Exception as e:
49
+ print(f"Error in monitor growth: {e}")
50
+
51
+ # Start the monitor thread
52
+ Thread(target=monitor_growth, daemon=True).start()
53
+
54
+ def get_dashboard_metrics():
55
+ return state["prev_total_tokens"], state["total_growth_speed"]
56
+
57
+ # Create Gradio Interface
58
+ with gr.Blocks() as demo:
59
+ gr.Markdown("# Token Count Dashboard")
60
+ total_tokens = gr.Number(label="Total Token Count", value=0)
61
+ growth_speed = gr.Number(label="Total Growth Speed", value=0)
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()