Integrate your ML model with a React frontend using our simple step-by-step guide. Build smarter, AI-powered web interfaces 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.
// Example using Flask
from flask import Flask, request, jsonify
app = Flask(**name**)
// Load your ML model here
// model = load\_model('path/to/your/model')
@app.route('/predict', methods=['POST'])
def predict():
data = request.json // Extracting JSON data from the request
// Prepare data for prediction
// prediction = model.predict(processed\_data)
// For demonstration, we return a dummy response
result = {'prediction': 'dummy\_output'}
return jsonify(result)
if **name** == '**main**':
app.run(debug=True) // Runs the API on a default port (5000)
// Using flask-cors
from flask\_cors import CORS
app = Flask(**name**)
CORS(app) // Enables CORS for all routes
// In your React component (e.g., PredictionComponent.js)
import React, { useState } from 'react'
const PredictionComponent = () => {
const [inputData, setInputData] = useState('')
const [prediction, setPrediction] = useState(null)
const [loading, setLoading] = useState(false)
const handlePredict = async () => {
setLoading(true)
try {
// Make API call to your ML model endpoint
const response = await fetch('http://localhost:5000/predict', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ data: inputData }),
})
const result = await response.json()
setPrediction(result.prediction)
} catch (error) {
console.error('Error fetching prediction:', error)
}
setLoading(false)
}
return (
setInputData(e.target.value)}
placeholder="Enter input data"
/>
{loading && Loading...}
{prediction && Prediction: {prediction}}
)
}
export default PredictionComponent
// In React, data is stringified before making the POST call as seen in the previous example
// In the ML model API, ensure that the input is parsed correctly:
@app.route('/predict', methods=['POST'])
def predict():
data = request.get\_json()
// Verify whether the data meets the format expected by the ML model
if 'data' not in data:
return jsonify({'error': 'Invalid input'}), 400
// Process and validate data here, e.g., cast to appropriate numerical types
// prediction = model.predict(processed\_data)
return jsonify({'prediction': 'dummy\_output'})
// Example: Using a simple caching strategy in the API
from functools import lru\_cache
@lru\_cache(maxsize=128)
def cached_prediction(input_str):
// Assume process_model returns the prediction based on input_str
return process_model(input_str)
@app.route('/predict', methods=['POST'])
def predict():
data = request.json
if 'data' not in data:
return jsonify({'error': 'Invalid input'}), 400
result = cached\_prediction(data['data'])
return jsonify({'prediction': result})
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.Â