oscarwang2's picture
Update app.py
b9c4200 verified
raw
history blame
1.7 kB
from flask import Flask, jsonify
from threading import Thread
import time
from gradio_client import Client
app = Flask(__name__)
# Initialize clients
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
}
def get_token_count():
result1 = client1.predict(api_name="/update_token_display")
result2 = client2.predict(api_name="/update_token_display")
result3 = client3.predict(api_name="/update_token_display")
return int(result1), int(result2), int(result3)
def monitor_growth():
while True:
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
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)