Learn to integrate your TensorFlow model into a web app with our easy step-by-step guide. Enhance your site's AI today!

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
// Sample code snippet to load the model
import tensorflow as tf
// Load the TensorFlow model from the specified directory
model = tf.saved_model.load("path/to/your/saved_model")
// Access the default serving function for prediction
predict_fn = model.signatures["serving_default"]
// Example of setting up a basic Flask application
from flask import Flask, request, jsonify
import tensorflow as tf
app = Flask(**name**)
// Load the model once when the server starts
model = tf.saved_model.load("path/to/your/saved_model")
predict_fn = model.signatures["serving_default"]
// Define a route to handle prediction requests
@app.route("/predict", methods=["POST"])
def predict():
// Extract JSON data from the request
data = request.get\_json(force=True)
// Convert input data into a tensor that matches model's input signature
// You might need to preprocess this data according to your model's requirements.
// Example if model expects a tensor named "input\_1":
input_tensor = tf.convert_to\_tensor(data["input"], dtype=tf.float32)
// Make a prediction calling the serving function with properly named input parameter
result = predict_fn(input_1=input\_tensor)
// Extract the prediction result. The returned dict values are tensors.
prediction = result["output\_0"].numpy().tolist()
// Return the prediction as a JSON response
return jsonify({"prediction": prediction})
if **name** == "**main**":
app.run(debug=True)
// Expanded endpoint for improved readability and error handling
@app.route("/enhanced\_predict", methods=["POST"])
def enhanced\_predict():
try:
data = request.get\_json(force=True)
// Validate presence of input data
if "input" not in data:
return jsonify({"error": "Missing 'input' parameter."}), 400
// Preprocess input data
input_tensor = tf.convert_to\_tensor(data["input"], dtype=tf.float32)
// Generate prediction
result = predict_fn(input_1=input\_tensor)
prediction = result["output\_0"].numpy().tolist()
// Return JSON with the prediction result
return jsonify({"prediction": prediction})
except Exception as e:
// In production, avoid detailed error messages for security
return jsonify({"error": "Prediction failed", "message": str(e)}), 500
curl -X POST -H "Content-Type: application/json" -d '{"input": [[0.5, 0.3]]}' http://localhost:5000/predict
// An example command to run with Gunicorn
// gunicorn app:app --bind 0.0.0.0:8000 --workers 4
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.Â