Download tests/test_demo.py from RightNowAI/RightNow-Mini: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/RightNowAI/RightNow-Mini/resolve/main/tests/test_demo.py
- Command line
-
hf download hf://RightNowAI/RightNow-Mini/tests/test_demo.py
-
curl -L -o test_demo.py https://huggingface.co/RightNowAI/RightNow-Mini/resolve/main/tests/test_demo.py
2.92 kB
| """The demo server against a fake model: no GPU, no key needed. python -m tests.test_demo""" | |
| import asyncio | |
| import json | |
| import os | |
| from aiohttp import web | |
| from aiohttp.test_utils import TestClient, TestServer | |
| def fake_model(seen): | |
| async def chat(request): | |
| body = await request.json() | |
| seen.append({"auth": request.headers.get("Authorization", ""), "model": body["model"], "max_tokens": body["max_tokens"]}) | |
| resp = web.StreamResponse(headers={"Content-Type": "text/event-stream"}) | |
| await resp.prepare(request) | |
| for piece in ("def ", "merge", "():"): | |
| await resp.write(("data: " + json.dumps({"choices": [{"delta": {"content": piece}}]}) + "\n\n").encode()) | |
| await resp.write(("data: " + json.dumps({"choices": [], "usage": {"completion_tokens": 3}}) + "\n\ndata: [DONE]\n\n").encode()) | |
| return resp | |
| app = web.Application() | |
| app.router.add_post("/v1/chat/completions", chat) | |
| return app | |
| async def run(): | |
| seen = [] | |
| up = TestServer(fake_model(seen)) | |
| await up.start_server() | |
| url = str(up.make_url("")).rstrip("/") | |
| os.environ.update(MINI_URL=url, MINI_MODEL="rightnow-mini", OTHER_URL=url, OTHER_MODEL="other/model", OTHER_KEY_ENV="DEMO_TEST_KEY", | |
| DEMO_TEST_KEY="not-a-real-key") | |
| from demo import server as demo | |
| import argparse | |
| client = TestClient(TestServer(await demo.make_app(argparse.Namespace(max_tokens=64, daily_cap=3)))) | |
| await client.start_server() | |
| page = await client.get("/") | |
| info = await (await client.get("/info")).json() | |
| texts = {} | |
| for side in ("mini", "other"): | |
| r = await client.post("/stream/" + side, json={"task": "merge intervals"}) | |
| texts[side] = await r.text() | |
| empty = await client.post("/stream/mini", json={"task": " "}) | |
| capped = await client.post("/stream/mini", json={"task": "again"}) | |
| capped2 = await client.post("/stream/mini", json={"task": "again"}) | |
| html = await page.text() | |
| await client.close() | |
| await up.close() | |
| return html, info, texts, seen, empty.status, capped.status, capped2.status | |
| def main(): | |
| html, info, texts, seen, empty, c1, c2 = asyncio.run(run()) | |
| assert "RightNow Mini, side by side" in html and info == {"mini": "rightnow-mini", "other": "other/model", "max_tokens": 64} | |
| assert all("merge" in t and "[DONE]" in t for t in texts.values()) | |
| assert seen[0]["auth"] == "" and seen[0]["model"] == "rightnow-mini" and seen[1]["auth"] == "Bearer not-a-real-key" and seen[1]["max_tokens"] == 64 | |
| assert "not-a-real-key" not in html and "not-a-real-key" not in json.dumps(info) and all("not-a-real-key" not in t for t in texts.values()) | |
| assert empty == 400 and c1 == 200 and c2 == 429 | |
| print("ok the page loads, both sides stream, the other model's key goes upstream only, an empty task is refused, the daily cap holds") | |
| if __name__ == "__main__": | |
| main() | |