Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,11 +12,43 @@ app = Flask(__name__)
|
|
| 12 |
# CONFIG
|
| 13 |
# =========================
|
| 14 |
Server = [
|
| 15 |
-
"https://
|
| 16 |
-
"https://APINOW-service-server.hf.space",
|
| 17 |
-
"https://APINOW-service-newserver.hf.space",
|
| 18 |
-
|
| 19 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
PING_INTERVAL = 120
|
| 21 |
REQUEST_TIMEOUT = 300
|
| 22 |
|
|
@@ -30,6 +62,94 @@ logging.basicConfig(
|
|
| 30 |
logger = logging.getLogger("proxy")
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# =========================
|
| 34 |
# HEALTH CHECK
|
| 35 |
# =========================
|
|
@@ -47,7 +167,7 @@ HEADERS = {"User-Agent": "HF-Proxy-KeepAliveBot"}
|
|
| 47 |
def keep_alive_worker():
|
| 48 |
while True:
|
| 49 |
logger.info("Pinging HF servers...")
|
| 50 |
-
for url in Server:
|
| 51 |
try:
|
| 52 |
r = requests.get(url, headers=HEADERS, timeout=8)
|
| 53 |
logger.info(f"{url} → {r.status_code}")
|
|
|
|
| 12 |
# CONFIG
|
| 13 |
# =========================
|
| 14 |
Server = [
|
| 15 |
+
"https://Techbitforge-server.hf.space"
|
|
|
|
|
|
|
|
|
|
| 16 |
]
|
| 17 |
+
HF_SERVERS = [
|
| 18 |
+
"https://6lqmgwrrrn-api1.hf.space",
|
| 19 |
+
"https://6lqmgwrrrn-api2.hf.space",
|
| 20 |
+
"https://6lqmgwrrrn-api3.hf.space",
|
| 21 |
+
"https://6lqmgwrrrn-api4.hf.space",
|
| 22 |
+
"https://6lqmgwrrrn-api5.hf.space",
|
| 23 |
+
"https://6lqmgwrrrn-api6.hf.space",
|
| 24 |
+
"https://6lqmgwrrrn-api7.hf.space",
|
| 25 |
+
"https://6lqmgwrrrn-api8.hf.space",
|
| 26 |
+
"https://dokek64685-api1.hf.space",
|
| 27 |
+
"https://dokek64685-api2.hf.space",
|
| 28 |
+
"https://dokek64685-api3.hf.space",
|
| 29 |
+
"https://dokek64685-api4.hf.space",
|
| 30 |
+
"https://dokek64685-api5.hf.space",
|
| 31 |
+
"https://dokek64685-api6.hf.space",
|
| 32 |
+
"https://dokek64685-api7.hf.space",
|
| 33 |
+
"https://dokek64685-api8.hf.space",
|
| 34 |
+
"https://sayonob407-api1.hf.space",
|
| 35 |
+
"https://sayonob407-api2.hf.space",
|
| 36 |
+
"https://sayonob407-api3.hf.space",
|
| 37 |
+
"https://sayonob407-api4.hf.space",
|
| 38 |
+
"https://sayonob407-api5.hf.space",
|
| 39 |
+
"https://sayonob407-api6.hf.space",
|
| 40 |
+
"https://sayonob407-api7.hf.space",
|
| 41 |
+
"https://sayonob407-api8.hf.space",
|
| 42 |
+
"https://Shadowty491-none1.hf.space",
|
| 43 |
+
"https://Shadowty491-none2.hf.space",
|
| 44 |
+
"https://Shadowty491-none3.hf.space",
|
| 45 |
+
"https://Shadowty491-none4.hf.space",
|
| 46 |
+
"https://Shadowty491-none5.hf.space",
|
| 47 |
+
"https://Shadowty491-none6.hf.space",
|
| 48 |
+
"https://Shadowty491-none7.hf.space",
|
| 49 |
+
"https://Shadowty491-none8.hf.space",
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
PING_INTERVAL = 120
|
| 53 |
REQUEST_TIMEOUT = 300
|
| 54 |
|
|
|
|
| 62 |
logger = logging.getLogger("proxy")
|
| 63 |
|
| 64 |
|
| 65 |
+
def pick_server() -> str:
|
| 66 |
+
return random.choice(HF_SERVERS)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def error_response(message: str, code: int = 500):
|
| 70 |
+
logger.error(message)
|
| 71 |
+
return jsonify({
|
| 72 |
+
"error": {
|
| 73 |
+
"message": message,
|
| 74 |
+
"type": "proxy_error"
|
| 75 |
+
}
|
| 76 |
+
}), code
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# =========================
|
| 80 |
+
# CHAT COMPLETIONS PROXY
|
| 81 |
+
# =========================
|
| 82 |
+
@app.route("/v1/chat/completions", methods=["POST"])
|
| 83 |
+
def chat_completions():
|
| 84 |
+
try:
|
| 85 |
+
payload = request.get_json(force=True)
|
| 86 |
+
except Exception:
|
| 87 |
+
return error_response("Invalid JSON payload", 400)
|
| 88 |
+
|
| 89 |
+
headers = dict(request.headers)
|
| 90 |
+
headers.pop("Host", None)
|
| 91 |
+
|
| 92 |
+
target = pick_server() + "/v1/chat/completions"
|
| 93 |
+
stream = payload.get("stream", False)
|
| 94 |
+
|
| 95 |
+
logger.info(f"Forwarding request to {target} | Stream={stream}")
|
| 96 |
+
|
| 97 |
+
try:
|
| 98 |
+
if stream:
|
| 99 |
+
def generate():
|
| 100 |
+
with requests.post(
|
| 101 |
+
target,
|
| 102 |
+
json=payload,
|
| 103 |
+
headers=headers,
|
| 104 |
+
stream=True,
|
| 105 |
+
timeout=REQUEST_TIMEOUT
|
| 106 |
+
) as res:
|
| 107 |
+
res.raise_for_status()
|
| 108 |
+
for line in res.iter_lines():
|
| 109 |
+
if line:
|
| 110 |
+
yield line + b"\n\n"
|
| 111 |
+
|
| 112 |
+
return Response(
|
| 113 |
+
generate(),
|
| 114 |
+
mimetype="text/event-stream",
|
| 115 |
+
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"}
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
res = requests.post(
|
| 119 |
+
target,
|
| 120 |
+
json=payload,
|
| 121 |
+
headers=headers,
|
| 122 |
+
timeout=REQUEST_TIMEOUT
|
| 123 |
+
)
|
| 124 |
+
res.raise_for_status()
|
| 125 |
+
return jsonify(res.json())
|
| 126 |
+
|
| 127 |
+
except requests.exceptions.Timeout:
|
| 128 |
+
return error_response("Upstream server timeout", 504)
|
| 129 |
+
except requests.exceptions.ConnectionError:
|
| 130 |
+
return error_response("Failed to connect to upstream server", 502)
|
| 131 |
+
except Exception as e:
|
| 132 |
+
return error_response(str(e), 500)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
# =========================
|
| 136 |
+
# MODELS ROUTE
|
| 137 |
+
# =========================
|
| 138 |
+
@app.route("/models", methods=["GET"])
|
| 139 |
+
def models():
|
| 140 |
+
url = pick_server()
|
| 141 |
+
target = f"{url}/models"
|
| 142 |
+
|
| 143 |
+
logger.info(f"Fetching models from {target}")
|
| 144 |
+
|
| 145 |
+
try:
|
| 146 |
+
res = requests.get(target, timeout=20)
|
| 147 |
+
res.raise_for_status()
|
| 148 |
+
return jsonify(res.json())
|
| 149 |
+
except Exception as e:
|
| 150 |
+
return error_response(f"Failed to fetch models: {e}", 500)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
# =========================
|
| 154 |
# HEALTH CHECK
|
| 155 |
# =========================
|
|
|
|
| 167 |
def keep_alive_worker():
|
| 168 |
while True:
|
| 169 |
logger.info("Pinging HF servers...")
|
| 170 |
+
for url in HF_SERVERS + Server:
|
| 171 |
try:
|
| 172 |
r = requests.get(url, headers=HEADERS, timeout=8)
|
| 173 |
logger.info(f"{url} → {r.status_code}")
|