<commit_before>import uuid
import base64

from bottle import run, get, post, request
from pydub import AudioSegment

import recognise
import watson


def convert_stream(stream, ext='wav', to='wav', parameters=None):
    binary = base64.b64decode(stream)
    filename = '/tmp/{}.{}'.format(uuid.uuid4(), ext)
    with open(filename, 'wb') as file:
        file.write(binary)
    if to != ext:
        voice = AudioSegment.from_file(filename)
        new_filename = '/tmp/{}.{}'.format(uuid.uuid4(), to)
        voice.export(new_filename, format=to, parameters=parameters)
        return new_filename
    return filename

@post('/auth')
def authtenticate():
    params = request.json.get('params')
    stream = params.get('stream')
    filename = convert_stream(stream, 'm4a', 'wav')
    print(filename)
    result = dict(result=recognise.recognise_voice(filename))
    print(result)
    #recognise.collect('adela', filename)
    return result

@post('/speech/text')
def speech_to_text():
    params = request.json.get('params')
    stream = params.get('stream')
    stream_type = request.json.get('type')
    filename = convert_stream(stream, stream_type, 'flac')
    return watson.speech_to_text(filename, content_type='audio/flac')

@post('/text/speech')
def text_to_speech():
    params = request.json.get('params')
    text = params.get('text')
    filename = watson.text_to_speech(text)
    with open(filename, 'rb') as file:
        return dict(content=base64.b64encode(file.read()))

@post('/text/tone')
def analyze_tone():
    params = request.json.get('params')
    text = params.get('text')
    filename = watson.text_to_speech(text)
    with open(filename, 'rb') as file:
        return dict(content=base64.b64encode(file.read()))


#watson.speech_to_text('/home/abi/Downloads/0001.flac')
#watson.speech_to_text('/home/abi/Downloads/Telegram Desktop/Start Jet Bank.m4a', content_type='audio/m4a')
#watson.speech_to_text('/home/abi/Downloads/Telegram Desktop/Start Jet Bank.m4a', content_type='audio/m4a')
#print(watson.text_to_speech('Hi Majid, Your balance is 0.2 EUR, you are out of money.'))
#with open('/home/abi/Downloads/Telegram Desktop/Start Jet Bank.m4a', 'rb') as f:
#    s = base64.b64encode(f.read())
#    filename = convert_stream(s, ext='m4a', to='wav', parameters='-ar 8000 -ac 1')
#    print(filename)
#    watson.speech_to_text(filename, content_type='audio/flac')

run(host='0.0.0.0', port=8080)
<commit_msg>Sample collecting<commit_after>import uuid
import base64

from bottle import run, get, post, request
from pydub import AudioSegment

import recognise
import watson

def file_to_base64(filename):
    filename = watson.text_to_speech(text)
    with open(filename, 'rb') as file:
        return base64.b64encode(file.read())


def convert_stream(stream, ext='wav', to='wav', parameters=None):
    binary = base64.b64decode(stream)
    filename = '/tmp/{}.{}'.format(uuid.uuid4(), ext)
    with open(filename, 'wb') as file:
        file.write(binary)
    if to != ext:
        voice = AudioSegment.from_file(filename)
        new_filename = '/tmp/{}.{}'.format(uuid.uuid4(), to)
        voice.export(new_filename, format=to, parameters=parameters)
        return new_filename
    return filename

@post('/auth')
def authtenticate():
    params = request.json.get('params')
    stream = params.get('stream')
    filename = convert_stream(stream, 'm4a', 'wav')
    result = recognise.recognise_voice(filename)
    if result is not None:
        voice_filename = watson.text_to_speech('Hello {}!'.format(result.get('name')))
        result.voice_message = file_to_base64(voice_filename)
        return dict(result=result)
    else:
        voice_filename = watson.text_to_speech('Sorry, I could not recognise you.')
        voice_message = file_to_base64(voice_filename)
        return dic(result=dict(name=None, voice_message=voice_message))

@post('/speech/text')
def speech_to_text():
    params = request.json.get('params')
    stream = params.get('stream')
    stream_type = request.json.get('type')
    filename = convert_stream(stream, stream_type, 'flac')
    return watson.speech_to_text(filename, content_type='audio/flac')

@post('/text/speech')
def text_to_speech():
    params = request.json.get('params')
    text = params.get('text')
    filename = watson.text_to_speech(text)
    with open(filename, 'rb') as file:
        return dict(result=base64.b64encode(file.read()))

@post('/text/tone')
def analyze_tone():
    params = request.json.get('params')
    text = params.get('text')
    filename = watson.text_to_speech(text)
    return dict(result=base64.b64encode(file.read()))


#watson.speech_to_text('/home/abi/Downloads/0001.flac')
#watson.speech_to_text('/home/abi/Downloads/Telegram Desktop/Start Jet Bank.m4a', content_type='audio/m4a')
#watson.speech_to_text('/home/abi/Downloads/Telegram Desktop/Start Jet Bank.m4a', content_type='audio/m4a')
#print(watson.text_to_speech('Hi Majid, Your balance is 0.2 EUR, you are out of money.'))
#with open('/home/abi/Downloads/Telegram Desktop/Start Jet Bank.m4a', 'rb') as f:
#    s = base64.b64encode(f.read())
#    filename = convert_stream(s, ext='m4a', to='wav', parameters='-ar 8000 -ac 1')
#    print(filename)
#    watson.speech_to_text(filename, content_type='audio/flac')

run(host='0.0.0.0', port=8080)

{'fraction_non_alphanumeric': 0.15065913370998116, 'fraction_numerical': 0.0192090395480226, 'mean_word_length': 4.305694305694305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '541790', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1756, 'n_words': 352}