I am creating a voice assistant using python 3.11.6, and inside of my project i have a "Synthesizer "package, which has two files: model.pt and main.py. In that main.py (self.local_file="model.pt"):
def download_model(self, url="https://models.silero.ai/models/tts/ru/v4_ru.pt"):
"""
Function for downloading voice model
:param url: address for downloading voice model
"""
# downloading model from source
if not os.path.isfile(self.local_file):
torch.hub.download_url_to_file(url, self.local_file)
I also have a package named Core, where i import sythesizer, using from Synthesizer.main import Synthesizer (Synthesizer is a class). But when i try to launch Core, it does not find "model.pt" file.
If i place model.pt inside of a Core directory than yes, it will work. But i want to place everything in corresponding packages. How can i modify my projects for it to work? I have lot's of another outer files like speech-recognition models and etc. But i want to keep them all in required packages.
You didn't tell me what
self.local_fileactually is, so I am assuming that it's a path that points to somewhere inside your package.You also didn't show me this
Coremodule, which, as far as I understand, is responsible for reading the.ptfile. You would probably have to change the code in Core to actually find the file.In general, it's a bad idea to download data files into your package's directory hierarchy. For instance, your code will not work if somebody installs your package as a zip archive. Also, tools such as
pipmight get confused if they see files inside your package that they didn't put there. Finally, what if the user of your package wants the file to be stored on a different filesystem (for example, if the filesystem to which the package is installed is too small, or read-only)?.For these reasons, it's best to keep externally downloaded data in a separate place. You should let the user choose where downloaded data should go to. And if the user doesn't specify it, the default value should be whatever is the convention for the target platform / operating system. On Linux, for example, it could be something like
~/.mypackageor (better yet)$XDG_DATA_HOME/mypackage.A good example is
easyocr, which is a popular python package that also downloads external data. You can take a look at how it determines the path to save data here and the code for actually saving the data here.