from flask import Flask, request, jsonify from sklearn.linear_model import LinearRegression import numpy as np app = Flask(__name__) # 1. Create a simple dataset (X = Input, y = Output) # We are teaching the model that y is roughly 2 times X X = np.array([[1], [2], [3], [4], [5]]) y = np.array([2, 4, 6, 8, 10]) # 2. Train the model model = LinearRegression() model.fit(X, y) @app.route('/') def home(): return "ML API is running! Use the /predict endpoint." @app.route('/predict', methods=['GET']) def predict(): try: # Get the 'value' parameter from the URL (e.g., /predict?value=10) user_input = request.args.get('value') if user_input is None: return jsonify({"error": "Please provide a 'value' parameter"}), 400 prediction = model.predict([[float(user_input)]]) return jsonify({ "input": user_input, "prediction": prediction[0] }) except Exception as e: return jsonify({"error": str(e)}), 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)