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_state_tracking.py 5.0 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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import unittest
  3. from modelscope.hub.snapshot_download import snapshot_download
  4. from modelscope.models import Model
  5. from modelscope.models.nlp import SpaceForDialogStateTracking
  6. from modelscope.pipelines import pipeline
  7. from modelscope.pipelines.nlp import DialogStateTrackingPipeline
  8. from modelscope.preprocessors import DialogStateTrackingPreprocessor
  9. from modelscope.utils.constant import Tasks
  10. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  11. from modelscope.utils.nlp.nlp_utils import tracking_and_print_dialog_states
  12. from modelscope.utils.test_utils import test_level
  13. class DialogStateTrackingTest(unittest.TestCase, DemoCompatibilityCheck):
  14. def setUp(self) -> None:
  15. self.task = Tasks.task_oriented_conversation
  16. self.model_id = 'damo/nlp_space_dialog-state-tracking'
  17. test_case = [{
  18. 'User-1':
  19. 'Hi, I\'m looking for a train that is going to cambridge and arriving there by 20:45, '
  20. 'is there anything like that?'
  21. }, {
  22. 'System-1':
  23. 'There are over 1,000 trains like that. Where will you be departing from?',
  24. 'Dialog_Act-1': {
  25. 'Train-Inform': [['Choice', 'over 1'], ['Choice', '000']],
  26. 'Train-Request': [['Depart', '?']]
  27. },
  28. 'User-2': 'I am departing from birmingham new street.'
  29. }, {
  30. 'System-2': 'Can you confirm your desired travel day?',
  31. 'Dialog_Act-2': {
  32. 'Train-Request': [['Day', '?']]
  33. },
  34. 'User-3': 'I would like to leave on wednesday'
  35. }, {
  36. 'System-3':
  37. 'I show a train leaving birmingham new street at 17:40 and arriving at 20:23 on Wednesday. '
  38. 'Will this work for you?',
  39. 'Dialog_Act-3': {
  40. 'Train-Inform': [['Arrive', '20:23'], ['Leave', '17:40'],
  41. ['Day', 'Wednesday'],
  42. ['Depart', 'birmingham new street']]
  43. },
  44. 'User-4':
  45. 'That will, yes. Please make a booking for 5 people please.',
  46. }, {
  47. 'System-4':
  48. 'I\'ve booked your train tickets, and your reference number is A9NHSO9Y.',
  49. 'Dialog_Act-4': {
  50. 'Train-OfferBooked': [['Ref', 'A9NHSO9Y']]
  51. },
  52. 'User-5':
  53. 'Thanks so much. I would also need a place to say. '
  54. 'I am looking for something with 4 stars and has free wifi.'
  55. }, {
  56. 'System-5':
  57. 'How about the cambridge belfry? '
  58. 'It has all the attributes you requested and a great name! '
  59. 'Maybe even a real belfry?',
  60. 'Dialog_Act-5': {
  61. 'Hotel-Recommend': [['Name', 'the cambridge belfry']]
  62. },
  63. 'User-6':
  64. 'That sounds great, could you make a booking for me please?',
  65. }, {
  66. 'System-6':
  67. 'What day would you like your booking for?',
  68. 'Dialog_Act-6': {
  69. 'Booking-Request': [['Day', '?']]
  70. },
  71. 'User-7':
  72. 'Please book it for Wednesday for 5 people and 5 nights, please.',
  73. }, {
  74. 'System-7': 'Booking was successful. Reference number is : 5NAWGJDC.',
  75. 'Dialog_Act-7': {
  76. 'Booking-Book': [['Ref', '5NAWGJDC']]
  77. },
  78. 'User-8': 'Thank you, goodbye',
  79. }]
  80. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  81. def test_run_by_direct_model_download(self):
  82. cache_path = snapshot_download(self.model_id, revision='update')
  83. model = SpaceForDialogStateTracking(cache_path)
  84. preprocessor = DialogStateTrackingPreprocessor(model_dir=cache_path)
  85. pipelines = [
  86. DialogStateTrackingPipeline(
  87. model=model, preprocessor=preprocessor),
  88. pipeline(
  89. task=Tasks.task_oriented_conversation,
  90. model=model,
  91. preprocessor=preprocessor)
  92. ]
  93. tracking_and_print_dialog_states(self.test_case, pipelines)
  94. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  95. def test_run_with_model_from_modelhub(self):
  96. model = Model.from_pretrained(self.model_id, revision='update')
  97. preprocessor = DialogStateTrackingPreprocessor(
  98. model_dir=model.model_dir)
  99. pipelines = [
  100. DialogStateTrackingPipeline(
  101. model=model, preprocessor=preprocessor),
  102. pipeline(task=self.task, model=model, preprocessor=preprocessor)
  103. ]
  104. tracking_and_print_dialog_states(self.test_case, pipelines)
  105. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  106. def test_run_with_model_name(self):
  107. pipelines = [
  108. pipeline(
  109. task=self.task, model=self.model_id, model_revision='update')
  110. ]
  111. tracking_and_print_dialog_states(self.test_case, pipelines)
  112. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  113. def test_demo_compatibility(self):
  114. self.compatibility_check()
  115. if __name__ == '__main__':
  116. unittest.main()