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)
 * Serving Flask app '__main__'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5003
Press CTRL+C to quit
127.0.0.1 - - [22/May/2023 13:55:09] "GET /classify HTTP/1.1" 405 -
127.0.0.1 - - [22/May/2023 13:55:10] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [22/May/2023 13:56:15] "GET /classify HTTP/1.1" 405 -
127.0.0.1 - - [22/May/2023 13:56:22] code 400, message Bad request version ('\x9a\x9a\x13\x01\x13\x02\x13\x03À+À/À,À0̨̩À\x13À\x14\x00\x9c\x00\x9d\x00/\x005\x01\x00\x01\x93')
127.0.0.1 - - [22/May/2023 13:56:22] "üÚ²*ê9-îÒ0ÝÜd€D	3·õ†ÏW±Ï@8¼ªÖÒ Pª*|²´éIaÃf!ôõYdóÿ'²Bã¹3?ô ššÀ+À/À,À0̨̩ÀÀœ/5“" 400 -
127.0.0.1 - - [22/May/2023 13:56:22] code 400, message Bad request version ('ÄPj»$Di\x00\x05\x00\x03\x02h2\x00')
3+)ÊÊ ä#õ—ˆwå¬mè;èôw|r6ªk²ŒðÄPj»$Dih2á‚•Æ·ÔI ø|¬¶çJ‘ºÐº+¶[wBgœù}-ok%,¡JK›»ú jjÀ+À/À,À0̨̩ÀÀœ/5“jj-+**hhttp/1.1ÿ" 400 -