File size: 1,055 Bytes
a2bb9cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from flask import Flask, request, jsonify, send_file
from fastai.vision.all import *

app = Flask(__name__)

# Load the trained model
model_path = './model.pkl'
learn = load_learner(model_path)

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

@app.route('/predict', methods=['POST'])
def predict():
    # Check if an image file is present in the request
    if 'image' not in request.files:
        return jsonify({'error': 'No image file found'}), 400

    image_file = request.files['image']

    # Save the uploaded image temporarily
    image_path = 'uploaded_image.jpg'
    image_file.save(image_path)

    # Prepare the image for prediction
    img = PILImage.create(image_path)

    # Make predictions
    pred_class, pred_idx, probs = learn.predict(img)

    # Prepare the response
    response = {
        'predicted_class': pred_class,
        'predicted_probabilities': probs.tolist()
    }

    # Remove the temporary image file
    os.remove(image_path)

    return jsonify(response)

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