| | import os, requests, json, subprocess, time |
| | from flask import Flask, request, jsonify |
| |
|
| | app = Flask(__name__) |
| | WORKER_URL = os.getenv("WORKER_URL") |
| | WORKER_SECRET = os.getenv("WORKER_SECRET") |
| | |
| | NODE_DATA_STR = os.getenv("NODE_DATA") |
| |
|
| | def setup_and_force_broadcast(): |
| | print("--- 🛠️ Attempting to Bypass Hugging Face Restrictions ---") |
| | try: |
| | base_path = os.path.expanduser("~/gaianet") |
| | os.makedirs(base_path, exist_ok=True) |
| | |
| | |
| | if NODE_DATA_STR: |
| | with open(os.path.join(base_path, "nodeid.json"), 'w') as f: |
| | f.write(NODE_DATA_STR) |
| | print("✅ Persistent Identity Restored.") |
| |
|
| | |
| | subprocess.run(["gaianet", "init"], check=False) |
| | |
| | |
| | config_path = os.path.join(base_path, "config.json") |
| | with open(config_path, 'r') as f: |
| | config = json.load(f) |
| | config['chat'] = {"url": "http://localhost:7860/v1/chat/completions"} |
| | with open(config_path, 'w') as f: |
| | json.dump(config, f, indent=4) |
| |
|
| | |
| | |
| | print("🚀 Forcing Tunnel Connection...") |
| | for i in range(3): |
| | subprocess.Popen(["gaianet", "start"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| | time.sleep(20) |
| | |
| | |
| | info = subprocess.check_output(["gaianet", "info"], text=True) |
| | print(f"\n{'='*50}\n{info}\n{'='*50}\n") |
| |
|
| | except Exception as e: |
| | print(f"❌ Setup Failed: {e}") |
| |
|
| | @app.route('/v1/chat/completions', methods=['POST']) |
| | def proxy(): |
| | data = request.get_json() |
| | headers = {"Authorization": f"Bearer {WORKER_SECRET}", "Content-Type": "application/json"} |
| | try: |
| | response = requests.post(WORKER_URL, json=data, headers=headers, timeout=120) |
| | return jsonify(response.json()), response.status_code |
| | except Exception as e: |
| | return jsonify({"error": str(e)}), 500 |
| |
|
| | @app.route('/') |
| | def home(): return "Node Status: Fighting for Connection", 200 |
| |
|
| | if __name__ == '__main__': |
| | setup_and_force_broadcast() |
| | app.run(host='0.0.0.0', port=7860) |