Integrating Machine Learning Models into Web Applications with Flask

Required Tools

This implementation requires Flask, a lightweight Python web framework, and Pickle for model serialization.

Flask provides a minimal foundation for web development with:

  • Simple routing system for URL management
  • Jinja2 template integration for dynamic content
  • RESTful API suport capabilities
  • Extensible architecture through various plugins

Pickle enables Python object serialization with:

  • Native Python support without extrenal dependencies
  • Support for most Python data structures and custom classes
  • Simple interface for saving and loading objects

Security Note: Pickle files should only be loaded from trusted sources due to potential code execution vulnerabilities during deserialization.

Data Preparation and Cleaning

Begin by examining the dataset structure and extracting relevant features:

import pandas as pd
from sklearn.preprocessing import LabelEncoder

# Load and inspect dataset
ufo_data = pd.read_csv('./data/ufos.csv')
print(ufo_data.head())

# Extract relevant columns
processed_data = pd.DataFrame({
    'Duration': ufo_data['duration (seconds)'],
    'Country': ufo_data['country'],
    'Latitude': ufo_data['latitude'],
    'Longitude': ufo_data['longitude']
})

# Remove missing values and filter duration
processed_data.dropna(inplace=True)
processed_data = processed_data[(processed_data['Duration'] >= 1) & 
                               (processed_data['Duration'] <= 60)]

# Convert categorical country labels to numerical values
label_encoder = LabelEncoder()
processed_data['Country'] = label_encoder.fit_transform(processed_data['Country'])
print(processed_data.head())

Model Training

Split the data and train a logistic regression classifier:

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report

# Define feature set and target variable
features = ['Duration', 'Latitude', 'Longitude']
X = processed_data[features]
y = processed_data['Country']

# Split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Initialize and train classifier
classifier = LogisticRegression()
classifier.fit(X_train, y_train)

# Evaluate model performance
predictions = classifier.predict(X_test)
print(classification_report(y_test, predictions))
print(f'Accuracy: {accuracy_score(y_test, predictions):.4f}')

Expected output shows approximately 95% accuracy due to strong correlation between geographical coordinates and country locations.

Model Serialization

Save the trained model using Pickle for later use:

import pickle

# Serialize model to file
model_filename = 'country_predictor.pkl'
with open(model_filename, 'wb') as file:
    pickle.dump(classifier, file)

# Verify model can be loaded
with open(model_filename, 'rb') as file:
    loaded_model = pickle.load(file)

Web Application Implementation

Create the following directory structure:

web-application/
  static/
    css/
      styles.css
  templates/
    index.html
  app.py
  requirements.txt
  country_predictor.pkl

requirements.txt:

scikit-learn==1.2.2
pandas==1.5.3
numpy==1.24.3
flask==2.3.2

Install dependencies:

pip install -r requirements.txt

app.py:

import numpy as np
from flask import Flask, request, render_template
import pickle

app = Flask(__name__)

# Load pre-trained model
with open('country_predictor.pkl', 'rb') as model_file:
    prediction_model = pickle.load(model_file)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/predict', methods=['POST'])
def predict_country():
    # Extract form values and convert to integers
    form_values = [int(value) for value in request.form.values()]
    
    # Prepare features for prediction
    input_features = np.array([form_values])
    
    # Generate prediction
    result = prediction_model.predict(input_features)
    predicted_index = result[0]
    
    # Map prediction to country name
    country_names = ["Australia", "Canada", "Germany", "United Kingdom", "United States"]
    predicted_country = country_names[predicted_index]
    
    return render_template(
        'index.html', 
        prediction_result=f"Predicted country: {predicted_country}"
    )

if __name__ == '__main__':
    app.run(debug=True)

templates/index.html (simplified):

<!DOCTYPE html>
<html>
<head>
    <title>Country Predictor</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
</head>
<body>
    <div class="container">
        <h1>UFO Sighting Country Predictor</h1>
        <form method="POST" action="/predict">
            <input type="number" name="duration" placeholder="Duration (seconds)" required>
            <input type="number" step="any" name="latitude" placeholder="Latitude" required>
            <input type="number" step="any" name="longitude" placeholder="Longitude" required>
            <button type="submit">Predict Country</button>
        </form>
        
        {% if prediction_result %}
            <div class="prediction">
                <h2>{{ prediction_result }}</h2>
            </div>
        {% endif %}
    </div>
</body>
</html>

Input values such as "50, 44, -12" will generate predictions dipslayed directly on the web page.

Tags: Machine Learning Flask web development model deployment python

Posted on Thu, 14 May 2026 10:59:28 +0000 by AIS4U