abhishekgahlot commited on
Commit
f2db5ff
·
verified ·
1 Parent(s): 6b45f45

OpenJev 1.1: full card, figures, helper and licence files

Browse files
Files changed (2) hide show
  1. helper/SERVE.md +2 -2
  2. helper/compat.py +9 -0
helper/SERVE.md CHANGED
@@ -50,7 +50,7 @@ Three of the flags are load-bearing for the helper, not tuning:
50
 
51
  `helper/` holds two files:
52
  - `shim.py` asks the model and reads the answer from the option scores. Its sha256 is `f35b7272906e6e3b2a75e05c233b52b412f7d3c533c689b5e3ded593df99ff3f`.
53
- - `compat.py` runs in front of it and adds the request features of the hosted Jev API (table on the model card). It imports `shim.py` from its own folder and hands every request it accepts to the helper unchanged, so the answers are the helper's. Its sha256 is `1ee46f50925f9a0c74bff014aba690d9e0879ae77ee59ede2b0caa7bb6cfaca7`.
54
 
55
  Run one of them. With compat in front and read twice on:
56
 
@@ -69,7 +69,7 @@ python helper/compat.py --host 127.0.0.1 --port 3000
69
 
70
  Leave out `READOUT_REPEAT=1` for one read, the faster setting. Put `helper/shim.py` in place of `helper/compat.py` for the bare helper: the same answers, without request compression, `assets`, `GET /v1/models` or JSON bodies on every error. The numbers on the model cards were measured with `shim.py` called directly; `compat.py` hands those requests to it byte for byte.
71
 
72
- This binds the helper to loopback: only processes on the same machine can call it. Every variable is written as an assignment in front of the command, which puts it into the helper process's environment whether or not it is exported in your shell. Setting the same variables on separate lines without `export` would leave the helper running on its file defaults. Both files need the `openai` and `transformers` Python packages; `compat.py` also needs Python 3.14 or the `zstandard` package to read zstd bodies, and answers 415 to them otherwise. Without `--host` / `--port` either file binds `127.0.0.1:8765`.
73
 
74
  ### Environment knobs
75
 
 
50
 
51
  `helper/` holds two files:
52
  - `shim.py` asks the model and reads the answer from the option scores. Its sha256 is `f35b7272906e6e3b2a75e05c233b52b412f7d3c533c689b5e3ded593df99ff3f`.
53
+ - `compat.py` runs in front of it and adds the request features of the hosted Jev API (table on the model card). It imports `shim.py` from its own folder and hands every request it accepts to the helper unchanged, so the answers are the helper's. Its sha256 is `58b7d76465c835244c71dfc44736bd347cde42b88bd24c8a6820e42b86757aca`.
54
 
55
  Run one of them. With compat in front and read twice on:
56
 
 
69
 
70
  Leave out `READOUT_REPEAT=1` for one read, the faster setting. Put `helper/shim.py` in place of `helper/compat.py` for the bare helper: the same answers, without request compression, `assets`, `GET /v1/models` or JSON bodies on every error. The numbers on the model cards were measured with `shim.py` called directly; `compat.py` hands those requests to it byte for byte.
71
 
72
+ This binds the helper to loopback: only processes on the same machine can call it. While no `SHIM_TOKEN` is set, `compat.py` also refuses browser requests that come from another site (`Sec-Fetch-Site: cross-site`), so a web page you visit cannot make your browser call the helper; SDKs and curl never send that header. Every variable is written as an assignment in front of the command, which puts it into the helper process's environment whether or not it is exported in your shell. Setting the same variables on separate lines without `export` would leave the helper running on its file defaults. Both files need the `openai` and `transformers` Python packages; `compat.py` also needs Python 3.14 or the `zstandard` package to read zstd bodies, and answers 415 to them otherwise. Without `--host` / `--port` either file binds `127.0.0.1:8765`.
73
 
74
  ### Environment knobs
75
 
helper/compat.py CHANGED
@@ -185,6 +185,11 @@ class H(S.H):
185
  def unauthorized(self):
186
  return bool(S.SHIM_TOKEN) and self.headers.get("Authorization", "") != "Bearer " + S.SHIM_TOKEN
187
 
 
 
 
 
 
188
  def send_response(self, code, message=None):
189
  self.answered = True
190
  super().send_response(code, message)
@@ -208,6 +213,8 @@ class H(S.H):
208
  n -= got
209
  return self.fail(413, "the body is over 32 MiB")
210
  raw = self.rfile.read(n) # read before any answer: an unread body would be parsed as the next request on a kept-alive connection
 
 
211
  if self.unauthorized():
212
  return self.fail(401, "missing or invalid bearer token")
213
  try:
@@ -247,6 +254,8 @@ class H(S.H):
247
  self.rfile = real
248
 
249
  def do_GET(self):
 
 
250
  if self.path.split("?")[0].rstrip("/").endswith("/v1/models"):
251
  if self.unauthorized():
252
  return self.fail(401, "missing or invalid bearer token")
 
185
  def unauthorized(self):
186
  return bool(S.SHIM_TOKEN) and self.headers.get("Authorization", "") != "Bearer " + S.SHIM_TOKEN
187
 
188
+ def cross_site(self):
189
+ """a browser request from another site while no SHIM_TOKEN is set: without this a web page could make its visitor's browser
190
+ post to a helper on their machine (multipart is a CORS simple request, so no preflight stops it); SDKs never send the header"""
191
+ return not S.SHIM_TOKEN and self.headers.get("Sec-Fetch-Site", "").lower() == "cross-site"
192
+
193
  def send_response(self, code, message=None):
194
  self.answered = True
195
  super().send_response(code, message)
 
213
  n -= got
214
  return self.fail(413, "the body is over 32 MiB")
215
  raw = self.rfile.read(n) # read before any answer: an unread body would be parsed as the next request on a kept-alive connection
216
+ if self.cross_site():
217
+ return self.fail(403, "cross-site browser requests are refused while no SHIM_TOKEN is set")
218
  if self.unauthorized():
219
  return self.fail(401, "missing or invalid bearer token")
220
  try:
 
254
  self.rfile = real
255
 
256
  def do_GET(self):
257
+ if self.cross_site():
258
+ return self.fail(403, "cross-site browser requests are refused while no SHIM_TOKEN is set")
259
  if self.path.split("?")[0].rstrip("/").endswith("/v1/models"):
260
  if self.unauthorized():
261
  return self.fail(401, "missing or invalid bearer token")