-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
44 lines (27 loc) · 947 Bytes
/
inference.py
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
import io
import utils
import config
import numpy as np
from PIL import Image
from flask_ngrok import run_with_ngrok
from flask import Flask, jsonify, request
app = Flask(__name__)
run_with_ngrok(app)
@app.route('/report/generate', methods=['POST'])
def generate_report():
global model
model.eval()
if request.method == 'POST':
file = request.files['file']
image = io.BytesIO(file.read())
image = np.array(Image.open(image).convert('L'))
image = np.expand_dims(image, axis=-1)
image = image.repeat(3, axis=-1)
image = config.basic_transforms(image=image)['image']
image = image.to(config.DEVICE)
report = model.generate_caption(image.unsqueeze(0), max_length=25)
return jsonify({'report': ' '.join(report)})
if __name__ == '__main__':
model = utils.get_model_instance(utils.load_dataset().vocab)
utils.load_checkpoint(model)
app.run()