Files
ths-speech/python-server/server_interface.py
2018-08-22 05:40:51 +02:00

83 lines
2.4 KiB
Python

import speech
import filesystem
MAIN_DIR = b"data/"
def parse_request(data):
''' parse request and call correct function '''
# echo/test connection #
cleared_data = is_data_type(b"ECHOREQUEST,",data)
if cleared_data:
return cleared_data
# handle audio transmission #
cleared_data = is_data_type(b"AUDIO_TRANSMISSION,",data)
if cleared_data:
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)
speech.async_create_transcript(filename)
return ret
# handle a chain of audiotransmissions #
if data.startswith(b"CHAIN_AUDIO_TRANSMISSION"):
files = []
base64_strings = []
for el in data.split(b"|"):
filename, base64_string = el.split(b',')
filename = MAIN_DIR + filename.split(b"/")[-1] + b".wav"
filename = filename.decode("utf-8")
files += [filename]
base64_strings += [base64_string]
filesystem.save_audio_chain(files,base64_string);
speech.async_create_transcript(files[0])
return b"SUCCESS"
# 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 '''