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.

client.rst 9.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. ============================================================
  2. Learnware Client
  3. ============================================================
  4. Introduction
  5. ====================
  6. ``Learnware Client`` is a ``Python API`` that provides a convenient interface for interacting with the ``BeimingWu`` system. You can easily use the client to upload, download, delete, update, and search learnwares.
  7. Prepare access token
  8. ====================
  9. Before using the ``Learnware Client``, you'll need to obtain a token from the `official website <https://bmwu.cloud/>`_. Just login to the website and click ``Client Token`` tab in the ``Personal Information``.
  10. How to Use Client
  11. ============================
  12. Initialize a Learware Client
  13. -------------------------------
  14. .. code-block:: python
  15. from learnware.client import LearnwareClient, SemanticSpecificationKey
  16. # Login to Beiming system
  17. client = LearnwareClient()
  18. client.login(email="your email", token="your token")
  19. Where email is the registered mailbox of the system and token is the token obtained in the previous section.
  20. Upload Leanware
  21. -------------------------------
  22. Before uploading a learnware, you'll need to prepare the semantic specification of your learnware. Let's take the classification task for tabular data as an example. You can create a semantic specification by a helper function ``create_semantic_specification``.
  23. .. code-block:: python
  24. # Prepare input description when data_type="Table"
  25. input_description = {
  26. "Dimension": 5,
  27. "Description": {
  28. "0": "age",
  29. "1": "weight",
  30. "2": "body length",
  31. "3": "animal type",
  32. "4": "claw length"
  33. },
  34. }
  35. # Prepare output description when task_type in ["Classification", "Regression"]
  36. output_description = {
  37. "Dimension": 3,
  38. "Description": {
  39. "0": "cat",
  40. "1": "dog",
  41. "2": "bird",
  42. },
  43. }
  44. # Create semantic specification
  45. semantic_spec = client.create_semantic_specification(
  46. name="learnware_example",
  47. description="Just a example for uploading a learnware",
  48. data_type="Table",
  49. task_type="Classification",
  50. library_type="Scikit-learn",
  51. scenarios=["Business", "Financial"],
  52. license=["Apache-2.0"],
  53. input_description=input_description,
  54. output_description=output_description,
  55. )
  56. Ensure that the input parameters for the semantic specification fall within the specified ranges provided by ``client.list_semantic_specification_values(key)``:
  57. * "data_type" must be within the range of ``key=SemanticSpecificationKey.DATA_TYPE``.
  58. * "task_type" must be within the range of ``key=SemanticSpecificationKey.TASK_TYPE``.
  59. * "library_type" must be within the range of ``key=SemanticSpecificationKey.LIBRARY_TYPE``.
  60. * "scenarios" must be a subset of ``key=SemanticSpecificationKey.SENARIOS``.
  61. * "license" must be a subset of ``key=SemanticSpecificationKey.LICENSE``.
  62. * When "data_type" is set to "Table", it is necessary to provide "input_description".
  63. * When "task_type" is either "Classification" or "Regression", it is necessary to provide "output_description".
  64. Finally, the semantic specification and the zip package path of the learnware were filled in to upload the learnware.
  65. Remember to verify the learnware before uploading it, as shown in the following code example:
  66. .. code-block:: python
  67. # Prepare your learnware zip file
  68. zip_path = "your learnware zip"
  69. # Check your learnware before upload
  70. client.check_learnware(
  71. learnware_zip_path=zip_path, semantic_specification=semantic_spec
  72. )
  73. # Upload your learnware
  74. learnware_id = client.upload_learnware(
  75. learnware_zip_path=zip_path, semantic_specification=semantic_spec
  76. )
  77. After uploading the learnware successfully, you can see it in ``My Learnware``, the background will check it. Click on the learnware, which can be viewed in the ``Verify Status``. After the check passes, the Unverified tag of the learnware will disappear, and the uploaded learnware will appear in the system.
  78. Update Learnware
  79. -------------------------------
  80. The ``update_learnware`` method is used to update the metadata and content of an existing learnware on the server. You can upload a new semantic specification, or directly upload a new learnware.
  81. .. code-block:: python
  82. # Replace with the actual learnware ID
  83. learnware_id = "123456789"
  84. # Create new semantic specification
  85. semantic_spec = client.create_semantic_specification(
  86. name="new learnware name",
  87. description="new description",
  88. data_type="Table",
  89. task_type="Classification",
  90. library_type="Scikit-learn",
  91. scenarios=["Computer", "Internet"],
  92. license=["CC-BY-4.0"],
  93. input_description=new_input_description,
  94. output_description=new_output_description,
  95. )
  96. # Update metadata without changing the content
  97. client.update_learnware(learnware_id, semantic_spec)
  98. # Update metadata and content with a new ZIP file
  99. updated_zip_path = "/path/to/updated_learnware.zip"
  100. client.update_learnware(learnware_id, semantic_spec, learnware_zip_path=updated_zip_path)
  101. Delete Learnware
  102. -------------------------------
  103. The ``delete_learnware`` method is used to delete a learnware from the server.
  104. .. code-block:: python
  105. # Replace with the actual learnware ID to delete
  106. learnware_id = "123456789"
  107. # Delete the specified learnware
  108. client.delete_learnware(learnware_id)
  109. Semantic Specification Search
  110. -------------------------------
  111. You can search the learnware in the system through the semantic specification, and all the learnware conforming to the semantic specification will be returned through the API. For example, the following code will give you all the learnware in the system whose task type is classified:
  112. .. code-block:: python
  113. from learnware.market import BaseUserInfo
  114. user_semantic = client.create_semantic_specification(
  115. task_type="Classification"
  116. )
  117. user_info = BaseUserInfo(semantic_spec=user_semantic)
  118. learnware_list = client.search_learnware(user_info, page_size=None)
  119. Statistical Specification Search
  120. ---------------------------------
  121. You can also search the learnware in the system through the statistical specification, and all the learnware with similar distribution will be returned through the API. Using the ``generate_stat_spec`` function mentioned above, you can easily get the ``stat_spec`` for your current task, and then get the learnware that meets the statistical specification for the same type of data in the system by using the following code:
  122. .. code-block:: python
  123. user_info = BaseUserInfo(stat_info={stat_spec.type: stat_spec})
  124. learnware_list = client.search_learnware(user_info, page_size=None)
  125. Combine Semantic and Statistical Search
  126. ----------------------------------------
  127. By combining statistical and semantic specifications, you can perform more detailed searches, such as the following code that searches tabular data for pieces of learnware that satisfy your semantic specifications:
  128. .. code-block:: python
  129. user_semantic = client.create_semantic_specification(
  130. task_type="Classification",
  131. scenarios=["Business"],
  132. )
  133. rkme_table = generate_stat_spec(type="table", X=train_x)
  134. user_info = BaseUserInfo(
  135. semantic_spec=user_semantic, stat_info={rkme_table.type: rkme_table}
  136. )
  137. learnware_list = client.search_learnware(user_info, page_size=None)
  138. Heterogeneous Table Search
  139. ----------------------------------------
  140. When you provide a statistical specification for tabular data, the task type is "Classification" or "Regression", and your semantic specification includes descriptions for each dimension, the system will automatically enable heterogeneous table search. It won't only search in the tabular learnwares with same dimensions. The following code will perform heterogeneous table search through the API:
  141. .. code-block:: python
  142. input_description = {
  143. "Dimension": 2,
  144. "Description": {
  145. "0": "leaf width",
  146. "1": "leaf length",
  147. },
  148. }
  149. user_semantic = client.create_semantic_specification(
  150. task_type="Classification",
  151. scenarios=["Business"],
  152. input_description=input_description,
  153. )
  154. rkme_table = generate_stat_spec(type="table", X=train_x)
  155. user_info = BaseUserInfo(
  156. semantic_spec=user_semantic, stat_info={rkme_table.type: rkme_table}
  157. )
  158. learnware_list = client.search_learnware(user_info)
  159. Download and Use Learnware
  160. -------------------------------
  161. When the search is complete, you can download the learnware and configure the environment through the following code:
  162. .. code-block:: python
  163. for temp_learnware in learnware_list:
  164. learnware_id = temp_learnware["learnware_id"]
  165. # you can use the learnware to make prediction now
  166. learnware = client.load_learnware(
  167. learnware_id=learnware_id, runnable_option="conda"
  168. )