redkits commited on
Commit
76c2cd2
·
verified ·
1 Parent(s): 1bdf1c9

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +6 -0
  2. README.md +5 -4
  3. app.py +87 -0
Dockerfile ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+ RUN pip install --no-cache-dir fastapi==0.115.6 uvicorn==0.34.0 httpx==0.28.1
3
+ WORKDIR /app
4
+ COPY app.py .
5
+ EXPOSE 7860
6
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--proxy-headers"]
README.md CHANGED
@@ -1,10 +1,11 @@
1
  ---
2
  title: Looper Router
3
- emoji: ⚡
4
- colorFrom: yellow
5
- colorTo: yellow
6
  sdk: docker
 
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Looper Router
3
+ emoji: 🔁
4
+ colorFrom: blue
5
+ colorTo: gray
6
  sdk: docker
7
+ app_port: 7860
8
  pinned: false
9
  ---
10
 
11
+ OpenAI-compatible router: `looper` and `looper flash` to their HF Inference Endpoints.
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import time
4
+
5
+ import httpx
6
+ from fastapi import FastAPI, Request
7
+ from fastapi.responses import JSONResponse, Response, StreamingResponse
8
+
9
+ HF_TOKEN = os.environ["HF_TOKEN"]
10
+ ROUTER_KEY = os.environ["ROUTER_KEY"]
11
+
12
+ ROUTES = {
13
+ "looper": {
14
+ "base": "https://6ac9858de4d5812f4c1a9f75.endpoints.huggingface.cloud/v1",
15
+ "model": "s-zaizen/DeepSeek-V4.1-Flash-Abliterated",
16
+ },
17
+ "looper flash": {
18
+ "base": "https://6ac6f9bf904ec5f3ba7f4e6e.endpoints.huggingface.cloud/v1",
19
+ "model": "huihui-ai/Huihui-GLM-5.3-Flash-abliterated-GGUF",
20
+ },
21
+ }
22
+ DEFAULT = "looper"
23
+
24
+ app = FastAPI()
25
+ client = httpx.AsyncClient(timeout=httpx.Timeout(600.0, connect=30.0))
26
+
27
+
28
+ def resolve(name):
29
+ key = (name or "").strip().lower().replace("-", " ").replace("_", " ")
30
+ key = " ".join(key.split())
31
+ return ROUTES.get(key, ROUTES[DEFAULT])
32
+
33
+
34
+ def authorized(request: Request):
35
+ auth = request.headers.get("authorization", "")
36
+ return auth.removeprefix("Bearer ").strip() == ROUTER_KEY
37
+
38
+
39
+ @app.get("/")
40
+ async def root():
41
+ return {"status": "ok", "models": list(ROUTES)}
42
+
43
+
44
+ @app.get("/v1/models")
45
+ async def models():
46
+ now = int(time.time())
47
+ return {"object": "list", "data": [
48
+ {"id": name, "object": "model", "created": now, "owned_by": "looper"} for name in ROUTES
49
+ ]}
50
+
51
+
52
+ @app.post("/v1/chat/completions")
53
+ async def chat(request: Request):
54
+ if not authorized(request):
55
+ return JSONResponse({"error": {"message": "Invalid API key", "type": "invalid_request_error"}}, 401)
56
+ body = await request.json()
57
+ requested = body.get("model")
58
+ route = resolve(requested)
59
+ body["model"] = route["model"]
60
+ headers = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
61
+ upstream = client.build_request("POST", route["base"] + "/chat/completions", json=body, headers=headers)
62
+ resp = await client.send(upstream, stream=True)
63
+
64
+ if not body.get("stream"):
65
+ content = await resp.aread()
66
+ await resp.aclose()
67
+ try:
68
+ data = json.loads(content)
69
+ if isinstance(data, dict) and "choices" in data:
70
+ data["model"] = requested
71
+ return JSONResponse(data, resp.status_code)
72
+ except ValueError:
73
+ return Response(content, resp.status_code, media_type=resp.headers.get("content-type"))
74
+
75
+ async def relay():
76
+ try:
77
+ async for chunk in resp.aiter_raw():
78
+ yield chunk
79
+ finally:
80
+ await resp.aclose()
81
+
82
+ return StreamingResponse(
83
+ relay(),
84
+ status_code=resp.status_code,
85
+ media_type=resp.headers.get("content-type", "text/event-stream"),
86
+ headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
87
+ )