Oscar Wang commited on
Commit
0f28c01
·
verified ·
1 Parent(s): 74807c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -29
app.py CHANGED
@@ -1,9 +1,9 @@
1
- from flask import Flask, jsonify
2
- from threading import Thread
3
  import time
 
4
  from gradio_client import Client
5
-
6
- app = Flask(__name__)
7
 
8
  # Initialize clients
9
  client1 = Client("orionai/training-data-collection_2")
@@ -18,37 +18,46 @@ state = {
18
  "total_growth_speed": 0
19
  }
20
 
 
21
  def get_token_count():
22
- result1 = client1.predict(api_name="/update_token_display")
23
- result2 = client2.predict(api_name="/update_token_display")
24
- result3 = client3.predict(api_name="/update_token_display")
25
  return int(result1), int(result2), int(result3)
26
 
27
  def monitor_growth():
28
  while True:
29
- curr_count1, curr_count2, curr_count3 = get_token_count()
30
- growth_speed1 = curr_count1 - state["prev_count1"]
31
- growth_speed2 = curr_count2 - state["prev_count2"]
32
- growth_speed3 = curr_count3 - state["prev_count3"]
33
- total_tokens = curr_count1 + curr_count2 + curr_count3
34
- total_growth_speed = growth_speed1 + growth_speed2 + growth_speed3
35
-
36
- state["prev_count1"] = curr_count1
37
- state["prev_count2"] = curr_count2
38
- state["prev_count3"] = curr_count3
39
- state["prev_total_tokens"] = total_tokens
40
- state["total_growth_speed"] = total_growth_speed
 
 
 
 
 
 
 
 
41
 
42
- time.sleep(1)
 
43
 
44
- Thread(target=monitor_growth, daemon=True).start()
 
45
 
46
- @app.route('/stats', methods=['GET'])
47
- def get_stats():
48
- return jsonify({
49
- "total_tokens": state["prev_total_tokens"],
50
- "total_growth_speed": state["total_growth_speed"]
51
- })
52
 
53
- if __name__ == '__main__':
54
- app.run(debug=True)
 
1
+ import streamlit as st
 
2
  import time
3
+ from threading import Thread
4
  from gradio_client import Client
5
+ from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type
6
+ import httpx
7
 
8
  # Initialize clients
9
  client1 = Client("orionai/training-data-collection_2")
 
18
  "total_growth_speed": 0
19
  }
20
 
21
+ @retry(stop=stop_after_attempt(3), wait=wait_fixed(2), retry=retry_if_exception_type(httpx.ReadTimeout))
22
  def get_token_count():
23
+ result1 = client1.predict(api_name="/update_token_display", timeout=10.0)
24
+ result2 = client2.predict(api_name="/update_token_display", timeout=10.0)
25
+ result3 = client3.predict(api_name="/update_token_display", timeout=10.0)
26
  return int(result1), int(result2), int(result3)
27
 
28
  def monitor_growth():
29
  while True:
30
+ try:
31
+ curr_count1, curr_count2, curr_count3 = get_token_count()
32
+ growth_speed1 = curr_count1 - state["prev_count1"]
33
+ growth_speed2 = curr_count2 - state["prev_count2"]
34
+ growth_speed3 = curr_count3 - state["prev_count3"]
35
+ total_tokens = curr_count1 + curr_count2 + curr_count3
36
+ total_growth_speed = growth_speed1 + growth_speed2 + growth_speed3
37
+
38
+ state["prev_count1"] = curr_count1
39
+ state["prev_count2"] = curr_count2
40
+ state["prev_count3"] = curr_count3
41
+ state["prev_total_tokens"] = total_tokens
42
+ state["total_growth_speed"] = total_growth_speed
43
+
44
+ time.sleep(1)
45
+ except httpx.ReadTimeout:
46
+ print("Timeout occurred while fetching token count.")
47
+
48
+ # Start the monitor thread
49
+ Thread(target=monitor_growth, daemon=True).start()
50
 
51
+ # Streamlit UI
52
+ st.title("Token Count Dashboard")
53
 
54
+ token_count_placeholder = st.empty()
55
+ growth_speed_placeholder = st.empty()
56
 
57
+ def update_ui():
58
+ while True:
59
+ token_count_placeholder.metric("Total Token Count", state["prev_total_tokens"])
60
+ growth_speed_placeholder.metric("Total Growth Speed", state["total_growth_speed"])
61
+ time.sleep(1)
 
62
 
63
+ Thread(target=update_ui, daemon=True).start()