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.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 StarForTextToSql
  7. from modelscope.pipelines import pipeline
  8. from modelscope.pipelines.nlp import ConversationalTextToSqlPipeline
  9. from modelscope.preprocessors import ConversationalTextToSqlPreprocessor
  10. from modelscope.utils.constant import Tasks
  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):
  14. model_id = 'damo/nlp_star_conversational-text-to-sql'
  15. test_case = {
  16. 'database_id':
  17. 'employee_hire_evaluation',
  18. 'local_db_path':
  19. None,
  20. 'utterance': [
  21. "I'd like to see Shop names.", 'Which of these are hiring?',
  22. 'Which shop is hiring the highest number of employees? | do you want the name of the shop ? | Yes'
  23. ]
  24. }
  25. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  26. def test_run_by_direct_model_download(self):
  27. cache_path = snapshot_download(self.model_id)
  28. preprocessor = ConversationalTextToSqlPreprocessor(
  29. model_dir=cache_path,
  30. database_id=self.test_case['database_id'],
  31. db_content=True)
  32. model = StarForTextToSql(
  33. model_dir=cache_path, config=preprocessor.config)
  34. pipelines = [
  35. ConversationalTextToSqlPipeline(
  36. model=model, preprocessor=preprocessor),
  37. pipeline(
  38. task=Tasks.conversational_text_to_sql,
  39. model=model,
  40. 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(
  52. task=Tasks.conversational_text_to_sql,
  53. model=model,
  54. preprocessor=preprocessor)
  55. ]
  56. text2sql_tracking_and_print_results(self.test_case, pipelines)
  57. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  58. def test_run_with_model_name(self):
  59. pipelines = [
  60. pipeline(
  61. task=Tasks.conversational_text_to_sql, model=self.model_id)
  62. ]
  63. text2sql_tracking_and_print_results(self.test_case, pipelines)
  64. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  65. def test_run_with_default_model(self):
  66. pipelines = [pipeline(task=Tasks.conversational_text_to_sql)]
  67. text2sql_tracking_and_print_results(self.test_case, pipelines)
  68. if __name__ == '__main__':
  69. unittest.main()