/ src / python / txtai / cloud / factory.py
factory.py
 1  """
 2  Factory module
 3  """
 4  
 5  from ..util import Resolver
 6  
 7  from .hub import HuggingFaceHub
 8  from .storage import ObjectStorage, LIBCLOUD
 9  
10  
11  class CloudFactory:
12      """
13      Methods to create Cloud instances.
14      """
15  
16      @staticmethod
17      def create(config):
18          """
19          Creates a Cloud instance.
20  
21          Args:
22              config: cloud configuration
23  
24          Returns:
25              Cloud
26          """
27  
28          # Cloud instance
29          cloud = None
30  
31          provider = config.get("provider", "")
32  
33          # Hugging Face Hub
34          if provider.lower() == "huggingface-hub":
35              cloud = HuggingFaceHub(config)
36  
37          # Cloud object storage
38          elif ObjectStorage.isprovider(provider):
39              cloud = ObjectStorage(config)
40  
41          # External provider
42          elif provider:
43              cloud = CloudFactory.resolve(provider, config)
44  
45          return cloud
46  
47      @staticmethod
48      def resolve(backend, config):
49          """
50          Attempt to resolve a custom cloud backend.
51  
52          Args:
53              backend: backend class
54              config: configuration parameters
55  
56          Returns:
57              Cloud
58          """
59  
60          try:
61              return Resolver()(backend)(config)
62  
63          except Exception as e:
64              # Failure message
65              message = f'Unable to resolve cloud backend: "{backend}".'
66  
67              # Append message if LIBCLOUD is not installed
68              message += ' Cloud storage is not available - install "cloud" extra to enable' if not LIBCLOUD else ""
69  
70              raise ImportError(message) from e