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_conversational_text_to_sql.py 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 StarForTextToSql
  6. from modelscope.pipelines import pipeline
  7. from modelscope.pipelines.nlp import ConversationalTextToSqlPipeline
  8. from modelscope.preprocessors import ConversationalTextToSqlPreprocessor
  9. from modelscope.utils.constant import Tasks
  10. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  11. from modelscope.utils.nlp.nlp_utils import text2sql_tracking_and_print_results
  12. from modelscope.utils.test_utils import test_level
  13. class ConversationalTextToSql(unittest.TestCase, DemoCompatibilityCheck):
  14. def setUp(self) -> None:
  15. self.task = Tasks.table_question_answering
  16. self.model_id = 'damo/nlp_star_conversational-text-to-sql'
  17. model_id = 'damo/nlp_star_conversational-text-to-sql'
  18. test_case = {
  19. 'database_id':
  20. 'employee_hire_evaluation',
  21. 'local_db_path':
  22. None,
  23. 'utterance': [
  24. "I'd like to see Shop names.", 'Which of these are hiring?',
  25. 'Which shop is hiring the highest number of employees? | do you want the name of the shop ? | Yes'
  26. ]
  27. }
  28. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  29. def test_run_by_direct_model_download(self):
  30. cache_path = snapshot_download(self.model_id)
  31. preprocessor = ConversationalTextToSqlPreprocessor(
  32. model_dir=cache_path,
  33. database_id=self.test_case['database_id'],
  34. db_content=True)
  35. model = StarForTextToSql(
  36. model_dir=cache_path, config=preprocessor.config)
  37. pipelines = [
  38. ConversationalTextToSqlPipeline(
  39. model=model, preprocessor=preprocessor),
  40. pipeline(task=self.task, model=model, preprocessor=preprocessor)
  41. ]
  42. text2sql_tracking_and_print_results(self.test_case, pipelines)
  43. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  44. def test_run_with_model_from_modelhub(self):
  45. model = Model.from_pretrained(self.model_id)
  46. preprocessor = ConversationalTextToSqlPreprocessor(
  47. model_dir=model.model_dir)
  48. pipelines = [
  49. ConversationalTextToSqlPipeline(
  50. model=model, preprocessor=preprocessor),
  51. pipeline(task=self.task, model=model, preprocessor=preprocessor)
  52. ]
  53. text2sql_tracking_and_print_results(self.test_case, pipelines)
  54. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  55. def test_run_with_model_name(self):
  56. pipelines = [pipeline(task=self.task, model=self.model_id)]
  57. text2sql_tracking_and_print_results(self.test_case, pipelines)
  58. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  59. def test_demo_compatibility(self):
  60. self.compatibility_check()
  61. if __name__ == '__main__':
  62. unittest.main()