from flask import Flask, redirect import requests import os import random from flask import Flask, redirect import requests import os import subprocess ngrok_token3 = "2cZivBw4scg2NQLDwuHPrHSbvkl_43ZbqjNAg5LVnvWXnQX5m" edge3 = "edghts_2cZj7RmeYoekH1kwK6RtexSkm0Z" command_ngrok = f"ngrok tunnel --label edge={edge3} --authtoken={ngrok_token3} --region=ap http://localhost:5006" app = Flask(__name__) def check_website_availability(url): try: response = requests.get(url) # 检查响应状态码,2xx表示成功 return response.status_code // 100 == 2 except requests.ConnectionError: return False @app.route('/') def redirect_to_website(): url = os.getenv('OPENI_GRADIO_URL') server1 = 'https://mlunotebook.openi.org.cn' server2 = 'http://cloudbrain1notebook.openi.org.cn' url1 = server1 + url + "/" url2 = server2 + url + "/" # 设定跳转概率,25%概率跳转到 {url1}, {url1}sd2/, {url1}sd3/, {url1}sd4/ options = [url1, f"{url1}sd2/", f"{url1}sd3/", f"{url1}sd4/"] selected_url = random.choices(options, weights=[0.25, 0.25, 0.25, 0.25])[0] options2 = [url2, f"{url2}sd2/", f"{url2}sd3/", f"{url2}sd4/"] selected_url2 = random.choices(options2, weights=[0.25, 0.25, 0.25, 0.25])[0] # 检查是否可访问 if check_website_availability(url1): return redirect(selected_url) else: # 如果url1不可访问,直接跳转到url2 return redirect(selected_url2) if __name__ == '__main__': app.run(debug=False, port=5006)