File size: 2,128 Bytes
d4d61a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from flask import Flask, jsonify
from threading import Thread
import time
from gradio_client import Client
from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type
import httpx

app = Flask(__name__)

# Initialize clients with retry and timeout settings
client1 = Client("orionai/training-data-collection_2")
client2 = Client("orionai/training-data-collection_3")
client3 = Client("orionai/training-data-collection")

state = {
    "prev_count1": 0,
    "prev_count2": 0,
    "prev_count3": 0,
    "prev_total_tokens": 0,
    "total_growth_speed": 0
}

@retry(stop=stop_after_attempt(3), wait=wait_fixed(2), retry=retry_if_exception_type(httpx.ReadTimeout))
def get_token_count():
    result1 = client1.predict(api_name="/update_token_display", timeout=10.0)
    result2 = client2.predict(api_name="/update_token_display", timeout=10.0)
    result3 = client3.predict(api_name="/update_token_display", timeout=10.0)
    return int(result1), int(result2), int(result3)

def monitor_growth():
    while True:
        try:
            curr_count1, curr_count2, curr_count3 = get_token_count()
            growth_speed1 = curr_count1 - state["prev_count1"]
            growth_speed2 = curr_count2 - state["prev_count2"]
            growth_speed3 = curr_count3 - state["prev_count3"]
            total_tokens = curr_count1 + curr_count2 + curr_count3
            total_growth_speed = growth_speed1 + growth_speed2 + growth_speed3

            state["prev_count1"] = curr_count1
            state["prev_count2"] = curr_count2
            state["prev_count3"] = curr_count3
            state["prev_total_tokens"] = total_tokens
            state["total_growth_speed"] = total_growth_speed
        except httpx.ReadTimeout:
            print("Timeout occurred while fetching token count.")
        time.sleep(1)

Thread(target=monitor_growth, daemon=True).start()

@app.route('/stats', methods=['GET'])
def get_stats():
    return jsonify({
        "total_tokens": state["prev_total_tokens"],
        "total_growth_speed": state["total_growth_speed"]
    })

if __name__ == '__main__':
    app.run(debug=True)