Inference Backends¶
All backends share the same one_inference / batch_inference interface defined in
[Inference][urbanworm.inference.Inference.Inference].
Unsloth (recommended)¶
GPU-accelerated local VLM inference via Unsloth's FastVisionModel.
Automatically spreads the model across all visible GPUs and retries
OOM-failed chunks item-by-item.
Install: pip install "urban-worm[unsloth]" (pre-install CUDA torch first)
urbanworm.inference.unsloth.InferenceUnsloth(llm=None, load_in_4bit=True, max_seq_length=4096, device=None, max_memory=None, model_dir=None, dtype=None, disable_compile=True, skip_errors=True, **kwargs)
¶
Bases: Inference
Vision-language model inference via Unsloth's FastVisionModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
str | None
|
Unsloth-compatible model id or local path. Defaults to
|
None
|
load_in_4bit
|
bool
|
Load weights in 4-bit (bitsandbytes). Big VRAM win,
small quality cost. Default |
True
|
max_seq_length
|
int
|
Maximum tokenized prompt+generation length passed to
|
4096
|
device
|
str | None
|
Override the device map string (e.g. |
None
|
max_memory
|
dict | None
|
Per-device VRAM budget passed to |
None
|
model_dir
|
str | None
|
Local directory where HuggingFace model weights are cached.
Passed as |
None
|
dtype
|
Any
|
Override the compute dtype. |
None
|
disable_compile
|
bool
|
Disable Unsloth / Torch auto-compilation before the
model is loaded. Default |
True
|
skip_errors
|
bool
|
If |
True
|
**kwargs
|
Forwarded to :class: |
{}
|
Source code in urbanworm/inference/unsloth.py
Functions¶
one_inference(system='', prompt='', image=None, audio=None, temp=0.0, top_k=20, top_p=0.8, max_new_tokens=512)
¶
Run inference on a single image (or list of images for one prompt).
Returns a one-row DataFrame in the same shape as
:meth:InferenceOllama.one_inference.
Source code in urbanworm/inference/unsloth.py
batch_inference(system='', prompt='', temp=0.0, top_k=20, top_p=0.8, max_new_tokens=512, batch_size=1, task_chunk_size=None, disableProgressBar=False, checkpoint_path=None, failed_log_path=None)
¶
Run inference over self.batch_images with optional GPU batching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Number of items per |
1
|
task_chunk_size
|
int | None
|
Logical job partition size, independent of
|
None
|
checkpoint_path
|
str | None
|
Path to a JSONL file for resume-safe checkpointing. Already-completed items are skipped on the next run. |
None
|
failed_log_path
|
str | None
|
Optional path to a CSV file where permanently failed sample indices and error messages are appended. Lets you rerun only the failures later. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame, same shape as :meth: |
Source code in urbanworm/inference/unsloth.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | |
to_df(output=True)
¶
Convert self.results into a DataFrame. Mirrors InferenceOllama.
Source code in urbanworm/inference/unsloth.py
Ollama¶
Inference via a locally running Ollama server. No GPU required; any GGUF-backed vision model works.
Install: pip install "urban-worm[ollama]" + the Ollama app
urbanworm.inference.llama.InferenceOllama(llm=None, ollama_key=None, model_dir=None, **kwargs)
¶
Bases: Inference
Constructor for vision inference using MLLMs with Ollama.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
str
|
model checkpoint. |
None
|
ollama_key
|
str
|
The Ollama API key. |
None
|
model_dir
|
str
|
Directory where Ollama stores downloaded
models. Sets the |
None
|
**kwargs
|
image (str|list[str]|tuple[str]), images (list|tuple), data constructor (GeoTaggedData), and schema (dict) |
{}
|
Source code in urbanworm/inference/llama.py
Functions¶
one_inference(system='', prompt='', image=None, audio=None, temp=0.0, top_k=20.0, top_p=0.8, max_new_tokens=512)
¶
Chat with MLLM model with one image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
str
|
The system message. |
''
|
prompt
|
str
|
The prompt message. |
''
|
image
|
str | list[str] | tuple[str]
|
The image path. |
None
|
audio
|
str | list[str] | tuple[str]
|
The audio path. |
None
|
temp
|
float
|
The temperature value. |
0.0
|
top_k
|
int
|
The top_k value. |
20.0
|
top_p
|
float
|
The top_p value. |
0.8
|
max_new_tokens
|
int
|
Maximum number of tokens to generate. Default 512. |
512
|
Notes
Ollama currently does not support audio input.
The argument audio is just a placeholder for the future development.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
A dictionary includes questions/messages, responses/answers |
Source code in urbanworm/inference/llama.py
batch_inference(system='', prompt='', temp=0.0, top_k=20, top_p=0.8, max_new_tokens=512, disableProgressBar=False, checkpoint_path=None)
¶
Chat with MLLM model for each image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
str
|
The system message. |
''
|
prompt
|
str
|
The prompt message. |
''
|
temp
|
float
|
The temperature value. |
0.0
|
top_k
|
float
|
The top_k value. |
20
|
top_p
|
float
|
The top_p value. |
0.8
|
max_new_tokens
|
int
|
Maximum number of tokens to generate per call. Default 512. |
512
|
disableProgressBar
|
bool
|
The progress bar for showing the progress of data analysis over the units. |
False
|
checkpoint_path
|
str
|
Path to a JSONL file for resume-safe checkpointing. Already-completed items are skipped on the next run automatically. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
list A list of dictionaries. Each dict includes questions/messages, responses/answers, and image base64 (if required) |
Source code in urbanworm/inference/llama.py
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 | |
to_df(output=True)
¶
Convert the output from an MLLM reponse (from .batch_inference) into a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
bool
|
Whether to return a DataFrame. Defaults to True. |
True
|
Returns:
pd.DataFrame: A DataFrame containing responses and associated metadata.
str: An error message if .batch_inference() has not been run or if the format is unsupported.
Source code in urbanworm/inference/llama.py
llama.cpp¶
Inference via the llama-mtmd-cli binary. Supports audio input; highly
configurable sampling parameters.
Install: pip install "urban-worm[llamacpp]" + brew install llama.cpp
urbanworm.inference.llama.InferenceLlamacpp(llm=None, mp=None, model_dir=None, **kwargs)
¶
Bases: Inference
Constructor for vision inference using MLLMs with llama.cpp
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
str
|
model checkpoint to download (e.g.
|
None
|
mp
|
str
|
If |
None
|
model_dir
|
str
|
Directory used as |
None
|
**kwargs
|
image (str|list[str]|tuple[str]), images (list|tuple), data constructor (GeoTaggedData), and schema (dict) |
{}
|
Source code in urbanworm/inference/llama.py
Functions¶
one_inference(system='', prompt='', image=None, audio=None, temp=0.2, top_k=20, top_p=0.8, ctx_size=4096, max_new_tokens=512, audio_input=False)
¶
Chat with MLLM model with one image. Args: system (str, optional): The system message. prompt (str): The prompt message. image (str | list | tuple, optional): The image path. audio (str | list | tuple, optional): The audio path. temp (float): The temperature value. top_k (int): The top_k value. top_p (float): The top_p value. ctx_size (int): Size of context (The default is 4096) max_new_tokens (int): Maximum number of tokens to generate. Default 512. audio_input (bool, optional): Whether to run inference with audio input
Returns: response from MLLM as a dataframe
Source code in urbanworm/inference/llama.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | |
batch_inference(system='', prompt='', temp=0.2, top_k=20, top_p=0.8, min_p=0.0, seed=3407, ctx_size=4096, max_new_tokens=512, audio_input=False, disableProgressBar=False, checkpoint_path=None)
¶
Chat with MLLM model for each image in a list. Args: system (str, optional): The system message. prompt (str): The prompt message. temp (float): The temperature value (default: 0.2) top_k (float): The top_k value (default: 20) top_p (float): The top_p value (default: 0.8) min_p (float): min-p sampling (default: 0.0, 0.0 = disabled) seed (int): The seed value (Default is 3407) ctx_size (int): Size of context (Default is 4096) max_new_tokens (int): Maximum number of tokens to generate per call. Default 512. audio_input (bool): Whether to run inference with audio input disableProgressBar (bool): Whether to disable progress bar. checkpoint_path (str, optional): Path to a JSONL file for resume-safe checkpointing. Already-completed items are skipped on the next run automatically. Returns: response from MLLM as a dataframe
Source code in urbanworm/inference/llama.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 | |
to_df(output=True)
¶
Convert the output from an MLLM reponse (from .batch_inference) into a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
bool
|
Whether to return a DataFrame. Defaults to True. |
True
|
Returns: pd.DataFrame: A DataFrame containing responses and associated metadata.
Source code in urbanworm/inference/llama.py
Cloud API¶
Inference via hosted providers (Anthropic, OpenAI, Google).
Install: pip install "urban-worm[api]"
urbanworm.inference.api.InferenceAPI(llm, provider='anthropic', api_key=None, max_tokens=1024, skip_errors=True, **kwargs)
¶
Bases: Inference
Vision-language inference via hosted API providers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
str
|
Model name for the chosen provider, e.g. |
required |
provider
|
str
|
One of |
'anthropic'
|
api_key
|
str | None
|
API key. If |
None
|
max_tokens
|
int
|
Maximum tokens to generate per call. Default 1024. |
1024
|
skip_errors
|
bool
|
If |
True
|
**kwargs
|
Forwarded to :class: |
{}
|
Source code in urbanworm/inference/api.py
Functions¶
one_inference(system='', prompt='', image=None, audio=None, **_kwargs)
¶
Run inference on a single image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
str
|
System prompt. |
''
|
prompt
|
str
|
User prompt. |
''
|
image
|
str | list | tuple | None
|
Path, URL, or base64 string (or list of those for multi-image-per-prompt). |
None
|
audio
|
str | list | tuple | None
|
Accepted for API parity; raises |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One-row DataFrame. |
Source code in urbanworm/inference/api.py
batch_inference(system='', prompt='', disableProgressBar=False, checkpoint_path=None, **_kwargs)
¶
Run inference over all collected images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
str
|
System prompt. |
''
|
prompt
|
str
|
User prompt. |
''
|
disableProgressBar
|
bool
|
Suppress tqdm bar. |
False
|
checkpoint_path
|
str | None
|
Path to a JSONL file for resume-safe checkpointing. On the next run items already in the file are skipped automatically. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame — same shape as the other backends. |
Source code in urbanworm/inference/api.py
to_df(output=True)
¶
Convert self.results into a DataFrame.
Source code in urbanworm/inference/api.py
Output schema¶
urbanworm.inference.format.create_format(fields, *, item_model_name='QnA', wrapper_model_name=None)
¶
Create a typed Response[CustomQnA] model using a dynamically defined schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields
|
dict[str, FieldSpec]
|
field definitions for the inner model. |
required |
item_model_name
|
str
|
name of the inner model. |
'QnA'
|
wrapper_model_name
|
str | None
|
optional pretty name for the specialized wrapper class. |
None
|
Returns:
| Type | Description |
|---|---|
type[BaseModel]
|
A concrete Pydantic model class: Response[CustomQnA]. |
Source code in urbanworm/inference/format.py
urbanworm.inference.format.Response
¶
Bases: BaseModel, Generic[T]
Wrapper schema: {"responses": [ ... ]}