-
-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ml): predict reading level during epub import #1822
feat(ml): predict reading level during epub import #1822
Conversation
WalkthroughThe recent changes introduce a feature in the Changes
Assessment against linked issues
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Outside diff range, codebase verification and nitpick comments (1)
src/main/java/ai/elimu/web/content/storybook/StoryBookCreateFromEPubController.java (1)
529-567
: Enhance Error Handling inpredictReadingLevel
MethodThe
predictReadingLevel
method currently lacks error handling for potential issues such as file not found or model prediction errors. Consider adding try-catch blocks to handle exceptions related to file access and model prediction to ensure robustness. This will prevent the application from crashing and allow for graceful error handling.
- Add error handling for file access issues when loading the model.
- Include error handling for potential prediction errors or invalid input features.
Analysis chain
Review the
predictReadingLevel
method for correctness and best practices.The method loads a PMML model and uses it to predict the reading level of a
StoryBook
. Ensure that:
- The model file path is correctly resolved.
- Input features are correctly prepared and match the model's expectations.
- Predictions are correctly interpreted and mapped to the
ReadingLevel
enum.- Consider adding error handling for potential issues like file not found or model prediction errors.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the correctness of the `predictReadingLevel` method. # Test: Ensure the model file path is resolved correctly. rg 'getResource("step2_2_model.pmml")' -A 3 src/main/java/ai/elimu/web/content/storybook/StoryBookCreateFromEPubController.java # Test: Check if the `predictReadingLevel` method is called correctly in the codebase. ast-grep --lang java --pattern $'predictReadingLevel($_)' src/main/java/ai/elimu/web/content/storybook/StoryBookCreateFromEPubController.javaLength of output: 441
Script:
#!/bin/bash # Check for error handling related to file access and model prediction in the predictReadingLevel method. rg 'try|catch|exception' -A 5 src/main/java/ai/elimu/web/content/storybook/StoryBookCreateFromEPubController.javaLength of output: 2760
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1822 +/- ##
============================================
- Coverage 15.19% 15.10% -0.09%
Complexity 450 450
============================================
Files 249 249
Lines 7627 7672 +45
Branches 798 800 +2
============================================
Hits 1159 1159
- Misses 6418 6463 +45
Partials 50 50 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
private ReadingLevel predictReadingLevel(int chapterCount, int paragraphCount, int wordCount) { | ||
logger.info("predictReadingLevel"); | ||
|
||
// Load the machine learning model (https://github.com/elimu-ai/ml-storybook-reading-level) | ||
String modelFilePath = getClass().getResource("step2_2_model.pmml").getFile(); | ||
logger.info("modelFilePath: " + modelFilePath); | ||
org.pmml4s.model.Model model = org.pmml4s.model.Model.fromFile(modelFilePath); | ||
logger.info("model: " + model); | ||
|
||
// Prepare values (features) to pass to the model | ||
Map<String, Double> values = Map.of( | ||
"chapter_count", Double.valueOf(chapterCount), | ||
"paragraph_count", Double.valueOf(paragraphCount), | ||
"word_count", Double.valueOf(wordCount) | ||
); | ||
logger.info("values: " + values); | ||
|
||
// Make prediction | ||
logger.info("Arrays.toString(model.inputNames()): " + Arrays.toString(model.inputNames())); | ||
Object[] valuesMap = Arrays.stream(model.inputNames()) | ||
.map(values::get) | ||
.toArray(); | ||
logger.info("valuesMap: " + valuesMap); | ||
Object[] results = model.predict(valuesMap); | ||
logger.info("results: " + results); | ||
logger.info("Arrays.toString(results): " + Arrays.toString(results)); | ||
Object result = results[0]; | ||
logger.info("result: " + result); | ||
logger.info("result.getClass().getSimpleName(): " + result.getClass().getSimpleName()); | ||
Double resultAsDouble = (Double) result; | ||
logger.info("resultAsDouble: " + resultAsDouble); | ||
Integer resultAsInteger = resultAsDouble.intValue(); | ||
logger.info("resultAsInteger: " + resultAsInteger); | ||
|
||
// Convert from number to ReadingLevel enum (e.g. "LEVEL2") | ||
String readingLevelAsString = "LEVEL" + resultAsInteger; | ||
logger.info("readingLevelAsString: " + readingLevelAsString); | ||
ReadingLevel readingLevel = ReadingLevel.valueOf(readingLevelAsString); | ||
logger.info("readingLevel: " + readingLevel); | ||
return readingLevel; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enhance error handling and logging.
The predictReadingLevel
method effectively handles the prediction process. However, consider adding error handling for potential issues like file not found or invalid model predictions. Additionally, improve logging by including more context about the prediction process.
try {
org.pmml4s.model.Model model = org.pmml4s.model.Model.fromFile(modelFilePath);
// Existing logic
} catch (FileNotFoundException e) {
logger.error("Model file not found: " + modelFilePath, e);
throw new RuntimeException("Model file not found", e);
} catch (Exception e) {
logger.error("Error during model prediction", e);
throw new RuntimeException("Error during model prediction", e);
}
…torybook-reading-level
Resolves #1821
Relates to elimu-ai/ml-storybook-reading-level#15