Skip to main content

Text To Text

Learn how to train a text-to-text model using Clarifai SDKs


Text-to-text models are a class of natural language processing (NLP) models designed to handle a wide range of text-based tasks using a unified architecture. Unlike traditional NLP models that are task-specific, text-to-text models are trained to map input text to output text, making them highly versatile and capable of performing various tasks such as translation, summarization, question answering, and text generation. You can learn more about Text-To-Text here.

App Creation

The first part of model training includes the creation of an app under which the training process takes place. Here we are creating an app with the app id as “demo_train” and the base workflow is set as “Universal”. You can change the base workflows to Empty, Universal, Language Understanding, and General according to your use case.

from clarifai.client.user import User
#replace your "user_id"
client = User(user_id="user_id")
app = client.create_app(app_id="demo_train", base_workflow="Universal")

Dataset Upload

The next step involves dataset upload. You can upload the dataset to your app so that the model accepts the data directly from the platform. The data used for training in this tutorial is available in the examples repository you have cloned.

#importing load_module_dataloader for calling the dataloader object in dataset.py in the local data folder
from clarifai.datasets.upload.utils import load_module_dataloader


# Construct the path to the dataset folder
CSV_PATH = os.path.join(os.getcwd().split('/models/model_train')[0],'datasets/upload/data/imdb.csv')


# Create a Clarifai dataset with the specified dataset_id
dataset = app.create_dataset(dataset_id="text_dataset")
# Upload the dataset using the provided dataloader and get the upload status
dataset.upload_from_csv(csv_path=CSV_PATH,input_type='text',csv_type='raw', labels=True)

If you have followed the steps correctly you should receive an output that looks like this,

Output

Choose The Model Type

First let's list the all available trainable model types in the platform,

print(app.list_trainable_model_types())
Output
['visual-classifier',
'visual-detector',
'visual-segmenter',
'visual-embedder',
'clusterer',
'text-classifier',
'embedding-classifier',
'text-to-text']

Click here to know more about Clarifai Model Types.

Model Creation

From the above list of model types we are going to choose text-to-text as it is similar to our use case. Now let's create a model with the above model type.

MODEL_ID = "model_text_to_text"
MODEL_TYPE_ID = "text-to-text"

# Create a model by passing the model name and model type as parameter
model = app.create_model(model_id=MODEL_ID, model_type_id=MODEL_TYPE_ID)
Output
tip

Click here to learn how to patch your model.

Template Selection

Inside the Clarifiai platform there is a template feature. Templates give you the control to choose the specific architecture used by your neural network, as well as define a set of hyperparameters you can use to fine-tune the way your model learns. We are going to choose the 'HuggingFace_AdvancedConfig' template for training our model.

print(model.list_training_templates())
Output
['HF_GPTNeo_125m_lora',
'HF_GPTNeo_2p7b_lora',
'HF_Llama_2_13b_chat_GPTQ_lora',
'HF_Llama_2_7b_chat_GPTQ_lora',
'HF_Mistral_7b_instruct_GPTQ_lora',
'HuggingFace_AdvancedConfig']

Setup Model Parameters

You can update the model params to your need before initiating training.

# get the model params
model_params = model.get_params(template='HuggingFace_AdvancedConfig')
# update dataset field
model.update_params(dataset_id = 'text_dataset')
print(model.training_params)
Output
{'dataset_id': 'text_dataset',
'dataset_version_id': '',
'train_params': {'invalid_data_tolerance_percent': 5.0,
'template': 'HuggingFace_AdvancedConfig',
'model_config': {'pretrained_model_name_or_path': 'facebook/opt-125m',
'torch_dtype': 'torch.float32'},
'tokenizer_config': {'model_max_length': 512.0},
'trainer_config': {'num_train_epochs': 1.0,
'auto_find_batch_size': True,
'output_dir': 'checkpoint'}}}

Initiate Model Training

We can initiate the model training by calling the model.train() method. The Clarifai SDKs also offers features like showing training status and saving training logs in a local file.

note

If the status code is 'MODEL-TRAINED', then the user can know the Model is Trained and ready to use.

import time
#Starting the training
model_version_id = model.train()

#Checking the status of training
while True:
status = model.training_status(version_id=model_version_id,training_logs=False)
if status.code == 21106: #MODEL_TRAINING_FAILED
print(status)
break
elif status.code == 21100: #MODEL_TRAINED
print(status)
break
else:
print("Current Status:",status)
print("Waiting---")
time.sleep(120)
Output

Model Prediction

Since the model is trained and ready let’s run some predictions to view the model performance,

# Getting the predictions
TEXT = b"This is a great place to work"
model_prediction = model.predict_by_bytes(TEXT, input_type="text")

# Get the output
print('Input: ',TEXT)
print(model_prediction.outputs[0].data.text)
Output

Input: b'This is a great place to work'

raw: ". The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food is good. The staff is very friendly and helpful. The food"

text_info {

encoding: "UnknownTextEnc"

}