# This example is the API example for this Ludwig command line example # (https://ludwig-ai.github.io/ludwig-docs/examples/#kaggles-titanic-predicting-survivors). # Import required libraries import logging import os import shutil from ludwig.api import LudwigModel from ludwig.datasets import titanic # clean out prior results shutil.rmtree("./results", ignore_errors=True) # Download and prepare the dataset training_set, _, _ = titanic.load(split=True) # Define Ludwig model object that drive model training model = LudwigModel(config="./model1_config.yaml", logging_level=logging.INFO) # initiate model training ( train_stats, # dictionary containing training statistics preprocessed_data, # tuple Ludwig Dataset objects of pre-processed training data output_directory, # location of training results stored on disk ) = model.train(dataset=training_set, experiment_name="simple_experiment", model_name="simple_model") # list contents of output directory print("contents of output directory:", output_directory) for item in os.listdir(output_directory): print("\t", item)
import sys import requests import pandas as pd from ludwig.datasets import titanic # Ludwig model server default values LUDWIG_HOST = '0.0.0.0' LUDWIG_PORT = '8000' # # retrieve data to make predictions # test_df = titanic.load() print('retrieved {:d} records for predictions'.format(test_df.shape[0])) # # execute REST API /predict for a single record # # get a single record from dataframe and convert to list of dictionaries prediction_request_dict_list = test_df.head(1).to_dict(orient='records') # extract dictionary for the single record only prediction_request_dict = prediction_request_dict_list[0] print('single record for prediction:\n', prediction_request_dict) # construct URL predict_url = ''.join(['http://', LUDWIG_HOST, ':', LUDWIG_PORT, '/predict']) print('\ninvoking REST API /predict for single record...')