Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,26 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
-
from connection_engine import start_connect_loop, CONNECTED, PORT # โ ุฅุฐุง ูุถุนุช ุงูููุฏ ูู ู
ูู ู
ููุตู
|
| 5 |
-
start_connect_loop()
|
| 6 |
-
|
| 7 |
-
app = Flask(__name__)# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 8 |
-
# ู
ุญุฑู ุงุชุตุงู: ุณูุฑูุฑ ุนุดูุงุฆู ู
ู peer_discovery + ู
ูุงูุฐ 0001..9999 ุจุงูุชุณูุณู
|
| 9 |
-
# ูุชููู ููุท ุนูุฏ ุฃูู ุชุณุฌูู ูุงุฌุญ
|
| 10 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 11 |
-
import os, time, threading, socket, requests, importlib, random, sys
|
| 12 |
|
|
|
|
| 13 |
CURRENT_SERVER = None
|
| 14 |
PORT = None
|
| 15 |
CONNECTED = threading.Event()
|
| 16 |
-
|
| 17 |
-
CONNECTED.wait()
|
| 18 |
-
|
| 19 |
-
# ู
ุซุงู: ุชุดุบูู ุฎุงุฏู
Flask ุฃู RPC ุจุงุณุชุฎุฏุงู
PORT ุงูู
ูุชุดู
|
| 20 |
-
from rpc_server import rpc_app
|
| 21 |
-
rpc_app.run(host="0.0.0.0", port=PORT)
|
| 22 |
|
|
|
|
| 23 |
def _load_peer_discovery():
|
| 24 |
import peer_discovery as pd
|
| 25 |
importlib.reload(pd)
|
| 26 |
return pd
|
| 27 |
|
| 28 |
def _pick_random_server(pd):
|
| 29 |
-
# ุฅู ูุงู ูุฏูู pd.random_server() ุงุณุชุฎุฏู
ูุ ูุฅูุง ุงุฎุชุฑ ุนุดูุงุฆูุงู ู
ู ุงููุงุฆู
ุฉ
|
| 30 |
-
if hasattr(pd, "random_server"):
|
| 31 |
-
return pd.random_server()
|
| 32 |
servers = list(getattr(pd, "CENTRAL_REGISTRY_SERVERS", []))
|
| 33 |
if not servers:
|
| 34 |
raise RuntimeError("ูุง ุชูุฌุฏ ุณูุฑูุฑุงุช ูู peer_discovery.CENTRAL_REGISTRY_SERVERS")
|
|
@@ -38,7 +30,6 @@ def _pick_random_server(pd):
|
|
| 38 |
def _get_local_ip(pd):
|
| 39 |
if hasattr(pd, "get_local_ip"):
|
| 40 |
return pd.get_local_ip()
|
| 41 |
-
# ุงุญุชูุงุท
|
| 42 |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
| 43 |
try:
|
| 44 |
s.connect(("8.8.8.8", 80))
|
|
@@ -52,13 +43,12 @@ def _lan_port(pd):
|
|
| 52 |
return int(getattr(pd, "LAN_PORT", os.getenv("LAN_PORT", 7520)))
|
| 53 |
|
| 54 |
def _can_bind_low_ports():
|
| 55 |
-
# ุนูู ููููุณ: ุชุญุชุงุฌ root ููู
ูุงูุฐ <1024
|
| 56 |
if os.name != "nt":
|
| 57 |
try:
|
| 58 |
-
return os.geteuid() == 0
|
| 59 |
except Exception:
|
| 60 |
return False
|
| 61 |
-
return True
|
| 62 |
|
| 63 |
def _is_port_free(port, host="0.0.0.0"):
|
| 64 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
@@ -75,7 +65,6 @@ def _try_register(server, port, node_id, ip):
|
|
| 75 |
r.raise_for_status()
|
| 76 |
|
| 77 |
def _sequential_ports(pd):
|
| 78 |
-
"""ููููุฏ 0001..9999 ู
ุน ู
ุฑุงุนุงุฉ ุงูู LAN_PORT ูุชุฎุทู <1024 ุฅุฐุง ุบูุฑ ู
ุณู
ูุญ."""
|
| 79 |
lan_p = _lan_port(pd)
|
| 80 |
start = 1 if _can_bind_low_ports() else 1025
|
| 81 |
for p in range(start, 10000):
|
|
@@ -105,39 +94,42 @@ def _connect_until_success():
|
|
| 105 |
print(f"โ
Connected to {server} with port {PORT:04d}")
|
| 106 |
return
|
| 107 |
except Exception as e:
|
| 108 |
-
# ุงุทุจุน ุนูู ูุชุฑุงุช ูุชุฌูุจ ุงูุถูุถุงุก
|
| 109 |
if p % 1000 == 0:
|
| 110 |
print(f"โฆstill scanning (last error on {server}:{p:04d}) -> {e}")
|
| 111 |
-
|
| 112 |
-
# ูู
ููุฌุญ ุฃู ู
ููุฐ ู
ุน ูุฐุง ุงูุณูุฑูุฑ โ ุฌุฑูุจ ุณูุฑูุฑ ุฌุฏูุฏ
|
| 113 |
if not any_attempt:
|
| 114 |
print("โ ๏ธ ูู
ุชูุฌุฑู ู
ุญุงููุงุช ู
ูุงูุฐ (ุชุญูู ู
ู ุตูุงุญูุงุช ุงูู
ูุงูุฐ ุงูู
ูุฎูุถุฉ).")
|
| 115 |
-
|
| 116 |
print(f"โป No success on {server}. Picking a new serverโฆ")
|
| 117 |
time.sleep(backoff)
|
| 118 |
-
backoff = min(backoff * 2, 10)
|
| 119 |
|
| 120 |
def start_connect_loop():
|
|
|
|
| 121 |
threading.Thread(target=_connect_until_success, daemon=True).start()
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
# ู
ุณุงุฑ
|
| 127 |
@app.get("/")
|
| 128 |
def hello():
|
| 129 |
-
return "โ
|
| 130 |
|
| 131 |
-
# ู
ุซุงู ูู
ุณุงุฑ POST ูู
ูู ุฑุจุทู ุจูุงุฌูุชู
|
| 132 |
@app.post("/generate")
|
| 133 |
def generate():
|
| 134 |
data = request.json or {}
|
| 135 |
text = data.get("text", "")
|
| 136 |
-
|
| 137 |
-
return jsonify({"echo": text[::-1]}) # ูุนูุณ ุงููุต ูู
ุซุงู ููุท
|
| 138 |
-
from peer_discovery import PORT, PORT
|
| 139 |
|
|
|
|
| 140 |
if __name__ == "__main__":
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
app.run(host="0.0.0.0", port=port, debug=False)
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
import os
|
| 3 |
+
import threading
|
| 4 |
+
import time
|
| 5 |
+
import socket
|
| 6 |
+
import requests
|
| 7 |
+
import importlib
|
| 8 |
+
import random
|
| 9 |
from flask import Flask, request, jsonify
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# โโโโโโโโโ ุฅุนุฏุงุฏุงุช ุนุงู
ุฉ โโโโโโโโโโ
|
| 12 |
CURRENT_SERVER = None
|
| 13 |
PORT = None
|
| 14 |
CONNECTED = threading.Event()
|
| 15 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# โโโโโโโโโ ุฏูุงู ู
ุญุฑู ุงูุงุชุตุงู โโโโโโโโโโ
|
| 18 |
def _load_peer_discovery():
|
| 19 |
import peer_discovery as pd
|
| 20 |
importlib.reload(pd)
|
| 21 |
return pd
|
| 22 |
|
| 23 |
def _pick_random_server(pd):
|
|
|
|
|
|
|
|
|
|
| 24 |
servers = list(getattr(pd, "CENTRAL_REGISTRY_SERVERS", []))
|
| 25 |
if not servers:
|
| 26 |
raise RuntimeError("ูุง ุชูุฌุฏ ุณูุฑูุฑุงุช ูู peer_discovery.CENTRAL_REGISTRY_SERVERS")
|
|
|
|
| 30 |
def _get_local_ip(pd):
|
| 31 |
if hasattr(pd, "get_local_ip"):
|
| 32 |
return pd.get_local_ip()
|
|
|
|
| 33 |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
| 34 |
try:
|
| 35 |
s.connect(("8.8.8.8", 80))
|
|
|
|
| 43 |
return int(getattr(pd, "LAN_PORT", os.getenv("LAN_PORT", 7520)))
|
| 44 |
|
| 45 |
def _can_bind_low_ports():
|
|
|
|
| 46 |
if os.name != "nt":
|
| 47 |
try:
|
| 48 |
+
return os.geteuid() == 0
|
| 49 |
except Exception:
|
| 50 |
return False
|
| 51 |
+
return True
|
| 52 |
|
| 53 |
def _is_port_free(port, host="0.0.0.0"):
|
| 54 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
| 65 |
r.raise_for_status()
|
| 66 |
|
| 67 |
def _sequential_ports(pd):
|
|
|
|
| 68 |
lan_p = _lan_port(pd)
|
| 69 |
start = 1 if _can_bind_low_ports() else 1025
|
| 70 |
for p in range(start, 10000):
|
|
|
|
| 94 |
print(f"โ
Connected to {server} with port {PORT:04d}")
|
| 95 |
return
|
| 96 |
except Exception as e:
|
|
|
|
| 97 |
if p % 1000 == 0:
|
| 98 |
print(f"โฆstill scanning (last error on {server}:{p:04d}) -> {e}")
|
|
|
|
|
|
|
| 99 |
if not any_attempt:
|
| 100 |
print("โ ๏ธ ูู
ุชูุฌุฑู ู
ุญุงููุงุช ู
ูุงูุฐ (ุชุญูู ู
ู ุตูุงุญูุงุช ุงูู
ูุงูุฐ ุงูู
ูุฎูุถุฉ).")
|
|
|
|
| 101 |
print(f"โป No success on {server}. Picking a new serverโฆ")
|
| 102 |
time.sleep(backoff)
|
| 103 |
+
backoff = min(backoff * 2, 10)
|
| 104 |
|
| 105 |
def start_connect_loop():
|
| 106 |
+
print("๐ Launching connection thread")
|
| 107 |
threading.Thread(target=_connect_until_success, daemon=True).start()
|
| 108 |
|
| 109 |
+
# โโโโโโโโโ ุชุดุบูู ุงูุงุชุตุงู ุงูุฎุงุฑุฌู โโโโโโโโโโ
|
| 110 |
+
start_connect_loop()
|
| 111 |
+
|
| 112 |
+
# โโโโโโโโโ (ุงุฎุชูุงุฑู) ุชุดุบูู ุฎุงุฏู
ุฎุงุฑุฌู ุจุนุฏ ุงูุงุชุตุงู โโโโโโโโโโ
|
| 113 |
+
def run_rpc_server_after_connected():
|
| 114 |
+
CONNECTED.wait()
|
| 115 |
+
print(f"๐ RPC Server ready on PORT={PORT}")
|
| 116 |
+
from rpc_server import rpc_app
|
| 117 |
+
rpc_app.run(host="0.0.0.0", port=PORT)
|
| 118 |
+
|
| 119 |
+
threading.Thread(target=run_rpc_server_after_connected, daemon=True).start()
|
| 120 |
|
| 121 |
+
# โโโโโโโโโ ู
ุณุงุฑุงุช Flask ุงูุฃุณุงุณูุฉ โโโโโโโโโโ
|
| 122 |
@app.get("/")
|
| 123 |
def hello():
|
| 124 |
+
return f"โ
ุงูุญู
ุฏ ูููโฆ ุงูุชุทุจูู ูุนู
ู ุนูู Hugging Face Spaces! [ุณูุฑูุฑ: {CURRENT_SERVER}, ุจูุฑุช: {PORT}]"
|
| 125 |
|
|
|
|
| 126 |
@app.post("/generate")
|
| 127 |
def generate():
|
| 128 |
data = request.json or {}
|
| 129 |
text = data.get("text", "")
|
| 130 |
+
return jsonify({"echo": text[::-1]})
|
|
|
|
|
|
|
| 131 |
|
| 132 |
+
# โโโโโโโโโ ุชุดุบูู ุชุทุจูู Flask ุงูุฃุณุงุณู โโโโโโโโโโ
|
| 133 |
if __name__ == "__main__":
|
| 134 |
+
hf_port = int(os.environ.get("PORT", 7860)) # Port ุงูู
ุฎุตุต ู
ู Hugging Face
|
| 135 |
+
app.run(host="0.0.0.0", port=hf_port, debug=False)
|
|
|