Learn ML model inference via REST API calls with our step-by-step guide. Easily integrate AI into your applications!

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
// Import required modules from the Flask framework
from flask import Flask, request, jsonify
// Create the Flask application instance
app = Flask(name)
// Function to load and return the pre-trained ML model
def load_model():
// Replace this with your actual model loading logic (e.g., using pickle, joblib, or a deep learning framework)
// For example, model = pickle.load(open('model.pkl', 'rb'))
model = "your_pretrained_model_object"
return model
// Load the model once during application startup
model = load_model()
// Define the API endpoint for model inference
@app.route('/predict', methods=['POST'])
def predict():
// Extract JSON data from the POST request
data = request.get_json()
// Validate that input data exists
if data is None:
return jsonify({'error': 'No input data provided'}), 400
try:
// Preprocess input data (you can add data normalization, tokenization, etc.)
// For instance, input\_data = preprocess(data)
input\_data = data
// Make predictions using the loaded model.
// Replace this with your actual ML inference code:
// prediction = model.predict(input\_data)
prediction = "dummy_prediction_result"
// Postprocess the prediction if necessary before returning
result = {'prediction': prediction}
return jsonify(result), 200
except Exception as e:
// Capture and return any error encountered during inference
return jsonify({'error': str(e)}), 500
// Run the application if executed as the main script
if name == 'main':
// Enable debug mode for development
app.run(debug=True)
From startups to enterprises and everything in between, see for yourself our incredible impact.
Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.Â