Skip to content

Commit

Permalink
Usa logging do python
Browse files Browse the repository at this point in the history
  • Loading branch information
valterf committed Jun 17, 2020
1 parent 43346d5 commit ec5c216
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 219 deletions.
11 changes: 2 additions & 9 deletions basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def usage():


if __name__ == "__main__":
ostream = open("log.txt", "a")
argc = len(argv)
if argc != 4 and argc != 6:
usage()
Expand All @@ -63,18 +62,12 @@ def usage():
if argc == 6:
credentials = (argv[4], argv[5])

asr = SpeechRecognizer(
url,
credentials=credentials,
log_stream=ostream,
log_level="debug",
max_wait_seconds=600,
)
asr = SpeechRecognizer(url, credentials=credentials, max_wait_seconds=600,)
asr.recognize(FileAudioSource(apath), lm)
res = asr.wait_recognition_result()
if res:
for k in res:
print(k.alternatives)
else:
print("Empty result! Check log.txt for more info.")
print("Empty result!")
asr.close()
11 changes: 2 additions & 9 deletions buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def usage():


if __name__ == "__main__":
ostream = open("log.txt", "a")
argc = len(argv)
if argc != 4 and argc != 6:
usage()
Expand All @@ -64,13 +63,7 @@ def usage():
if argc == 6:
credentials = (argv[4], argv[5])

asr = SpeechRecognizer(
url,
credentials=credentials,
log_stream=ostream,
log_level="debug",
max_wait_seconds=600,
)
asr = SpeechRecognizer(url, credentials=credentials, max_wait_seconds=600,)

source = BufferAudioSource()
asr.recognize(source, lm)
Expand All @@ -85,6 +78,6 @@ def usage():
for k in res:
print(k.alternatives)
else:
print("Empty result! Check log.txt for more info.")
print("Empty result!")

asr.close()
70 changes: 0 additions & 70 deletions cpqdasr/logger.py

This file was deleted.

9 changes: 3 additions & 6 deletions cpqdasr/recognizer/speech_recognizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
from threading import Condition, Thread
from base64 import b64encode
from time import time
import logging
import copy

