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_table_question_answering.py 7.6 kB

[to #42322933] NLP 1030 Refactor Features: 1. Refactor the directory structure of nlp models. All model files are placed into either the model folder or the task_model folder 2. Refactor all the comments to google style 3. Add detail comments to important tasks and nlp models, to list the description of the model, and its preprocessor&trainer 4. Model Exporting now supports a direct all to TorchModelExporter(no need to derive from it) 5. Refactor model save_pretrained method to support direct running(independent from trainer) 6. Remove the judgement of Model in the pipeline base class, to support outer register models running in our pipelines 7. Nlp trainer now has a NLPTrainingArguments class , user can pass arguments into the dataclass, and use it as a normal cfg_modify_fn, to simplify the operation of modify cfg. 8. Merge the BACKBONES and the MODELS, so user can get a backbone with the Model.from_pretrained call 9. Model.from_pretrained now support a task argument, so user can use a backbone and load it with a specific task class. 10. Support Preprocessor.from_pretrained method 11. Add standard return classes to important nlp tasks, so some of the pipelines and the models are independent now, the return values of the models will always be tensors, and the pipelines will take care of the conversion to numpy and the following stuffs. 12. Split the file of the nlp preprocessors, to make the dir structure more clear. Bugs Fixing: 1. Fix a bug that lr_scheduler can be called earlier than the optimizer's step 2. Fix a bug that the direct call of Pipelines (not from pipeline(xxx)) throws error 3. Fix a bug that the trainer will not call the correct TaskDataset class 4. Fix a bug that the internal loading of dataset will throws error in the trainer class Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/10490585
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os
  3. import unittest
  4. from threading import Thread
  5. from typing import List
  6. import json
  7. from transformers import BertTokenizer
  8. from modelscope.hub.snapshot_download import snapshot_download
  9. from modelscope.models import Model
  10. from modelscope.outputs import OutputKeys
  11. from modelscope.pipelines import pipeline
  12. from modelscope.pipelines.nlp import TableQuestionAnsweringPipeline
  13. from modelscope.preprocessors import TableQuestionAnsweringPreprocessor
  14. from modelscope.preprocessors.nlp.space_T_cn.fields.database import Database
  15. from modelscope.utils.constant import ModelFile, Tasks
  16. from modelscope.utils.test_utils import test_level
  17. def tableqa_tracking_and_print_results_with_history(
  18. pipelines: List[TableQuestionAnsweringPipeline]):
  19. test_case = {
  20. 'utterance': [
  21. '有哪些风险类型?',
  22. '风险类型有多少种?',
  23. '珠江流域的小型水库的库容总量是多少?',
  24. '那平均值是多少?',
  25. '那水库的名称呢?',
  26. '换成中型的呢?',
  27. ]
  28. }
  29. for p in pipelines:
  30. historical_queries = None
  31. for question in test_case['utterance']:
  32. output_dict = p({
  33. 'question': question,
  34. 'history_sql': historical_queries
  35. })[OutputKeys.OUTPUT]
  36. print('question', question)
  37. print('sql text:', output_dict[OutputKeys.SQL_STRING])
  38. print('sql query:', output_dict[OutputKeys.SQL_QUERY])
  39. print('query result:', output_dict[OutputKeys.QUERT_RESULT])
  40. print('json dumps', json.dumps(output_dict, ensure_ascii=False))
  41. print()
  42. historical_queries = output_dict[OutputKeys.HISTORY]
  43. def tableqa_tracking_and_print_results_without_history(
  44. pipelines: List[TableQuestionAnsweringPipeline]):
  45. test_case = {
  46. 'utterance': [
  47. '有哪些风险类型?',
  48. '风险类型有多少种?',
  49. '珠江流域的小型水库的库容总量是多少?',
  50. ]
  51. }
  52. for p in pipelines:
  53. for question in test_case['utterance']:
  54. output_dict = p({'question': question})[OutputKeys.OUTPUT]
  55. print('question', question)
  56. print('sql text:', output_dict[OutputKeys.SQL_STRING])
  57. print('sql query:', output_dict[OutputKeys.SQL_QUERY])
  58. print('query result:', output_dict[OutputKeys.QUERT_RESULT])
  59. print('json dumps', json.dumps(output_dict, ensure_ascii=False))
  60. print()
  61. def tableqa_tracking_and_print_results_with_tableid(
  62. pipelines: List[TableQuestionAnsweringPipeline]):
  63. test_case = {
  64. 'utterance': [
  65. ['有哪些风险类型?', 'fund'],
  66. ['风险类型有多少种?', 'reservoir'],
  67. ['珠江流域的小型水库的库容总量是多少?', 'reservoir'],
  68. ['那平均值是多少?', 'reservoir'],
  69. ['那水库的名称呢?', 'reservoir'],
  70. ['换成中型的呢?', 'reservoir'],
  71. ],
  72. }
  73. for p in pipelines:
  74. historical_queries = None
  75. for question, table_id in test_case['utterance']:
  76. output_dict = p({
  77. 'question': question,
  78. 'table_id': table_id,
  79. 'history_sql': historical_queries
  80. })[OutputKeys.OUTPUT]
  81. print('question', question)
  82. print('sql text:', output_dict[OutputKeys.SQL_STRING])
  83. print('sql query:', output_dict[OutputKeys.SQL_QUERY])
  84. print('query result:', output_dict[OutputKeys.QUERT_RESULT])
  85. print('json dumps', json.dumps(output_dict, ensure_ascii=False))
  86. print()
  87. historical_queries = output_dict[OutputKeys.HISTORY]
  88. class TableQuestionAnswering(unittest.TestCase):
  89. def setUp(self) -> None:
  90. self.task = Tasks.table_question_answering
  91. self.model_id = 'damo/nlp_convai_text2sql_pretrain_cn'
  92. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  93. def test_run_by_direct_model_download(self):
  94. cache_path = snapshot_download(self.model_id)
  95. preprocessor = TableQuestionAnsweringPreprocessor(model_dir=cache_path)
  96. pipelines = [
  97. pipeline(
  98. Tasks.table_question_answering,
  99. model=cache_path,
  100. preprocessor=preprocessor)
  101. ]
  102. tableqa_tracking_and_print_results_with_history(pipelines)
  103. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  104. def test_run_by_direct_model_download_with_multithreads(self):
  105. cache_path = snapshot_download(self.model_id)
  106. pl = pipeline(Tasks.table_question_answering, model=cache_path)
  107. def print_func(pl, i):
  108. result = pl({
  109. 'question': '上个月收益从低到高排前七的基金的名称和风险等级是什么',
  110. 'table_id': 'fund',
  111. 'history_sql': None
  112. })
  113. print(i, result[OutputKeys.OUTPUT][OutputKeys.SQL_QUERY],
  114. result[OutputKeys.OUTPUT][OutputKeys.QUERT_RESULT],
  115. json.dumps(result))
  116. procs = []
  117. for i in range(5):
  118. proc = Thread(target=print_func, args=(pl, i))
  119. procs.append(proc)
  120. proc.start()
  121. for proc in procs:
  122. proc.join()
  123. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  124. def test_run_with_model_from_modelhub(self):
  125. model = Model.from_pretrained(self.model_id)
  126. self.tokenizer = BertTokenizer(
  127. os.path.join(model.model_dir, ModelFile.VOCAB_FILE))
  128. db = Database(
  129. tokenizer=self.tokenizer,
  130. table_file_path=[
  131. os.path.join(model.model_dir, 'databases', fname)
  132. for fname in os.listdir(
  133. os.path.join(model.model_dir, 'databases'))
  134. ],
  135. syn_dict_file_path=os.path.join(model.model_dir, 'synonym.txt'),
  136. is_use_sqlite=True)
  137. preprocessor = TableQuestionAnsweringPreprocessor(
  138. model_dir=model.model_dir, db=db)
  139. pipelines = [
  140. pipeline(
  141. Tasks.table_question_answering,
  142. model=model,
  143. preprocessor=preprocessor,
  144. db=db)
  145. ]
  146. tableqa_tracking_and_print_results_with_tableid(pipelines)
  147. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  148. def test_run_with_model_from_modelhub_with_other_classes(self):
  149. model = Model.from_pretrained(self.model_id)
  150. self.tokenizer = BertTokenizer(
  151. os.path.join(model.model_dir, ModelFile.VOCAB_FILE))
  152. db = Database(
  153. tokenizer=self.tokenizer,
  154. table_file_path=[
  155. os.path.join(model.model_dir, 'databases', fname)
  156. for fname in os.listdir(
  157. os.path.join(model.model_dir, 'databases'))
  158. ],
  159. syn_dict_file_path=os.path.join(model.model_dir, 'synonym.txt'),
  160. is_use_sqlite=True)
  161. preprocessor = TableQuestionAnsweringPreprocessor(
  162. model_dir=model.model_dir, db=db)
  163. pipelines = [
  164. pipeline(
  165. Tasks.table_question_answering,
  166. model=model,
  167. preprocessor=preprocessor,
  168. db=db)
  169. ]
  170. tableqa_tracking_and_print_results_without_history(pipelines)
  171. if __name__ == '__main__':
  172. unittest.main()