davsclaus opened a new pull request, #31027: URL: https://github.com/apache/camel/pull/31027
JIRA: https://issues.apache.org/jira/browse/CAMEL-27671 ## What was wrong `AbstractTaskPredictor.loadModel` writes a Python handler (`handler.py`) into a temporary directory and loads it with the DJL Python engine. - The `modelId`, `revision` and `device` values were put into templates like `model='%s'` without any escaping. A value with a quote, a backslash or a newline gave a broken handler (or a different value than configured). - `withAuthToken` wrote the `authToken` into the handler as `os.environ['HF_TOKEN'] = '...'` (it escaped the quote, not the backslash), so the token was stored in clear text in a file on disk while the producer ran. - The script was formatted with the default locale, so with a decimal-comma locale the handler got `'temperature': 0,700000`. - When the model failed to load, the temporary directory stayed on disk. When the producer stopped, it was already removed. ## What changed - New `AbstractTaskPredictor.pythonString(String)` writes a value as a Python string literal. It escapes the backslash and the single quote, and writes every character that is not printable ASCII as `\xNN`, `\uNNNN` or `\UNNNNNNNN` escapes. A `null` value becomes `None`. The ten task templates now use `%s` without quotes, and every predictor passes its values through `pythonString`. - `loadPythonScript` formats with `Locale.ROOT`. - The token is now passed to the Python process as the `HF_TOKEN` environment variable, using the `env` option of the DJL Python engine (`transformers` and `huggingface_hub` read `HF_TOKEN`). `withAuthToken` is removed. The `env` option is a comma-separated list, so a token with a comma (or a control character) now fails at startup with a message that does not show the token. - If `loadModel` fails, it deletes the temporary directory. `close()` deletes it even when closing the model fails. The directory is still created with `rwx------` on POSIX systems. ## Tests No Python runtime or network is needed (the existing `HuggingFaceIT` is skipped as before). - `AuthTokenInjectionTest` was rewritten (4 tests): the token is in the `env` option as `HF_TOKEN=...`, there is no `env` option without a token, the token is not in `handler.py` or `requirements.txt`, and a token with a comma fails. - New `HandlerValuesTest` (6 tests): - the literal escaping (backslash, quote, newline, CR, tab, Latin-1, BMP, supplementary) - a value with backslash, quote and newline stays inside its literal and adds no line - all ten tasks write the values as literals - the temperature is written with a dot under `Locale.GERMANY` (with `@ResourceLock(Resources.LOCALE)`, and the locale is restored) - `close()` removes the directory - a failing `loadModel` removes the directory, which had `rwx------` All 10 tests fail on the code before the fix. On `closeRemovesTheModelDirectory`, only the assertion that `tmpDir` is cleared fails, because the directory was already removed on stop. With the fix, all 54 unit tests of the module pass. I also checked by hand that all ten templates, filled with an escaped value, parse with Python's `ast`. ## Docs - `huggingface-component.adoc` (and its catalog copy) has a new short "Authentication" section: where the handler is written, that the token goes in as `HF_TOKEN`, and that it must not contain a comma. - Upgrade guide 4.24: new entry `camel-huggingface - the token is given to the Python process as an environment variable`. It covers the token with a comma failing, the removed `withAuthToken`, and the escaped values. Note: DJL's `PyModel` logs every engine option at DEBUG level, including `env`. So with DEBUG enabled for `ai.djl.python.engine`, the token shows up in that log. No other DJL option reaches the Python process environment. _Claude Code on behalf of Claus Ibsen_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
