Require `trust_remote_code` to run a local-directory `custom_generate`
Author: LinZiyuu
Pull request: yes

`GenerationMixin.load_custom_generate` runs a repository's `custom_generate/generate.py` (`get_class_in_module` → `exec_module`). This is gated by `trust_remote_code` for Hub repos, but a **local directory** was treated as trusted: `is_local_code = os.path.exists(pretrained_model_name_or_path)` is passed as `has_local_code`, so `resolve_trust_remote_code` never raises and the code runs. As a result `model.generate(custom_generate="<local dir>")` imports and executes the repo's `generate.py` with no `trust_remote_code` opt-in.

This is inconsistent with the rest of the library: `from_pretrained` requires `trust_remote_code` for custom modeling code even when that code lives in a *local* repository directory. An application that loads generation recipes from a local path (e.g. a downloaded/uploaded model directory) while relying on `trust_remote_code=False` to keep code from running is therefore exposed to code execution.

The fix treats `custom_generate` code as remote regardless of where it is loaded from (`has_local_code=False`, `has_remote_code=True`), so the existing `trust_remote_code` gate applies to local directories too. The execution sink (`get_class_in_module`) runs after this check, so untrusted code no longer runs. The existing `test_custom_generate_local_directory` already passes `trust_remote_code=True`, so it is unaffected; a regression test is added for the local-directory-without-opt-in case.

AI assistance was used while drafting this change; I reviewed every line and ran the tests below.

# What does this PR do?

Requires `trust_remote_code=True` to load and run a `custom_generate` function from a local directory, matching the existing behaviour for Hub repos and for custom modeling code in `from_pretrained`.

**Tests run** (`torch==2.12.0`):

```
pytest tests/generation/test_utils.py -k \
  "test_custom_generate_local_directory or test_custom_generate_requires_trust_remote_code"
# 3 passed (existing local-dir test with trust_remote_code=True still passes;
#           new test asserts a local dir without it is rejected)
ruff format --check   # clean
```

Fixes # (security hardening; reported privately rather than via a public issue to avoid disclosing the vector)

## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request), Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes?
- [x] Did you write any new necessary tests?

## Who can review?

- generate: @gante @zucchini-nlp
- model loading (from pretrained): @CyrilVallez


Comments:
@Rocketknight1 following up on the cc above, could you take a look when you get a chance? Happy to make any changes if anything needs adjusting.

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_46483). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.