from cpqdasr.logger import Logger
from cpqdasr.recognizer_protocol import WS4PYClient
from cpqdasr.recognizer_protocol import (
send_audio_msg,
Expand Down Expand Up @@ -69,7 +69,6 @@ def __init__(
max_wait_seconds=30,
connect_on_recognize=False,
auto_close=False,
log_level="warning",
):
assert audio_sample_rate in [8000, 16000]
assert audio_encoding in ["pcm", "wav", "raw"]
Expand All @@ -85,7 +84,7 @@ def __init__(
self._max_wait_seconds = max_wait_seconds
self._connect_on_recognize = connect_on_recognize
self._auto_close = auto_close
self._logger = Logger(log_stream, alias, log_level)
self._logger = logging.getLogger("cpqdasr")
self._cv_define_grammar = Condition()
self._cv_create_session = Condition()
self._cv_send_audio = Condition()
Expand Down Expand Up @@ -123,7 +122,6 @@ def _connect(self):
listener=self._listener,
user_agent=self._user_agent,
config=self._session_config,
logger=self._logger,
headers=headers,
)
self._ws.connect()
Expand All @@ -137,7 +135,7 @@ def _send_audio_loop(self):
b = next(self._audio_source)
except StopIteration:
self._logger.warning("Empty audio source!")
self._ws.send(send_audio_msg(b'', True), binary=True)
self._ws.send(send_audio_msg(b"", True), binary=True)
return
self._ws._time_wait_recog = time()
for x in self._audio_source:
Expand All @@ -153,7 +151,6 @@ def _send_audio_loop(self):
self._ws.send(send_audio_msg(b, True), binary=True)
self._logger.debug("Send audio")


def _disconnect(self):
if self._ws is not None:
try:
Expand Down
5 changes: 2 additions & 3 deletions cpqdasr/recognizer_protocol/ws4py_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from sys import stderr
from threading import Condition
from ws4py.client.threadedclient import WebSocketClient
import logging

from ..logger import Logger
from ..recognizer.listener import RecognitionListener
from ..recognizer.result import RecognitionResult, PartialRecognitionResult
from .protocol import (
Expand All @@ -46,7 +46,6 @@ def __init__(
listener=RecognitionListener(),
user_agent=None,
config=None,
logger=Logger(stderr),
debug=False,
protocols=None,
extensions=None,
Expand All @@ -65,7 +64,7 @@ def __init__(
self._user_agent = user_agent
self._listener = listener
self._config = config
self._logger = logger
self._logger = logging.getLogger("cpqdasr")
self._status = "DISCONNECTED"
self._cv_define_grammar = cv_define_grammar
self._time_define_grammar = 0
Expand Down
9 changes: 1 addition & 8 deletions mic.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def usage():


if __name__ == "__main__":
ostream = open("log.txt", "a")
argc = len(argv)
if argc != 3 and argc != 5:
usage()
Expand All @@ -98,13 +97,7 @@ def usage():
if argc == 5:
credentials = (argv[3], argv[4])

asr = SpeechRecognizer(
url,
credentials=credentials,
log_stream=ostream,
listener=PrinterListener(),
log_level="warning",
)
asr = SpeechRecognizer(url, credentials=credentials, listener=PrinterListener(),)

with MicAudioSource() as mic:
try:
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def stress_recognition(
session_range=(0, 1),
recognitions=1,
executions=1,
log_stream=stdout,
assertion=lambda x: (True, ""),
):
"""
Expand All @@ -62,8 +61,6 @@ def stress_recognition(
:executions: Number of executions to be run with each worker. The
execution involves the full process of creating and
destroying SpeechRecognizer instances inside each worker.
:log_stream: The stream (usually system streams or open files on write
mode) for writing the SpeechRecognizer logs.
:assertion: A function which receives a list RecognitionResult and
returns a tuple of bool/string depending on the criterion
of success for the running test and the intended message
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
credentials = ("", "")
log_level = "warning" # "error", "warning", "info" or "debug"
log_path = "/tmp/asr-sdk-python-test.log"
slm = "builtin:slm/general"
slm = "builtin:slm/callcenter-small"
phone_grammar_uri = "builtin:grammar/phone"

# Resource config
Expand Down
7 changes: 1 addition & 6 deletions tests/unit/test_audioSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@
BufferAudioSource,
)
from .config import url, credentials, slm, phone_wav
from .config import log_level, log_path

import soundfile as sf


asr_kwargs = {
"credentials": credentials,
"log_level": log_level,
"log_stream": open(log_path, "w"),
}
asr_kwargs = {"credentials": credentials}


# =============================================================================
Expand Down
54 changes: 21 additions & 33 deletions tests/unit/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
These tests only work with specific server configurations.
"""
from cpqdasr import LanguageModelList
from .config import url, credentials, log_level
from .config import url, credentials
from .config import slm, pizza_grammar_path, pizza_multi_wav
from .common import stress_recognition

Expand Down Expand Up @@ -63,41 +63,29 @@ def multi_assertion(results):
# =============================================================================
def test_slm_multi():
lm = LanguageModelList(slm)
with open("test_slm_multi.log", "w") as f:
stress_recognition(
url,
{
"credentials": credentials,
"log_level": log_level,
"max_wait_seconds": 600,
"log_stream": f,
},
pizza_multi_wav,
lm,
(0, 25),
10,
10,
assertion=multi_assertion,
)
stress_recognition(
url,
{"credentials": credentials, "max_wait_seconds": 600},
pizza_multi_wav,
lm,
(0, 25),
10,
10,
assertion=multi_assertion,
)


def test_grammar_multi():
lm = LanguageModelList(
LanguageModelList.grammar_from_path("pizza", pizza_grammar_path)
)
with open("test_grammar_multi.log", "w") as f:
stress_recognition(
url,
{
"credentials": credentials,
"log_level": log_level,
"max_wait_seconds": 600,
"log_stream": f,
},
pizza_multi_wav,
lm,
(0, 25),
10,
10,
assertion=multi_assertion,
)
stress_recognition(
url,
{"credentials": credentials, "max_wait_seconds": 600},
pizza_multi_wav,
lm,
(0, 25),
10,
10,
assertion=multi_assertion,
)
3 changes: 0 additions & 3 deletions tests/unit/test_noGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
from cpqdasr import SpeechRecognizer, LanguageModelList
from cpqdasr import FileAudioSource
from .config import url, credentials, slm, phone_wav, silence_wav
from .config import log_level, log_path


asr_kwargs = {
"credentials": credentials,
"log_level": log_level,
"log_stream": open(log_path, "w"),
}


Expand Down
4 changes: 1 addition & 3 deletions tests/unit/test_recognizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from cpqdasr import SpeechRecognizer, LanguageModelList
from cpqdasr import FileAudioSource
from .config import url, credentials, phone_wav, silence_wav, yes_wav
from .config import log_level, log_path, phone_grammar_uri, yes_grammar_path
from .config import phone_grammar_uri, yes_grammar_path
import soundfile as sf
import time

Expand All @@ -32,8 +32,6 @@
# =============================================================================
asr_kwargs = {
"credentials": credentials,
"log_level": log_level,
"log_stream": open(log_path, "a"),
}


Expand Down
Loading

0 comments on commit ec5c216

Please sign in to comment.