-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathserver.py
More file actions
82 lines (73 loc) · 3.17 KB
/
server.py
File metadata and controls
82 lines (73 loc) · 3.17 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
from http.server import HTTPServer, BaseHTTPRequestHandler
import ssl
import sys
import cgi
import base64
import numpy as np
from skimage import io
from io import BytesIO
import requests
import keras
import imghdr
model = keras.models.load_model('./model.h5')
print("Welcome to .....\n")
print("""\
█▀▀ █▀█ █░█ █▀█ ▀█▀ █░█ █▄░█ █▀▀ █▀▄▀█ █░░ █▀▀ ▀█▀ █▀▀ █▀▀ █░█ ▄▀█ █░░ █░░ █▀▀ █▄░█ █▀▀ █▀▀
█▀░ █▄█ █▄█ █▀▄ ░█░ █▄█ █░▀█ ██▄ █░▀░█ █▄▄ █▄▄ ░█░ █▀░ █▄▄ █▀█ █▀█ █▄▄ █▄▄ ██▄ █░▀█ █▄█ ██▄
""")
print("Created by: Alex Neelankavil Devassy")
print("Access http://127.0.0.1 in host systems's browser")
print("Press Ctrl+C to quit")
with open("templates/AICorp.html","rb") as file:
STATIC_HTML_PAGE = file.read()
#simple web server
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
forwardedfor = str(self.headers['X-Forwarded-For'])
print(f"GET {forwardedfor}")
self.send_response(200)
self.end_headers()
self.wfile.write(STATIC_HTML_PAGE)
def do_POST(self):
content_length = int(self.headers["Content-Length"])
if int(content_length) > 10000000:
print("File too big")
self.send_response(500, "File too big")
return
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={"REQUEST_METHOD":"POST",
"CONTENT_TYPE":self.headers["Content-Type"],
})
filename = str(form['file'].filename)
forwardedfor = str(self.headers['X-Forwarded-For'])
print(f"POST {forwardedfor} File: {filename} - ", end = ".")
data = form["file"].file.read()
print("Checking image", end = ". ")
#print(data)
filetype = imghdr.what(file="", h=data)
print(filetype)
if filetype not in ["png","jpeg","gif"]:
print(f"Unsupported media type: {filetype}", end = ". ")
self.send_response(415, "Only png, jpg and gif are supported.")
return
image = io.imread(BytesIO(data))
processedImage = np.zeros([1, 28, 28, 1])
for yy in range(28):
for xx in range(28):
processedImage[0][xx][yy][0] = float(image[xx][yy]) / 255
shownDigit = np.argmax(model.predict(processedImage))
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
if shownDigit == 4:
response = '{ "text": " Welcome Mr. ' + str(shownDigit) +'tune {++BackPropogation Magic++}"}'
else:
response = '{ "text": "Access Denied"}'
sys.stdout.flush()
self.wfile.write(bytes(response,"utf-8"))
httpd = HTTPServer(("", 8080), SimpleHTTPRequestHandler)
httpd.serve_forever()