client.short.wav.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import json
  2. import sys
  3. from MLLPStreamingClient import MLLPStreamingClient
  4. def main(wav):
  5. server_hostname = "<SERVER_ADDRESS>"
  6. server_port = "<PORT>"
  7. api_user = "<YOU_API_USER>"
  8. api_secret = "<YOUR_API_KEY>"
  9. server_ssl_cert_file = "<CRT_FILE>"
  10. #Client object creation
  11. cli = MLLPStreamingClient(server_hostname, server_port, api_user,api_secret, server_ssl_cert_file)
  12. #Get Token for the session
  13. cli.getAuthToken()
  14. #Get available systems, a dictionary with the information related to the available systems and languages
  15. systems = cli.getTranscribeSystemsInfo()
  16. #Audio streaming iterator, sends audio in chunks of 250 bytes
  17. def myStreamIterator():
  18. with open(wav,"rb") as fd:
  19. data = fd.read(250)
  20. while data != b"":
  21. yield data
  22. data = fd.read(250)
  23. es_system = {}
  24. #Select Spanish system for testing
  25. for system in systems:
  26. if system['info']['langs'][0]['code'] == "es":
  27. es_system = system
  28. if es_system == {}:
  29. raise Exception("Spanish system not found")
  30. for resp in cli.transcribe(es_system['id'], myStreamIterator):
  31. # Hyp_novar contains part of the hypothesis that is consolidated (hypothesis will not change)
  32. if resp["hyp_novar"] != "":
  33. sys.stdout.write("{} ".format(resp["hyp_novar"].strip()))
  34. if resp["eos"]:
  35. sys.stdout.write("\n")
  36. sys.stdout.flush()
  37. if __name__ == "__main__":
  38. if len(sys.argv) != 2:
  39. print("client.short.wav.py <WAV>")
  40. else:
  41. main(sys.argv[1])