CLI Components¶
This page documents the command-line interface components of AniSearch Model.
Overview¶
The CLI package provides the command-line interface functionality for AniSearch Model, including:
- Argument parsing
- Interactive mode
- Command handling
Argument Parser¶
Defines and processes all command-line arguments:
Command-Line Argument Parser¶
A robust argument parsing system for the anime/manga search and training commands.
This module provides command-line argument parsing functionality for the application, supporting both search and training operations for anime and manga datasets. It defines the available arguments, their defaults, help text, and handles validation of argument combinations.
Features¶
- Subcommand structure with 'search' and 'train' commands
- Comprehensive argument validation to prevent invalid combinations
- Support for interactive and batch search modes
- Extensive configuration options for model training
- Listing capabilities for pre-trained and fine-tuned models
Command Structure¶
The CLI supports two main commands:
-
search: Search for anime/manga using natural language descriptions
-
train: Train custom cross-encoder models on anime/manga datasets
Default Behavior¶
For backward compatibility, if no command is specified, the system defaults to 'search' mode.
parse_args ¶
Parse command line arguments for the anime/manga search and training system.
This function sets up the argument parser with two main subcommands:
- 'search': For finding anime/manga using natural language descriptions
- 'train': For training custom cross-encoder models on the datasets
Each subcommand has its own set of options and arguments. The function handles backward compatibility by defaulting to 'search' if no command is provided.
RETURNS | DESCRIPTION |
---|---|
Namespace | argparse.Namespace: The parsed and validated command line arguments. Contains all the options and arguments specified by the user, or their default values if not explicitly provided. |
Notes
- For backward compatibility, if no command is provided, 'search' is assumed
- The 'search' command supports interactive and batch modes
- The 'train' command has extensive options for model training
- All arguments are validated by calling validate_args() internally
Argument Structure:
Common Arguments:¶
--type: Dataset type ('anime' or 'manga')
--model: Model name or path
--batch-size: Batch size for processing
--list-models: List available pre-trained models
--list-fine-tuned: List available fine-tuned models
Search Command Arguments:¶
--query: Description to search for
--results: Number of results to return
--interactive (-i): Enter interactive mode
--include-light-novels: Include light novels in manga results
Train Command Arguments:¶
--epochs: Number of training epochs
--eval-steps: Steps between evaluations
--learning-rate: Learning rate for training
--max-samples: Maximum number of training samples
--labeled-data: Path to labeled data CSV
--create-labeled-data: Create synthetic labeled data
--seed: Random seed for reproducibility
--loss: Loss function for training
--scheduler: Learning rate scheduler
Example
Source code in src/cli/args.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
|
validate_args ¶
Validate command line arguments for consistency and correctness.
This function ensures that argument combinations are valid and compatible. It performs checks such as:
- Ensuring the 'type' argument is provided when required
- Validating that certain arguments are only used with appropriate dataset types (e.g., --include-light-novels only with manga datasets)
PARAMETER | DESCRIPTION |
---|---|
args | The parsed command line arguments to validate. This is typically the output from argparse.ArgumentParser.parse_args(). TYPE: |
RETURNS | DESCRIPTION |
---|---|
Namespace | argparse.Namespace: The validated arguments, unchanged if validation passes. |
RAISES | DESCRIPTION |
---|---|
SystemExit | If validation fails, the program exits with an error message. The error message is printed to stderr before exiting. |
Notes
- This function is called automatically by parse_args()
- Some validation rules are specific to certain commands or dataset types
- The function provides user-friendly error messages to help diagnose issues
Example
Source code in src/cli/args.py
Interactive Mode¶
Provides interactive search capabilities:
Interactive Search Mode¶
A command-line interface for interactive semantic search of anime and manga.
This module provides an interactive command-line interface for searching anime and manga using semantic search models. It allows users to enter natural language descriptions and displays formatted search results with titles, IDs, and synopsis excerpts.
Features¶
- Interactive query input with continuous search capability
- Formatted display of search results with relevance scores
- Error handling with graceful error recovery
- Customizable number of results and processing batch size
- Support for both anime and manga search models
Usage¶
The interactive mode can be started from the main CLI with:
# For anime search
python -m src.main search --interactive
# For manga search
python -m src.main search --type manga --interactive
Once in interactive mode, users can enter descriptive queries and receive ranked results until they choose to exit the session.
interactive_mode ¶
interactive_mode(search_model: BaseSearchModel, num_results: int, batch_size: int) -> None
Run the search model in an interactive command-line session.
This function starts an interactive search session where users can repeatedly enter queries and see results until they choose to exit. It handles the input loop, input validation, search execution, and session termination.
The interactive session continues until the user types 'exit' or 'quit'. Each valid query is processed by the search_and_display_results function, which executes the search and formats the output.
PARAMETER | DESCRIPTION |
---|---|
search_model | An initialized search model instance that will perform the searches. This determines whether anime or manga content will be searched. TYPE: |
num_results | The maximum number of results to return for each query. This value is passed to search_and_display_results for each search. TYPE: |
batch_size | The batch size to use when processing search pairs with the model. This value is passed to search_and_display_results for each search. TYPE: |
RETURNS | DESCRIPTION |
---|---|
None | This function runs an interactive session but doesn't return any values. TYPE: |
Example
Notes
- Empty queries are validated and rejected with a message
- The function displays which type of dataset is being searched (anime/manga)
- Error handling for the search process is managed by search_and_display_results
Source code in src/cli/interactive.py
search_and_display_results ¶
search_and_display_results(search_model: BaseSearchModel, query: str, num_results: int, batch_size: int) -> None
Execute a search query and display formatted results to the console.
This function performs a semantic search using the provided search model and displays the results in a user-friendly format. It formats each result with a title, ID, normalized score, and synopsis excerpt.
The function is decorated with error handling to gracefully handle any exceptions that might occur during the search process or result formatting.
PARAMETER | DESCRIPTION |
---|---|
search_model | An initialized search model instance that will perform the search. This can be any class that extends BaseSearchModel (like AnimeSearchModel or MangaSearchModel). TYPE: |
query | The text description or query to search for. This should be a natural language description of the content the user is looking for. TYPE: |
num_results | The maximum number of results to return and display. This determines how many top matches will be shown to the user. TYPE: |
batch_size | The batch size to use when processing search pairs with the model. Larger batch sizes can improve performance but require more memory. TYPE: |
RETURNS | DESCRIPTION |
---|---|
None | This function prints results to the console but does not return any values. TYPE: |
Notes
- The function truncates synopsis excerpts to 200 characters for display
- Scores are formatted differently based on the model type using the format_score utility
- Results are numbered starting from 1 for user-friendly display
- Error handling is provided by the @handle_exceptions decorator