init_sample_data.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. """
  2. 初始化示例数据脚本
  3. 创建一个标准的文本标注项目和任务,用于测试标注功能。
  4. """
  5. import requests
  6. import json
  7. # API 基础 URL
  8. BASE_URL = "http://localhost:8000"
  9. # 文本分类标注配置(LabelStudio XML 格式)
  10. TEXT_CLASSIFICATION_CONFIG = """<View>
  11. <Header value="文本分类标注"/>
  12. <Text name="text" value="$text"/>
  13. <Choices name="sentiment" toName="text" choice="single" showInline="true">
  14. <Choice value="正面"/>
  15. <Choice value="负面"/>
  16. <Choice value="中性"/>
  17. </Choices>
  18. </View>"""
  19. # 命名实体识别标注配置
  20. NER_CONFIG = """<View>
  21. <Header value="命名实体识别"/>
  22. <Text name="text" value="$text"/>
  23. <Labels name="label" toName="text">
  24. <Label value="人名" background="red"/>
  25. <Label value="地名" background="blue"/>
  26. <Label value="机构名" background="green"/>
  27. <Label value="时间" background="orange"/>
  28. </Labels>
  29. </View>"""
  30. # 文本标注配置(高亮标注)
  31. TEXT_HIGHLIGHT_CONFIG = """<View>
  32. <Header value="文本高亮标注"/>
  33. <Text name="text" value="$text"/>
  34. <Labels name="label" toName="text">
  35. <Label value="重要信息" background="yellow"/>
  36. <Label value="关键词" background="lightblue"/>
  37. <Label value="问题" background="pink"/>
  38. </Labels>
  39. </View>"""
  40. # 示例任务数据
  41. SAMPLE_TASKS = [
  42. {
  43. "name": "文本分类任务-1",
  44. "data": {
  45. "text": "这家餐厅的服务态度非常好,菜品也很美味,环境优雅,强烈推荐!"
  46. }
  47. },
  48. {
  49. "name": "文本分类任务-2",
  50. "data": {
  51. "text": "产品质量太差了,用了不到一周就坏了,客服态度也很恶劣,非常失望。"
  52. }
  53. },
  54. {
  55. "name": "文本分类任务-3",
  56. "data": {
  57. "text": "这款手机性能一般,价格适中,适合日常使用。"
  58. }
  59. },
  60. {
  61. "name": "命名实体识别任务-1",
  62. "data": {
  63. "text": "2024年1月15日,张三在北京大学参加了人工智能研讨会。"
  64. }
  65. },
  66. {
  67. "name": "命名实体识别任务-2",
  68. "data": {
  69. "text": "李明是清华大学的教授,他在上海交通大学获得了博士学位。"
  70. }
  71. },
  72. {
  73. "name": "文本高亮任务-1",
  74. "data": {
  75. "text": "机器学习是人工智能的一个重要分支,它使计算机能够从数据中学习并做出决策。深度学习是机器学习的一个子领域,使用神经网络来模拟人脑的工作方式。"
  76. }
  77. }
  78. ]
  79. def create_project(name, description, config):
  80. """创建项目"""
  81. url = f"{BASE_URL}/api/projects"
  82. data = {
  83. "name": name,
  84. "description": description,
  85. "config": config
  86. }
  87. response = requests.post(url, json=data)
  88. if response.status_code == 201:
  89. project = response.json()
  90. print(f"✓ 创建项目成功: {project['name']} (ID: {project['id']})")
  91. return project
  92. else:
  93. print(f"✗ 创建项目失败: {response.status_code} - {response.text}")
  94. return None
  95. def create_task(project_id, task_name, task_data):
  96. """创建任务"""
  97. url = f"{BASE_URL}/api/tasks"
  98. data = {
  99. "project_id": project_id,
  100. "name": task_name,
  101. "data": task_data
  102. }
  103. response = requests.post(url, json=data)
  104. if response.status_code == 201:
  105. task = response.json()
  106. print(f" ✓ 创建任务: {task['name']} (ID: {task['id']})")
  107. return task
  108. else:
  109. print(f" ✗ 创建任务失败: {response.status_code} - {response.text}")
  110. return None
  111. def main():
  112. """主函数"""
  113. print("=" * 60)
  114. print("初始化标注平台示例数据")
  115. print("=" * 60)
  116. print()
  117. # 1. 创建文本分类项目
  118. print("1. 创建文本分类项目...")
  119. classification_project = create_project(
  120. name="情感分析标注项目",
  121. description="对用户评论进行情感分类(正面/负面/中性)",
  122. config=TEXT_CLASSIFICATION_CONFIG
  123. )
  124. if classification_project:
  125. print(" 创建文本分类任务...")
  126. for i in range(3):
  127. create_task(
  128. classification_project['id'],
  129. SAMPLE_TASKS[i]['name'],
  130. SAMPLE_TASKS[i]['data']
  131. )
  132. print()
  133. # 2. 创建命名实体识别项目
  134. print("2. 创建命名实体识别项目...")
  135. ner_project = create_project(
  136. name="命名实体识别项目",
  137. description="识别文本中的人名、地名、机构名和时间等实体",
  138. config=NER_CONFIG
  139. )
  140. if ner_project:
  141. print(" 创建命名实体识别任务...")
  142. for i in range(3, 5):
  143. create_task(
  144. ner_project['id'],
  145. SAMPLE_TASKS[i]['name'],
  146. SAMPLE_TASKS[i]['data']
  147. )
  148. print()
  149. # 3. 创建文本高亮项目
  150. print("3. 创建文本高亮标注项目...")
  151. highlight_project = create_project(
  152. name="文本高亮标注项目",
  153. description="标记文本中的重要信息、关键词和问题",
  154. config=TEXT_HIGHLIGHT_CONFIG
  155. )
  156. if highlight_project:
  157. print(" 创建文本高亮任务...")
  158. create_task(
  159. highlight_project['id'],
  160. SAMPLE_TASKS[5]['name'],
  161. SAMPLE_TASKS[5]['data']
  162. )
  163. print()
  164. print("=" * 60)
  165. print("示例数据初始化完成!")
  166. print("=" * 60)
  167. print()
  168. print("你现在可以:")
  169. print("1. 访问 http://localhost:4200/projects 查看项目列表")
  170. print("2. 点击项目查看详情和任务")
  171. print("3. 点击'开始标注'按钮进行标注")
  172. print()
  173. if __name__ == "__main__":
  174. try:
  175. main()
  176. except requests.exceptions.ConnectionError:
  177. print("✗ 错误: 无法连接到后端服务器")
  178. print(" 请确保后端服务器正在运行: python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000")
  179. except Exception as e:
  180. print(f"✗ 发生错误: {e}")