-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (41 loc) · 1.45 KB
/
main.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
45
46
47
48
49
50
import logging
import sys
import io
import zmq
from PIL import Image
from flask import Flask
from flask import render_template
from flask import jsonify
from flask import request
from os import environ
from zmq import ssh
# initialize Flask web server
app = Flask(__name__)
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.INFO)
app.config.from_object('config')
# initialize zmq message queue
context = zmq.Context()
port = "6666"
ssh_password = environ.get("SSH_PASSWORD")
app.logger.info("Connecting to titan.elka.pw.edu.pl...")
socket = context.socket(zmq.REQ)
ssh.tunnel_connection(socket, "tcp://localhost:6666", "msucheck@mion.elka.pw.edu.pl", password=ssh_password)
app.logger.info("Connected!")
@app.route('/')
def homepage():
return render_template('index.html')
@app.route('/rate-photo', methods=["PUT"])
def rate_photo():
app.logger.info('Received image: {} bytes, {}.'.format(request.content_length, request.content_type))
app.logger.info('Reading data...')
stream = io.BytesIO(request.data)
image = Image.open(stream)
app.logger.info('Done. Size of the image is {}x{} pixels.'.format(image.size[0], image.size[1]))
app.logger.info('Sending the image to titan.elka.pw.edu.pl...')
socket.send(request.data)
response = socket.recv_json()
app.logger.info('Done. Photo got score of {}%.'.format(response['score']))
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True, use_reloader=True)