import logging import os from flask import Flask, jsonify from huggingface_hub import HfApi, get_space_runtime, restart_space logging.basicConfig(level=logging.INFO) app = Flask(__name__) api = HfApi( token=os.environ.get("token") ) def get_external_url(space_id): try: username, spacename = space_id.split("/") return f"https://{username}-{spacename.replace('_', '-')}.hf.space/api" except Exception as e: return "" @app.route('/') def index(): return f'{get_external_url("batanus/restarter")}' @app.route('/api/restart_if_needed', methods=['GET']) def restart_if_needed(): try: # bibleRuntime = api.get_space_runtime("batanus/bible") # logging.info(f"bible: {openaiRuntime.stage}") # if bibleRuntime.stage != "RUNNING": # logging.info(f"bible - Restarted") # newBibleRuntime = api.restart_space("batanus/bible") # gpchatRuntime = api.get_space_runtime("batanus/gpchat") # logging.info(f"gpchat: {gpchatRuntime.stage}") # if gpchatRuntime.stage != "RUNNING": # logging.info(f"gpchat - Restarted") # newGpchatRuntime = api.restart_space("batanus/gpchat") sslerRuntime = api.get_space_runtime("batanus/ssler") # logging.info(f"ssler: {sslerRuntime.stage}") if sslerRuntime.stage != "RUNNING": logging.info(f"ssler - Restarted") newSslerRuntime = api.restart_space("batanus/ssler") # anichatRuntime = api.get_space_runtime("batanus/anichat") # logging.info(f"anichat: {openaiRuntime.stage}") # if anichatRuntime.stage != "RUNNING": # logging.info(f"anichat - Restarted") # newAnichatRuntime = api.restart_space("batanus/anichat") return '', 204 except Exception as e: logging.exception("An error occurred:") return 'Internal Server Error', 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=7860)