You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_dialog_modeling.py 6.9 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import unittest
  3. from typing import List
  4. from modelscope.hub.snapshot_download import snapshot_download
  5. from modelscope.models import Model
  6. from modelscope.models.nlp import SpaceForDialogModeling
  7. from modelscope.pipelines import pipeline
  8. from modelscope.pipelines.nlp import DialogModelingPipeline
  9. from modelscope.preprocessors import DialogModelingPreprocessor
  10. from modelscope.utils.constant import Tasks
  11. from modelscope.utils.test_utils import test_level
  12. class DialogModelingTest(unittest.TestCase):
  13. model_id = 'damo/nlp_space_dialog-modeling'
  14. test_case = {
  15. 'sng0073': {
  16. 'goal': {
  17. 'taxi': {
  18. 'info': {
  19. 'leaveat': '17:15',
  20. 'destination': 'pizza hut fen ditton',
  21. 'departure': "saint john's college"
  22. },
  23. 'reqt': ['car', 'phone'],
  24. 'fail_info': {}
  25. }
  26. },
  27. 'log': [{
  28. 'user':
  29. "i would like a taxi from saint john 's college to pizza hut fen ditton .",
  30. 'user_delex':
  31. 'i would like a taxi from [value_departure] to [value_destination] .',
  32. 'resp':
  33. 'what time do you want to leave and what time do you want to arrive by ?',
  34. 'sys':
  35. 'what time do you want to leave and what time do you want to arrive by ?',
  36. 'pointer': '0,0,0,0,0,0',
  37. 'match': '',
  38. 'constraint':
  39. "[taxi] destination pizza hut fen ditton departure saint john 's college",
  40. 'cons_delex': '[taxi] destination departure',
  41. 'sys_act': '[taxi] [request] leave arrive',
  42. 'turn_num': 0,
  43. 'turn_domain': '[taxi]'
  44. }, {
  45. 'user': 'i want to leave after 17:15 .',
  46. 'user_delex': 'i want to leave after [value_leave] .',
  47. 'resp':
  48. 'booking completed ! your taxi will be [value_car] contact number is [value_phone]',
  49. 'sys':
  50. 'booking completed ! your taxi will be blue honda contact number is 07218068540',
  51. 'pointer': '0,0,0,0,0,0',
  52. 'match': '',
  53. 'constraint':
  54. "[taxi] destination pizza hut fen ditton departure saint john 's college leave 17:15",
  55. 'cons_delex': '[taxi] destination departure leave',
  56. 'sys_act': '[taxi] [inform] car phone',
  57. 'turn_num': 1,
  58. 'turn_domain': '[taxi]'
  59. }, {
  60. 'user': 'thank you for all the help ! i appreciate it .',
  61. 'user_delex': 'thank you for all the help ! i appreciate it .',
  62. 'resp':
  63. 'you are welcome . is there anything else i can help you with today ?',
  64. 'sys':
  65. 'you are welcome . is there anything else i can help you with today ?',
  66. 'pointer': '0,0,0,0,0,0',
  67. 'match': '',
  68. 'constraint':
  69. "[taxi] destination pizza hut fen ditton departure saint john 's college leave 17:15",
  70. 'cons_delex': '[taxi] destination departure leave',
  71. 'sys_act': '[general] [reqmore]',
  72. 'turn_num': 2,
  73. 'turn_domain': '[general]'
  74. }, {
  75. 'user': 'no , i am all set . have a nice day . bye .',
  76. 'user_delex': 'no , i am all set . have a nice day . bye .',
  77. 'resp': 'you too ! thank you',
  78. 'sys': 'you too ! thank you',
  79. 'pointer': '0,0,0,0,0,0',
  80. 'match': '',
  81. 'constraint':
  82. "[taxi] destination pizza hut fen ditton departure saint john 's college leave 17:15",
  83. 'cons_delex': '[taxi] destination departure leave',
  84. 'sys_act': '[general] [bye]',
  85. 'turn_num': 3,
  86. 'turn_domain': '[general]'
  87. }]
  88. }
  89. }
  90. def generate_and_print_dialog_response(
  91. self, pipelines: List[DialogModelingPipeline]):
  92. result = {}
  93. for step, item in enumerate(self.test_case['sng0073']['log']):
  94. user = item['user']
  95. print('user: {}'.format(user))
  96. result = pipelines[step % 2]({
  97. 'user_input': user,
  98. 'history': result
  99. })
  100. print('response : {}'.format(result['response']))
  101. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  102. def test_run_by_direct_model_download(self):
  103. cache_path = snapshot_download(self.model_id)
  104. preprocessor = DialogModelingPreprocessor(model_dir=cache_path)
  105. model = SpaceForDialogModeling(
  106. model_dir=cache_path,
  107. text_field=preprocessor.text_field,
  108. config=preprocessor.config,
  109. device='cpu')
  110. pipelines = [
  111. DialogModelingPipeline(
  112. model=model, preprocessor=preprocessor, device='cpu'),
  113. pipeline(
  114. task=Tasks.dialog_modeling,
  115. model=model,
  116. preprocessor=preprocessor,
  117. device='cpu')
  118. ]
  119. self.generate_and_print_dialog_response(pipelines)
  120. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  121. def test_run_with_model_from_modelhub(self):
  122. model = Model.from_pretrained(self.model_id)
  123. preprocessor = DialogModelingPreprocessor(
  124. model_dir=model.model_dir, device='cpu')
  125. pipelines = [
  126. DialogModelingPipeline(
  127. model=model, preprocessor=preprocessor, device='cpu'),
  128. pipeline(
  129. task=Tasks.dialog_modeling,
  130. model=model,
  131. preprocessor=preprocessor,
  132. device='cpu')
  133. ]
  134. self.generate_and_print_dialog_response(pipelines)
  135. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  136. def test_run_with_model_name(self):
  137. pipelines = [
  138. pipeline(
  139. task=Tasks.dialog_modeling, model=self.model_id, device='cpu'),
  140. pipeline(
  141. task=Tasks.dialog_modeling, model=self.model_id, device='cpu')
  142. ]
  143. self.generate_and_print_dialog_response(pipelines)
  144. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  145. def test_run_with_default_model(self):
  146. pipelines = [
  147. pipeline(task=Tasks.dialog_modeling, device='cpu'),
  148. pipeline(task=Tasks.dialog_modeling, device='cpu')
  149. ]
  150. self.generate_and_print_dialog_response(pipelines)
  151. if __name__ == '__main__':
  152. unittest.main()