client.short.wav.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #Some delay to simulate mic input...
  24. time.sleep(0.0078125)
  25. es_system = {}
  26. #Select Spanish system for testing
  27. for system in systems:
  28. if system['info']['langs'][0]['code'] == "es":
  29. es_system = system
  30. if es_system == {}:
  31. raise Exception("Spanish system not found")
  32. for resp in cli.transcribe(es_system['id'], myStreamIterator):
  33. # Hyp_novar contains part of the hypothesis that is consolidated (hypothesis will not change)
  34. if resp["hyp_novar"] != "":
  35. sys.stdout.write("{} ".format(resp["hyp_novar"].strip()))
  36. if resp["eos"]:
  37. sys.stdout.write("\n")
  38. sys.stdout.flush()
  39. if __name__ == "__main__":
  40. if len(sys.argv) != 2:
  41. print("client.short.wav.py <WAV>")
  42. else:
  43. main(sys.argv[1])