Testing ML
Quick launch into Variables, Functions, Arrays, Classes, HTML.
import os
from flask import Flask, jsonify, request
from PIL import Image
import torch
from torchvision import models, transforms
import certifi
app = Flask(__name__)
os.environ['SSL_CERT_FILE'] = certifi.where()
# Load the pre-trained model
model = models.resnet50(pretrained=True)
model.eval()
# Define the image transformation
transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# Define the pre-set tags
pre_set_tags = ["cat", "dog", "flower"]
@app.route('/classify', methods=['POST'])
def classify_image():
    # Check if an image file was sent
    if 'image' not in request.files:
        return jsonify({'error': 'No image file provided'})
    image_file = request.files['image']
    # Load the image and apply transformations
    image = Image.open(image_file)
    image = transform(image)
    image = image.unsqueeze(0)
    # Make predictions
    with torch.no_grad():
        output = model(image)
    # Get the predicted tag with the highest probability
    _, predicted_idx = torch.max(output, 1)
    predicted_tag = pre_set_tags[predicted_idx.item()]
    # Return the predicted tag
    return jsonify({'tag': predicted_tag})
if __name__ == '__main__':
    # Set the path to the CA certificate bundle file
    # Run the app with SSL certificate verification
    app.run(port=5003)