Files
ths-speech/python-server/server_interface.py
2018-10-13 18:15:53 +02:00

112 lines
3.4 KiB
Python

import speech
import filesystem
from configparse_wrapper.cpwrap import CFG
MAIN_DIR = bytes(CFG("mainDataDir"),CFG("encoding"))
def parse_request(data):
''' parse request and call correct function '''
#return b"DUMMY"
print(data.split(b",")[0])
# echo/test connection #
cleared_data = is_data_type(b"ECHOREQUEST,",data)
if cleared_data:
return cleared_data
# reply transcript #
cleared_data = is_data_type(b"GET_TRANSCRIPT,",data)
if cleared_data:
filename = cleared_data.decode("utf-8")
return filesystem.get_transcript(filename).encode("utf-8")
# get single file info #
cleared_data = is_data_type(b"GET_FILEINFO,",data)
if cleared_data:
filename = data.decode("utf-8")
return filesystem.fileinfo(filename).encode("utf-8")
# get single file info #
cleared_data = is_data_type(b"GET_FILEINFO_ALL,",data)
if cleared_data:
return filesystem.filelist().encode("utf-8")
# handle audio transmission #
cleared_data = is_data_type(b"AUDIO_TRANSMISSION,",data)
if cleared_data:
print("Handling audio transmission")
filename = None
try:
filename, base64_string = cleared_data.split(b',')
filename = MAIN_DIR + filename.split(b"/")[-1] + b".wav"
filename = filename.decode("utf-8")
except ValueError:
return b"ERROR_MALFORMED_REQUEST"
ret = filesystem.save_audio(filename, base64_string)
if b"ERROR" in ret:
return ret
speech.async_create_transcript(filename)
return ret
# handle a chain of audiotransmissions #
if data.startswith(b"CHAIN_AUDIO_TRANSMISSION"):
file_str_tuples = []
arr = data.split(b"|")
for el in arr[1:-1]:
filename, base64_string = el.split(b',')
filename = MAIN_DIR + filename.split(b"/")[-1] + b".wav"
filename = filename.decode("utf-8")
file_str_tuples += [(filename,base64_string)]
if len(file_str_tuples) < 2: # a chain has 2 or more elements
return bytes("ERROR_INVALID_NUMBER_FILES_{}".format(len(file_str_tuples)),"utf-8")
ret = filesystem.save_audio_chain(file_str_tuples);
if b"ERROR" in ret:
return ret
speech.async_create_transcript(file_str_tuples[0][0])
return ret
# other shit
return b"UNRECOGNIZED_SERVER_OPTION\n"
def is_data_type(tag,data):
data = data.strip(b"\n")
if data.startswith(tag):
ret = data.split(tag)[1]
if not ret:
ret = b"NULL\n"
return ret
return None
def reply_logs(loglevel=0,lines=100):
''' replies with recent logs '''
pass
def reply_backend(backend=None):
''' replies with current backend if None or sets the new one '''
pass
def reply_files(after=None, before=None, regex=None):
''' replies with a list of transcribed or to be transcribed files '''
def reply_transcript(filename=None):
''' replies with the latest transcript or the transcript of the chosen name '''
pass
def reply_audio(filename=None):
''' replies with the chosen audio as a file '''
pass
def recive_transcribe_request(audiofile):
''' saves and transcribes and audiofile '''
pass
def android_unittest_transcribe_request(audiofile):
''' the android unittests append a special keyword, requests are dummy handled '''
pass