AutoModel.from_pretrained('.') raises ModuleNotFoundError with trust_remote_code=True
Author: cebtenzzre
Pull request: no

### System Info

- `transformers` version: 4.39.0.dev0 (commit c8d98405a)
- Platform: Linux-6.6.10-zen1-1-zen-cebtenzzre-x86_64-with-glibc2.39
- Python version: 3.11.3
- Huggingface_hub version: 0.20.3
- Safetensors version: 0.4.2
- Accelerate version: not installed
- Accelerate config: not found
- PyTorch version (GPU?): 2.2.0+cu121 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: no
- Using distributed or parallel set-up in script?: no

### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [X] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [X] My own task or dataset (give details below)

### Reproduction

This issue was reported in https://github.com/huggingface/transformers/issues/28919#issuecomment-1939195589 but not fixed by #29175.

1. `git clone https://huggingface.co/nomic-ai/nomic-embed-text-v1`
2. `cd nomic-embed-text-v1`
3. `git lfs pull` (if needed)
4. Try to load the locally downloaded model:
```
$ python -c 'from transformers import AutoModel; AutoModel.from_pretrained(".", trust_remote_code=True)'
Traceback (most recent call last):
  File "/home/jared/src/clones/transformers/src/transformers/dynamic_module_utils.py", line 203, in get_class_in_module
    module = importlib.import_module(module_path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1128, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1128, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'transformers_modules.'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/jared/src/clones/transformers/src/transformers/models/auto/auto_factory.py", line 521, in from_pretrained
    config, kwargs = AutoConfig.from_pretrained(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jared/src/clones/transformers/src/transformers/models/auto/configuration_auto.py", line 1120, in from_pretrained
    config_class = get_class_from_dynamic_module(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jared/src/clones/transformers/src/transformers/dynamic_module_utils.py", line 516, in get_class_from_dynamic_module
    return get_class_in_module(repo_id, class_name, final_module.replace(".py", ""))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jared/src/clones/transformers/src/transformers/dynamic_module_utils.py", line 215, in get_class_in_module
    module = importlib.machinery.SourceFileLoader(module_path, corrected_path).load_module()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 605, in _check_name_wrapper
  File "<frozen importlib._bootstrap_external>", line 1120, in load_module
  File "<frozen importlib._bootstrap_external>", line 945, in load_module
  File "<frozen importlib._bootstrap>", line 290, in _load_module_shim
  File "<frozen importlib._bootstrap>", line 721, in _load
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 936, in exec_module
  File "<frozen importlib._bootstrap_external>", line 1073, in get_code
  File "<frozen importlib._bootstrap_external>", line 1130, in get_data
FileNotFoundError: [Errno 2] No such file or directory: '.home.jared..cache.huggingface.modules.transformers_modules...configuration_hf_nomic_bert.py'
```

### Expected behavior

I would expect `"."` to be interpreted as the path to the model, and for the model to successfully load.

Or at least a clear error message explaining what I am doing incorrectly - the unclear nature of the traceback makes this look like a bug to me.

Comments:
@cebtenzzre could you try updating your library from the main branch and try again 
```
pip install -U git+https://github.com/huggingface/transformers
```
also your command should be updated into this
```
python -c 'from transformers import AutoModel; AutoModel.from_pretrained("./", trust_remote_code=True)'
```
there is a wide difference between `.` and  `./` 

i have also tried it with older version of the transformers (`4.37.2`)  library and it works well 
the only problem is that you need to use 
```python
AutoModel.from_pretrained("./", trust_remote_code=True) # works
AutoModel.from_pretrained(".", trust_remote_code=True) # will create an error
```

![image](https://github.com/huggingface/transformers/assets/70411813/ec363e5d-d552-464e-8feb-30d407a933bf)


I am already on the latest commit (c8d98405a8f7b0e5d07391b671dcc61bb9d7bad5).

`AutoModel.from_pretrained('.')` works as I would expect for models that do not require trust_remote_code - it loads the model from the current directory. If this is only by accident, maybe transformers should throw an exception if `'.'` is ever passed as the model path, so that developers like me don't start to rely on this fact and then have it fail with a cryptic error in edge cases like this.

for now we have #29175 fixing remote repos that have a dot in their name and have a local custom architecture(example `briaai/RMBG-1.4`, `not-lain/MyRepo1.0`, ... )
`nomic-ai/nomic-embed-text-v1.5` has a remote custom architecture defined under `nomic-ai/nomic-embed-text-v1-unsupervised` so it doesn't fall under the same category (**TLDR;** not the same repo) so it works with the transformers library .

for local imports `./` can be a possible solution for now, also cc @Rocketknight1 if you can have a look at this. 

This is also a bug in our repo handling, and likely has the same cause that we fixed in #29175. It's on my list to fix - hopefully we can make it work with small tweaks to that PR.

@Rocketknight1 i don't think it's related to #29175 tbh, but let's monkey-patch this one too with 
```python
# or generalise it by using startswith taking into consideration this usecase too 
# AutoModel.from_pretrained(".myfolder", trust_remote_code=True) # it starts with a dot . 
if repo_id == "." :  
   repo_id = "./"
```
pretty sure this is the last problem for imports, didn't encounter anything with your patch tbh, it's stable to all the stress tests i've done so far, so i'll let you handle this one.

I'm not sure why `importlib.machinery.SourceFileLoader` isn't used in all cases - `module_path = module_path.replace(os.path.sep, ".")` without *at least* a prior `assert "." not in module_path` seems like a bad idea, period. What if module_path contains `/./`, or `/../`?

And replacing the "." with "/" just to try and replace it back with "." doesn't make a lot of sense either.