Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,135 +1,28 @@
|
|
| 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")
|
| 27 |
-
random.shuffle(servers)
|
| 28 |
-
return servers[0]
|
| 29 |
-
|
| 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))
|
| 36 |
-
return s.getsockname()[0]
|
| 37 |
-
except Exception:
|
| 38 |
-
return "127.0.0.1"
|
| 39 |
-
finally:
|
| 40 |
-
s.close()
|
| 41 |
-
|
| 42 |
-
def _lan_port(pd):
|
| 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:
|
| 55 |
-
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
| 56 |
-
try:
|
| 57 |
-
s.bind((host, int(port)))
|
| 58 |
-
return True
|
| 59 |
-
except OSError:
|
| 60 |
-
return False
|
| 61 |
-
|
| 62 |
-
def _try_register(server, port, node_id, ip):
|
| 63 |
-
payload = {"node_id": node_id, "ip": ip, "port": int(port)}
|
| 64 |
-
r = requests.post(f"{server}/register", json=payload, timeout=6)
|
| 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):
|
| 71 |
-
if p == lan_p:
|
| 72 |
-
continue
|
| 73 |
-
yield p
|
| 74 |
-
|
| 75 |
-
def _connect_until_success():
|
| 76 |
-
global CURRENT_SERVER, PORT
|
| 77 |
-
backoff = 1
|
| 78 |
-
while True:
|
| 79 |
-
pd = _load_peer_discovery()
|
| 80 |
-
server = _pick_random_server(pd)
|
| 81 |
-
node_id = os.getenv("NODE_ID", socket.gethostname())
|
| 82 |
-
ip = _get_local_ip(pd)
|
| 83 |
-
print(f"๐ Trying server: {server}")
|
| 84 |
-
print("๐ Started connection attempt loop")
|
| 85 |
-
any_attempt = False
|
| 86 |
-
for p in _sequential_ports(pd):
|
| 87 |
-
any_attempt = True
|
| 88 |
-
if not _is_port_free(p):
|
| 89 |
-
continue
|
| 90 |
-
try:
|
| 91 |
-
_try_register(server, p, node_id, ip)
|
| 92 |
-
CURRENT_SERVER, PORT = server, int(p)
|
| 93 |
-
CONNECTED.set()
|
| 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
|
| 124 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
@app.post("/generate")
|
| 127 |
def generate():
|
| 128 |
data = request.json or {}
|
| 129 |
text = data.get("text", "")
|
| 130 |
-
|
|
|
|
| 131 |
|
| 132 |
-
#
|
| 133 |
if __name__ == "__main__":
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
+
from peer_discovery import PORT, CURRENT_SERVER, PEERS
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@app.get("/")
|
| 9 |
+
def home():
|
| 10 |
+
return f"""
|
| 11 |
+
<h2>โ
Hugging Face App ูุนู
ู</h2>
|
| 12 |
+
<p><b>๐ ุงูุณูุฑูุฑ ุงูู
ุฑูุฒู:</b> {CURRENT_SERVER}</p>
|
| 13 |
+
<p><b>๐ง ุงูู
ููุฐ ุงูุฐู ุชุนู
ู ุนููู:</b> {PORT}</p>
|
| 14 |
+
<p><b>๐ค ุนุฏุฏ ุงูุฃูุฑุงู (Peers):</b> {len(PEERS)}</p>
|
| 15 |
+
"""
|
| 16 |
|
| 17 |
@app.post("/generate")
|
| 18 |
def generate():
|
| 19 |
data = request.json or {}
|
| 20 |
text = data.get("text", "")
|
| 21 |
+
response = text[::-1] # ุนูุณ ุงููุต ูู
ุซุงู
|
| 22 |
+
return jsonify({"result": response})
|
| 23 |
|
| 24 |
+
# ููุทุฉ ุงูุชุดุบูู ุงูุฑุฆูุณูุฉ
|
| 25 |
if __name__ == "__main__":
|
| 26 |
+
port = int(os.environ.get("PORT", 7860)) # Hugging Face ูู
ุฑุฑ ูุฐุง ุงูู
ุชุบูุฑ ุชููุงุฆููุง
|
| 27 |
+
print(f"๐ Flask app is starting on port {port}...")
|
| 28 |
+
app.run(host="0.0.0.0", port=port)
|