This tutorial walks you through the essential steps to create and use a machine learning model with the Plexe Platform API.
Base URL: https://api.plexe.ai
Prerequisites
- Account: Sign up at console.plexe.ai
- API Key: Generate one from your account settings
- HTTP Client: Such as
curl
, Postman, or Python with requests
1. Authentication
All API requests require your API key in the x-api-key
header:
import requests, os, time, json
# Load API key from environment
api_key = os.getenv("PLEXE_API_KEY")
if not api_key:
raise ValueError("Please set the PLEXE_API_KEY environment variable.")
headers = {
"x-api-key": api_key,
"Content-Type": "application/json"
}
base_url = "https://api.plexe.ai"
2. Upload Data
While optional, providing data yields better results. Use a pre-signed URL approach:
# Request pre-signed URL
file_path = "housing_data.csv"
file_name = os.path.basename(file_path)
try:
# Get upload URL
response = requests.post(
f"{base_url}/uploads",
headers=headers,
json={"filename": file_name, "content_type": "text/csv"}
)
response.raise_for_status()
upload_info = response.json()
presigned_url = upload_info.get("presigned_url")
temp_upload_id = upload_info.get("upload_id")
s3_key = upload_info.get("key")
# Upload file to S3
with open(file_path, 'rb') as f:
requests.put(presigned_url, data=f, headers={'Content-Type': 'text/csv'}).raise_for_status()
# Confirm upload completion
confirm_response = requests.post(
f"{base_url}/uploads/status",
headers=headers,
json={"upload_id": temp_upload_id, "filename": file_name, "s3_key": s3_key}
)
confirm_response.raise_for_status()
upload_id = confirm_response.json().get("upload_id")
print(f"Upload confirmed. ID: {upload_id}")
except Exception as e:
print(f"Upload error: {e}")
# Fallback to public dataset
upload_id = "https://raw.githubusercontent.com/plotly/datasets/master/housing_new-york.csv"
The two-step upload process (pre-signed URL → direct upload) enables secure and efficient handling of large files.
3. Build the Model
Submit a build request with your model name, goal, and data reference:
model_name = "api-quickstart-housing"
response = requests.post(
f"{base_url}/models/{model_name}",
headers=headers,
json={
"goal": "Predict house prices in USD based on sqft, beds, baths.",
"upload_id": upload_id,
"metric": "rmse" # Optional: suggest optimization metric
}
)
response.raise_for_status()
model_id = response.json().get("model_id")
print(f"Build requested. Model ID: {model_id}")
4. Monitor Build Status
Model building happens asynchronously. Poll until complete:
if model_id:
# Extract name and version from model_id (format: name:version)
m_name, m_version = model_id.split(':')
status = "pending"
# Poll until completed or failed
while status in ["pending", "processing", "building"]:
time.sleep(15) # Wait between checks
try:
response = requests.get(
f"{base_url}/models/{m_name}/{m_version}/status",
headers=headers
)
response.raise_for_status()
status_result = response.json()
status = status_result.get("status")
print(f"Status: {status}")
if status == "completed":
print("Build successful!")
break
elif status == "failed":
print(f"Build failed: {status_result.get('error', 'Unknown error')}")
model_id = None
break
except Exception as e:
print(f"Status check error: {e}")
time.sleep(30) # Longer wait on error
5. Make Predictions
Once the model is ready, use the inference endpoint:
if model_id and status == "completed":
try:
# Prepare sample input matching your data schema
prediction = requests.post(
f"{base_url}/models/{m_name}/{m_version}/infer",
headers=headers,
json={
"sqft": 1950.0,
"beds": 3,
"baths": 2.5
}
).json()
print(f"Prediction result: {prediction}")
except Exception as e:
print(f"Inference error: {e}")
This completes the basic API workflow. Explore the Platform API Reference for details on all available endpoints and parameters.