AlexFSmirnov

Untitled

Mar 5th, 2021
302
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. # import main Flask class and request object
  2. from flask import Flask, request #Flask for post requests
  3. import xml.etree.ElementTree as ET #XML tree toolkit
  4. import os.path, time, torch
  5. from argparse import ArgumentParser
  6. from nemo.collections.asr.metrics.wer import WER, word_error_rate
  7. from nemo.collections.asr.models import EncDecCTCModel
  8. from nemo.utils import logging
  9.  
  10. #Get Phrase from Library
  11. def getPhrase(itemNum):
  12. tree = ET.parse('phrase.xml')
  13. root = tree.getroot()
  14. text = root.find('.//phrase[@id="{value}"]'.format(value=itemNum)).text
  15. print("{}. {}".format(itemNum, text))
  16. return text
  17.  
  18. # Get Key of phrase in-time
  19. def getKey(string):
  20. #!echo $text | phonemize > key.txt
  21. with open('key.txt','r') as file:
  22. key = file.read()
  23. return(key)
  24.  
  25. #Run ASR
  26. def Grade(key, input):
  27. parser = ArgumentParser()
  28. parser.add_argument(
  29. "--asr_model", type=str, default="QuartzNet15x5Base-En", required=True, help="Pass: 'QuartzNet15x5Base-En'",
  30. )
  31. parser.add_argument("--dataset", type=str, required=True, help="path to evaluation data")
  32. parser.add_argument("--batch_size", type=int, default=4)
  33. parser.add_argument("--wer_tolerance", type=float, default=1.0, help="used by test")
  34. parser.add_argument(
  35. "--normalize_text", default=True, type=bool, help="Normalize transcripts or not. Set to False for non-English."
  36. )
  37. args = parser.parse_args(["--dataset", "dataset.json", "--asr_model", "QuartzNet15x5Base-En"])
  38. torch.set_grad_enabled(False)
  39.  
  40. if args.asr_model.endswith('.nemo'):
  41. logging.info(f"Using local ASR model from {args.asr_model}")
  42. asr_model = EncDecCTCModel.restore_from(restore_path=args.asr_model)
  43. else:
  44. logging.info(f"Using NGC cloud ASR model {args.asr_model}")
  45. asr_model = EncDecCTCModel.from_pretrained(model_name=args.asr_model)
  46. asr_model.setup_test_data(
  47. test_data_config={
  48. 'sample_rate': 16000,
  49. 'manifest_filepath': args.dataset,
  50. 'labels': asr_model.decoder.vocabulary,
  51. 'batch_size': args.batch_size,
  52. 'normalize_transcripts': args.normalize_text,
  53. }
  54. )
  55. if can_gpu:
  56. asr_model = asr_model.cuda()
  57. asr_model.eval()
  58. labels_map = dict([(i, asr_model.decoder.vocabulary[i]) for i in range(len(asr_model.decoder.vocabulary))])
  59. wer = WER(vocabulary=asr_model.decoder.vocabulary)
  60. hypotheses = []
  61. references = []
  62. for test_batch in asr_model.test_dataloader():
  63. if can_gpu:
  64. test_batch = [x.cuda() for x in test_batch]
  65. with autocast():
  66. log_probs, encoded_len, greedy_predictions = asr_model(
  67. input_signal=test_batch[0], input_signal_length=test_batch[1]
  68. )
  69. hypotheses += wer.ctc_decoder_predictions_tensor(greedy_predictions)
  70. for batch_ind in range(greedy_predictions.shape[0]):
  71. reference = ''.join([labels_map[c] for c in test_batch[2][batch_ind].cpu().detach().numpy()])
  72. references.append(reference)
  73. del test_batch
  74. wer_value = word_error_rate(hypotheses=hypotheses, references=references)
  75.  
  76. for h, r in zip(hypotheses, references):
  77. print("Recognized:\t{}\nReference:\t{}\n".format(h, r))
  78.  
  79. logging.info(f'Got WER of {wer_value}. Tolerance was {args.wer_tolerance}')
  80. return wer_value
  81.  
  82.  
  83. # create the Flask app
  84. app = Flask(__name__)
  85.  
  86. @app.route('/hole', methods=['POST'])
  87. def query():
  88. # arguments args
  89. if (action == 1): #Run the ASR
  90. # take chunks, convert back to audio
  91. key = getKey(text)
  92. score = Grade(key, text)
  93. return score
  94.  
  95. elif (action == 2):
  96. #
  97. elif (action == 3):
  98. #
  99. ###
  100.  
  101. #chunks = request.args['audio']
  102.  
  103. #always check for chunks file -> call Grade()
  104.  
  105. print(test)
  106. return 'Query String Example'
  107.  
  108.  
  109. if __name__ == '__main__':
  110. # run app in debug mode on port 5000
  111. app.run(debug=True, port=5000)
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment