Gig / app.py
Riy777's picture
Update app.py
9a1aec1 verified
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")
# هام جداً: تأكد من وضع بيانات الهوية في الـ Secrets باسم NODE_DATA
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)
# 1. تثبيت الهوية من الـ Secrets لضمان عدم تغير الـ ID
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.")
# 2. تهيئة سريعة
subprocess.run(["gaianet", "init"], check=False)
# 3. توجيه الإعدادات للووركر
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)
# 4. تشغيل العقدة ومحاولة فتح النفق بالقوة
# سنحاول تشغيله عدة مرات لضمان اختراق جدار الحماية
print("🚀 Forcing Tunnel Connection...")
for i in range(3):
subprocess.Popen(["gaianet", "start"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
time.sleep(20)
# 5. طباعة المعلومات الرسمية النهائية
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)