<commit_before># -*- coding: utf-8 -*-
import wave
import json
import requests
import pyaudio
from settings import HTTP_PROXY, HTTPS_PROXY

ASR_URL = "https://try-api.recaius.jp/asr/v1/"

class RecaiusASR(object):
    """Speech Recognizer by RECAIUS-dev API"""

    def __init__(self, recaius_id, recaius_password, lang):
        # TODO: Use proper id and password automatically according to lang
        self.recaius_id = recaius_id
        self.recaius_password = <PASSWORD>
        self.lang = lang

        self._values = dict()
        self._values['id'] = self.recaius_id
        self._values['password'] = self.<PASSWORD>

        model = dict()
        if self.lang == 'ja_JP':
            model['model_id'] = 1
        elif self.lang == 'en_US':
            model['model_id'] = 5
        elif self.lang == 'zh_CN':
            model['model_id'] = 7

        self._values['model'] = model

        # set proxies
        self.proxies = {
            'http': HTTP_PROXY,
            'https': HTTPS_PROXY,
        }

    def recognize(self, wave_file):
        uuid = self._login()

        w = wave.open(wave_file)

        p = pyaudio.PyAudio()
        stream = p.open(
            format=p.get_format_from_width(w.getsampwidth()),
            channels=w.getnchannels(),
            rate=w.getframerate(),
            output=True)
        chunk = 999999999
        data = w.readframes(chunk)
        voiceid = 1
        self._voice(uuid, voiceid, data, wave_file)
        response = self._voice(uuid, voiceid, b'', wave_file)
        print("==>", response.status_code, response.text)
        self._logout(uuid)

    def _login(self):
        # check necessary parameters
        if 'id' not in self._values:
            raise RecaiusASRException('Missing parameter: id')
        if 'password' not in self._values:
            raise RecaiusASRException('Missing parameter: password')
        if 'model' not in self._values:
            raise RecaiusASRException('Missing parameter: model')

        headers = {'Content-Type': 'application/json'}
        data = json.dumps(self._values)
        data = data.encode('utf-8')
        response = requests.post(ASR_URL + 'login', data=data, headers=headers, proxies=self.proxies)
        uuid = response.text

        return uuid

    def _logout(self, uuid):
        values = dict()
        values['uuid'] = uuid

        headers = {'Content-Type': 'application/json'}
        data = json.dumps(values)
        data = data.encode('utf-8')
        response = requests.post(ASR_URL + uuid + '/logout', data=data, headers=headers, proxies=self.proxies)
        uuid = response.text

        return

    def _voice(self, uuid, voiceid, pcm_data, wave_file):
        voice = open(wave_file, 'rb')
        files = {'voice': ('data.wav', voice, 'application/octet-stream')}
        data = {'voiceid': voiceid}
        response = requests.put(ASR_URL + uuid + '/voice', files=files, data=data, proxies=self.proxies)
        print(response.status_code)
        return response

    def _result(self, uuid):
        pass

class RecaiusASRException(Exception):
    pass

if __name__ == '__main__':
    from settings import ASR_ID, ASR_PASSWORD
    rec = RecaiusASR(ASR_ID, ASR_PASSWORD, '<PASSWORD>')
    rec.recognize('../recaius_test.wav')<commit_msg>Send voice requests by the chunk<commit_after># -*- coding: utf-8 -*-
import wave
import json
import requests
import pyaudio
from settings import HTTP_PROXY, HTTPS_PROXY

ASR_URL = "https://try-api.recaius.jp/asr/v1/"

class RecaiusASR(object):
    """Speech Recognizer by RECAIUS-dev API"""

    def __init__(self, recaius_id, recaius_password, lang):
        # TODO: Use proper id and password automatically according to lang
        self.recaius_id = recaius_id
        self.recaius_password = <PASSWORD>
        self.lang = lang

        self._values = dict()
        self._values['id'] = self.recaius_id
        self._values['password'] = self.recaius_password

        model = dict()
        if self.lang == 'ja_JP':
            model['model_id'] = 1
        elif self.lang == 'en_US':
            model['model_id'] = 5
        elif self.lang == 'zh_CN':
            model['model_id'] = 7

        self._values['model'] = model

        # set proxies
        self.proxies = {
            'http': HTTP_PROXY,
            'https': HTTPS_PROXY,
        }

    def recognize(self, wave_file):
        uuid = self._login()
        print("uuid:", uuid)

        wf = wave.open(wave_file)

        p = pyaudio.PyAudio()
        stream = p.open(
            format=p.get_format_from_width(wf.getsampwidth()),
            channels=wf.getnchannels(),
            rate=wf.getframerate(),
            output=True)
        chunk = 31250
        data = wf.readframes(chunk)
        voice_id = 1
        while data != b'':
            response = self._voice(uuid, voice_id, data)
            print("%d:" % voice_id, len(data), response)
            data = wf.readframes(chunk)
            voice_id += 1
        response = self._voice(uuid, voice_id, data)
        print("%d:" % voice_id, len(data), response)
        response = self._result(uuid)
        print("==>", response)
        self._logout(uuid)

    def _login(self):
        # check necessary parameters
        if 'id' not in self._values:
            raise RecaiusASRException('Missing parameter: id')
        if 'password' not in self._values:
            raise RecaiusASRException('Missing parameter: password')
        if 'model' not in self._values:
            raise RecaiusASRException('Missing parameter: model')

        headers = {'Content-Type': 'application/json'}
        data = json.dumps(self._values)
        data = data.encode('utf-8')
        response = requests.post(ASR_URL + 'login', data=data, headers=headers, proxies=self.proxies)
        uuid = response.text

        return uuid

    def _logout(self, uuid):
        values = dict()
        values['uuid'] = uuid

        headers = {'Content-Type': 'application/json'}
        data = json.dumps(values)
        data = data.encode('utf-8')
        response = requests.post(ASR_URL + uuid + '/logout', data=data, headers=headers, proxies=self.proxies)
        uuid = response.text

        return

    def _voice(self, uuid, voice_id, pcm_data):
        files = {'voice': ('dummy.wav', pcm_data, 'application/octet-stream')}
        data = {'voiceid': voice_id}
        response = requests.put(ASR_URL + uuid + '/voice', files=files, data=data, proxies=self.proxies)
        return response.status_code, response.text

    def _result(self, uuid):
        response = requests.get(ASR_URL + uuid + '/result', proxies=self.proxies)
        return response.status_code, response.text

class RecaiusASRException(Exception):
    pass

if __name__ == '__main__':
    from settings import ASR_ID, ASR_PASSWORD
    rec = RecaiusASR(ASR_ID, ASR_PASSWORD, '<PASSWORD>')
    rec.recognize('../sample.wav')
{'fraction_non_alphanumeric': 0.12366213479895863, 'fraction_numerical': 0.004483656349435927, 'mean_word_length': 2.3649635036496353, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4283250', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2141, 'n_words': 488}