Skip to main content

Deploy a Module

So you have your first module ready. Now you want to deploy it as a remote module. Givin the following module. Assume its located in commune/model.py, resulting in a module path of model.

import commune as c

class Model(c.Module):

def __init__(self, model='gpt1000'):
self.set_model(model)

def set_model(self,model:str):
...

def forward(self, x:dict) -> dict:
return self.model(x)

Deploy the Object by calling the deploy function, which is a function within c.Module. This deploys the module as a grpc server. The following chooses an open port on your machine and runs the model on that port.

Model.deploy()

Whilelist functions and Attributes

Because you may expose your module to the public, there may be some functions you want to protect from the outside, such as the c.cmd function, which allows for shell commands. By default, you only expose the functions that your python class has. If you want to whitelist or blacklist functions, create a function in the module as such



class Model(c.Module):
...
def get_password(cls):
return c.cmd('echo $MY_PASSWORD')

def blacklist(self):
return ['get_password']

# or whitelist the functions you want
def whitelist(self):
return ['forward']

When deployed, the server will block any calls to these functions that arent whitelisted or are blacklisted. User based permissions are works in progress.

Port Range​

When a module is deployed, it is deployed on a free port that is within the port range. The port range is a range or list of available ports on a machine. These ports can be open or closed for them to be locally accessible

Making Ports Publically Accesible​

In order for your module to be publically accessible, you will need to make sure the port is open on your router. Follow this tutorial for exposing your modules to the world. How to open your port to the public

Security Considerations When Openning Your Ports​

When making your module publicly accessible by opening ports on your router, it's crucial to consider security implications and take appropriate measures to protect your system and data. Here are some important security considerations to keep in mind:

  1. Firewall Configuration: Ensure that you have a properly configured firewall in place to filter incoming traffic and allow access only to the necessary ports. Limiting access to specific IP addresses or using port forwarding rules can help enhance security.

  2. Strong Authentication: Implement strong authentication mechanisms to prevent unauthorized access. Use secure authentication protocols and enforce strong passwords for accessing your module.

  3. Regular Updates and Patching: Keep your system and any software dependencies up to date with the latest security patches. Regularly update your module and underlying frameworks to address any known security vulnerabilities.

  4. Secure Communications: Use secure communication protocols, such as HTTPS, for transmitting sensitive data between your module and clients. Encryption helps protect data from interception and ensures confidentiality.

  5. Access Control and Authorization: Implement access control mechanisms to restrict access to your module based on user roles and permissions. Only grant necessary privileges to users and limit administrative access to trusted individuals.

  6. Logging and Monitoring: Implement logging and monitoring capabilities to track and identify any suspicious activities or potential security breaches. Regularly review logs and monitor system behavior for any signs of unauthorized access or abnormal behavior.

  7. Security Audits and Penetration Testing: Consider conducting regular security audits and penetration testing to identify and address potential vulnerabilities. Engage security professionals to evaluate the security posture of your module and provide recommendations for improvement.

  8. Security Best Practices: Follow general security best practices, such as using secure coding practices, validating user inputs, and protecting against common security vulnerabilities like injection attacks and cross-site scripting (XSS).

Remember, exposing ports publicly increases the potential attack surface and can make your system more susceptible to malicious activities. It's crucial to balance accessibility with security and implement appropriate security measures to protect your module and the underlying infrastructure.