davsclaus opened a new pull request, #29037:
URL: https://github.com/apache/camel/pull/29037

   Fixes [CAMEL-26211](https://issues.apache.org/jira/browse/CAMEL-26211).
   
   ### 1. A cv or audio body without a converter reached the predictor as null
   
   `AbstractCvZooPredictor`, `CustomCvPredictor`, `ZooAudioPredictor` and 
`CustomAudioPredictor` read the body with `getBody(Image.class)` / 
`getBody(Audio.class)` and caught `TypeConversionException`. For a String, Map, 
`BufferedImage` or null body there is no converter, so `getBody` returned null 
and the model failed later with "Could not process input or output".
   
   They now use `getMandatoryBody` and fail with "Data type is not supported. 
Body should be ...", with the conversion failure as the cause. A corrupt image 
that the converter cannot read also gets this message, now with its cause.
   
   ### 2. nlp predictors accepted only a String body
   
   `AbstractNlpZooPredictor` and `CustomNlpPredictor` checked `getBody() 
instanceof String`. They now use `getMandatoryBody(String.class)`, so a 
`byte[]` or `InputStream` body (from file, HTTP, JMS) works. A null body still 
fails with "Data type is not supported. Body should be String, byte[] or 
InputStream".
   
   ### 3. Custom predictors looked up the model and translator per message
   
   The seven `Custom*Predictor` classes now extend a new 
`AbstractCustomPredictor`. It looks up the model and translator once, at the 
first message, and keeps them until the producer stops. A missing bean fails 
with a `NoSuchBeanException` that names the bean, instead of a 
`NullPointerException`. An endpoint without the `model` or `translator` option 
fails when the producer starts.
   
   **Not done as proposed in the issue:** the beans are not looked up when the 
producer starts. A route with `autoStartup(false)` still has its producers 
started when the context starts. The module's own 
`CvImageClassificationLocalTest`, `CvImageEnhancementLocalTest` and 
`AudioLocalTest` bind the model after the context started, and so would users 
following that pattern. A lookup at producer start breaks that (I tried it: 
`CvImageClassificationLocalTest` failed with `NoSuchBeanException`). See the 
open question below.
   
   ### Tests
   
   New `PredictorBodyTest`: 10 tests. A mocked DJL `Model`, `ZooModel` and 
`Predictor` are used, so no model is loaded or downloaded. `ModelZoo.loadModel` 
is mocked statically for `ZooAudioPredictor`. 9 of them fail without the fix; 
the 10th (an image given as bytes is converted) guards the working path.
   
   Also run locally: `DJLConverterTest`, `DJLProducerTest`, `ImageNetUtilTest`, 
`ModelPredictorProducerTest` (the two tests that do not download), 
`CvImageClassificationLocalTest` (custom cv end to end, late binding), 
`CvImageClassificationTest` and `NlpSentimentAnalysisTest` (zoo cv and nlp end 
to end with cached models): 20 tests, all pass. The other tests of the module 
download zoo models and were not run here. On aarch64 the module skips its 
tests by default, so I ran them with `-DskipTests.aarch64=false`.
   
   ### Docs and generated files
   
   - `@BodyTypes`: the nlp applications now list text, bytes and stream, and 
the description no longer says that bytes or a stream fail. `djl.json` 
regenerated and copied to the catalog. No yaml-dsl-validator test references 
djl.
   - `djl-component.adoc`: the nlp input types, and a note on when the custom 
model and translator are looked up. Catalog copy updated.
   - Upgrade guide 4.24: a `camel-djl` entry. The beans are looked up once, so 
a bean bound again under the same name is used only after a route restart. A 
missing bean gives a `NoSuchBeanException`. The nlp applications take bytes and 
streams.
   - `mockito-core` added as a test dependency.
   
   ### Open question
   
   Should the model and translator be looked up when the producer starts, and 
fail the route startup, as the issue proposes? My recommendation is to keep the 
lookup at the first message: failing at startup would break binding the model 
after the context started, which the module's own tests do.
   
   _Claude Code on behalf of Claus Ibsen (davsclaus)_
   
   🤖 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]

Reply via email to