Repository: oobabooga/text-generation-webui

Issue:
silero_tts will not load if I am not connected to the internet
### Describe the bug

I used to be able to use this extension offline, but now I can't load the extension if I am not online.  If I am online the extension loads just fine.  The actual language models is saved on my machine via the .cache file: C:\Users\myself\.cache\torch\hub\snakers4_silero-models_master\src\silero\model

The model name is called v3_en.pt, it's being cached on my machine and when I load the extension with an internet connection the miniconda console says that it's using the cached model, so I don't know why I NEED to be connected to the internet for it to work.

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Reproduction

Run this (change your install location as necessary) with and without an internet connection.

cd F:\OoBaboogaMarch17\text-generation-webui
conda activate textgen
python .\server.py --auto-devices --gptq-bits 4 --cai-chat --gptq-model-type LLaMa --extension silero_tts 

### Screenshot

I'm including two screenshots, one when I am connected to the internet, and one when I am not connected to the internet.

![internetConnection](https://user-images.githubusercontent.com/6488699/227263118-cf13d103-975d-4103-b1d7-a0763e492bc7.png)
![no internet connection](https://user-images.githubusercontent.com/6488699/227263135-017e10b7-7164-465f-b2be-af7f837a9d87.png)


### Logs

```shell
See screenshots
```


### System Info

```shell
Window 10, 4090, i9 13900, windows mode not wsl
```



Discussion:
This ain't a bug, it's just raising an exception because it can't connect to the torch hub. It tries to either way, regardless of local cache.

However, it is possible to check if the cached `.pt` exists and substitute the loader
Thank you Bralwence, this kind soul on Reddit has solved my issue and provided code: update there are two versions of the change.  Mine is the first one and the original og Reddit change is below that.  I couldn't get it to work without the comma they get it to work without the comman so 🤷‍♂️ try theirs first and if it doesn't work try mine I guess.

https://old.reddit.com/r/Oobabooga/comments/11zsw5s/anyone_know_how_to_load_the_silero_tts_extension/jdmvocy/

def load_model(): needs to be changed like this:

def load_model(): 
    cache_path='C:/Users/Myself/.cache/torch/hub/snakers4_silero-models_master/' 
    model_path = cache_path + "src/silero/model/" + params['model_id'] + ".pt" 
    if Path(model_path).is_file(): 
       model, example_text = torch.hub.load(repo_or_dir=cache_path, model='silero_tts', language=params['language'], speaker=params['model_id'], source='local', path = model_path, force_reload = True) 
    else: model, example_text = torch.hub.load(repo_or_dir='snakers4/silero-models', model='silero_tts', language=params['language'], speaker=params['model_id']), model.to(params['device']) 
    return model


def load_model():  
    cache_path='C:/Users/USER/.cache/torch/hub/snakers4_silero-models_master/'  
    model_path = cache_path + "src/silero/model/" + params['model_id'] + ".pt"  
    if Path(model_path).is_file():  
        model, example_text = torch.hub.load(repo_or_dir=cache_path, model='silero_tts', language=params['language'], speaker=params['model_id'], source='local', path = model_path, force_reload = True)  
    else:  
        model, example_text = torch.hub.load(repo_or_dir='snakers4/silero-models', model='silero_tts', language=params['language'], speaker=params['model_id'])  
    model.to(params['device'])  
    return model

Resolving patch:
diff --git a/extensions/silero_tts/script.py b/extensions/silero_tts/script.py
--- a/extensions/silero_tts/script.py
+++ b/extensions/silero_tts/script.py
@@ -21,6 +21,7 @@
     'autoplay': True,
     'voice_pitch': 'medium',
     'voice_speed': 'medium',
+    'local_cache_path': ''  # User can override the default cache path to something other via settings.json
 }
 
 current_params = params.copy()
@@ -44,14 +45,18 @@ def xmlesc(txt):
 
 
 def load_model():
-    model, example_text = torch.hub.load(repo_or_dir='snakers4/silero-models', model='silero_tts', language=params['language'], speaker=params['model_id'])
+    torch_cache_path = torch.hub.get_dir() if params['local_cache_path'] == '' else params['local_cache_path']
+    model_path = torch_cache_path + "/snakers4_silero-models_master/src/silero/model/" + params['model_id'] + ".pt"
+    if Path(model_path).is_file():
+        print(f'\nUsing Silero TTS cached checkpoint found at {torch_cache_path}')
+        model, example_text = torch.hub.load(repo_or_dir=torch_cache_path + '/snakers4_silero-models_master/', model='silero_tts', language=params['language'], speaker=params['model_id'], source='local', path=model_path, force_reload=True)
+    else:
+        print(f'\nSilero TTS cache not found at {torch_cache_path}. Attempting to download...')
+        model, example_text = torch.hub.load(repo_or_dir='snakers4/silero-models', model='silero_tts', language=params['language'], speaker=params['model_id'])
     model.to(params['device'])
     return model
 
 
-model = load_model()
-
-
 def remove_tts_from_history(name1, name2, mode):
     for i, entry in enumerate(shared.history['internal']):
         shared.history['visible'][i] = [shared.history['visible'][i][0], entry[1]]
@@ -132,6 +137,11 @@ def bot_prefix_modifier(string):
     return string
 
 
+def setup():
+    global model
+    model = load_model()
+
+
 def ui():
     # Gradio elements
     with gr.Accordion("Silero TTS"):


Test patch: