ff1ra/test_serve.py

114 lines
3.6 KiB
Python
Raw Normal View History

2024-08-05 13:41:50 -04:00
import json
from urllib.parse import urlparse
from http.server import HTTPServer, BaseHTTPRequestHandler
from image_util import load_image
import unittest
import time
import accessible_output2.outputs.auto
import os
import base64
import test
2024-08-05 13:41:50 -04:00
po = test.PackageOutput("ff1_data.lud", "enemy_lookup.dat")
auto_obj = accessible_output2.outputs.auto.Auto()
def text_to_sound(string):
string = string.replace("\"", "'")
os.system("espeak \""+string+"\" --stdout > wave_out.wav")
data = base64.b64encode(open("wave_out.wav").read())
return data
class MainHandler(BaseHTTPRequestHandler):
2024-08-05 13:41:50 -04:00
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("<html><head><title></title></head></html>")
self.wfile.write("<body>yo!</body></html>")
def do_POST(self):
print("____")
query = urlparse.urlparse(self.path).query
if query.strip():
query_components = dict(qc.split("=") for qc in query.split("&"))
else:
query_components = {}
content_length = int(self.headers.getheader('content-length', 0))
data = self.rfile.read(content_length);
doc = json.loads(data)
#print([self.request.body])
output = query_components.get("output","")
outputs = dict()
for value in output.split(","):
if value in ['sound', 'wav']:
outputs['sound'] = value
elif value in ['image', 'png-a', 'png']:
outputs['image'] = value
elif value in ['text']:
outputs['text'] = value
t_time = time.time()
strings = ""
presses = list()
if doc.get("image"):
im = load_image(doc['image'])
state = doc.get('state', {})
t_time = time.time()
strings, presses = po.run(im, state)
print(('aaa', time.time()-t_time))
2024-08-05 13:41:50 -04:00
words = " ".join(strings).replace(">", " ").split(" ")
nw = list()
for idx, word in enumerate(words):
if word.replace("G", "").replace("P", "").replace("O", "0").replace("o", "0").replace("DMG","").isdigit():
if len(words) > idx+1 and words[idx+1]=="to:":
nw.append(word)
else:
nw.append(word.replace("O", "0").replace("o", "0"))
else:
nw.append(word)
strings = " ".join(nw)
res = {"auto": "auto"}
if strings:
for key in outputs:
if key == 'text':
res['text'] = strings
elif key == 'sound':
res['sound'] = text_to_sound(strings)
elif key == 'image':
pass
#res['text'] = strings
#else:
# res['error'] = "No text found."
res['press'] = ",".join(presses)
print(presses)
print(strings)
self.send_response(200)
self.send_header("Content-type", "text/html")
output = json.dumps(res)
print((len(output), time.time()-t_time))
2024-08-05 13:41:50 -04:00
self.send_header("Content-Length", len(output))
self.end_headers()
self.wfile.write(output)
def main():
host = "localhost"
port = 4404
server_class = HTTPServer
2024-08-05 13:41:50 -04:00
httpd = server_class((host, port), MainHandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print('end')
if __name__=='__main__':
main()