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)