Skip to main content

Getting Started

Welcome to Covalent Cloud! This guide will assist you in quickly deploying your computing applications, whether they are AI apps or other types. With Covalent Cloud, you can run computing batch jobs, create scalable API endpoints, and orchestrate complex workflows without the burden of managing any infrastructure directly from Python.

Installation and Initial Setup

Start by installing the Covalent Cloud SDK:

pip install -U covalent-cloud

Create an account and retrieve your API key from the dashboard and set it in your local environment.

import covalent as ct 
import covalent_cloud as cc

cc.save_api_key("your-api-key-here")

Environment Setup

Skip the hassle of managing Docker containers. With Covalent Cloud, you can directly create and manage Python environments. This approach is simpler, scalable, and keeps your environments always available across the platform. Create and reuse as many as you need with ease.

cc.create_env(
name="sklearn-env",
pip=["scikit-learn","yfinance"],
conda={"channels": ["anaconda"],
"dependencies": ["matplotlib"]},
wait=True,
)

For more on creating and managing environments, see our Environment Management Guide

Define Compute Resources

Cloud Executors represent a modular set of compute resources, specifying the resources needed for each task. Note that resources are not created when defining an executor; they are only utilized when the function attached to this executor is run.

cpu_ex = cc.CloudExecutor(env="sklearn-env", 
num_cpus=2,
memory="8GB",
time_limit="2 hours")

To utilize GPUs, specify the GPU type and the number of GPUs:

gpu_ex = cc.CloudExecutor(env="sklearn-env", 
num_cpus=24,
num_gpus=4,
gpu_type="h100",
time_limit="30 minutes")

Learn more about defining resources and other GPUs in our Compute Resource Guide.

Running batch jobs on Covalent Cloud is straightforward. By adding a single decorator to your Python functions, you can easily dispatch and execute them using the specified compute resources. Click here to learn more about job submissions. Here's how you can train a stock predictor using the environment and resources we defined earlier:

import numpy as np
import yfinance as yf
import sklearn
import sklearn.model_selection
import sklearn.svm

import covalent_cloud as cc
import covalent as ct

@ct.lattice(executor=cpu_ex,workflow_executor=cpu_ex)
@ct.electron(executor=cpu_ex)
def fit_svr_model_and_evaluate(ticker,n_chunks,C=1):
# Fetch and prepare data
data = yf.download(ticker, start='2022-01-01', end='2023-01-01')['Close'].values
X,y = np.array([data[i:i+n_chunks] for i in range(len(data)-n_chunks)]),data[n_chunks:]

# Split data into train and test sets
X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=0.2, shuffle=False)

# Fit SVR model
model = sklearn.svm.SVR(C=C).fit(X_train, y_train)

# Predict and calculate accuracy
predictions = model.predict(X_test)
accuracy = sklearn.metrics.mean_squared_error(y_test, predictions)

return model, accuracy

runid=cc.dispatch(fit_svr_model_and_evaluate)('AAPL',n_chunks=6,C=10)
result=cc.get_result(runid,wait=True)
result.result.load()
print(result.result.value)
Full Code

import numpy as np
import yfinance as yf
import sklearn
import sklearn.model_selection
import sklearn.svm

import covalent_cloud as cc
import covalent as ct

# Save your API key
cc.save_api_key("your-api-key-here")

# Define environment
cc.create_env(
name="sklearn-env",
pip=["scikit-learn", "yfinance"],
conda={"channels": ["anaconda"],
"dependencies": ["matplotlib"]},
wait=True,
)

# Define executor
cpu_ex = cc.CloudExecutor(env="sklearn-env",
num_cpus=2,
memory="8GB",
time_limit="2 hours")

@ct.lattice(executor=cpu_ex, workflow_executor=cpu_ex)
@ct.electron(executor=cpu_ex)
def fit_svr_model_and_evaluate(ticker, n_chunks, C=1):
# Fetch and prepare data
data = yf.download(ticker, start='2022-01-01', end='2023-01-01')['Close'].values
X, y = np.array([data[i:i+n_chunks] for i in range(len(data)-n_chunks)]), data[n_chunks:]

# Split data into train and test sets
X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=0.2, shuffle=False)

# Fit SVR model
model = sklearn.svm.SVR(C=C).fit(X_train, y_train)

# Predict and calculate accuracy
predictions = model.predict(X_test)
accuracy = sklearn.metrics.mean_squared_error(y_test, predictions)

return model, accuracy

runid = cc.dispatch(fit_svr_model_and_evaluate)('AAPL', n_chunks=6, C=10)
result = cc.get_result(runid, wait=True)
result.result.load()
print(result.result.value)