| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- """
- 初始化示例数据脚本
- 创建一个标准的文本标注项目和任务,用于测试标注功能。
- """
- import requests
- import json
- # API 基础 URL
- BASE_URL = "http://localhost:8000"
- # 文本分类标注配置(LabelStudio XML 格式)
- TEXT_CLASSIFICATION_CONFIG = """<View>
- <Header value="文本分类标注"/>
- <Text name="text" value="$text"/>
- <Choices name="sentiment" toName="text" choice="single" showInline="true">
- <Choice value="正面"/>
- <Choice value="负面"/>
- <Choice value="中性"/>
- </Choices>
- </View>"""
- # 命名实体识别标注配置
- NER_CONFIG = """<View>
- <Header value="命名实体识别"/>
- <Text name="text" value="$text"/>
- <Labels name="label" toName="text">
- <Label value="人名" background="red"/>
- <Label value="地名" background="blue"/>
- <Label value="机构名" background="green"/>
- <Label value="时间" background="orange"/>
- </Labels>
- </View>"""
- # 文本标注配置(高亮标注)
- TEXT_HIGHLIGHT_CONFIG = """<View>
- <Header value="文本高亮标注"/>
- <Text name="text" value="$text"/>
- <Labels name="label" toName="text">
- <Label value="重要信息" background="yellow"/>
- <Label value="关键词" background="lightblue"/>
- <Label value="问题" background="pink"/>
- </Labels>
- </View>"""
- # 示例任务数据
- SAMPLE_TASKS = [
- {
- "name": "文本分类任务-1",
- "data": {
- "text": "这家餐厅的服务态度非常好,菜品也很美味,环境优雅,强烈推荐!"
- }
- },
- {
- "name": "文本分类任务-2",
- "data": {
- "text": "产品质量太差了,用了不到一周就坏了,客服态度也很恶劣,非常失望。"
- }
- },
- {
- "name": "文本分类任务-3",
- "data": {
- "text": "这款手机性能一般,价格适中,适合日常使用。"
- }
- },
- {
- "name": "命名实体识别任务-1",
- "data": {
- "text": "2024年1月15日,张三在北京大学参加了人工智能研讨会。"
- }
- },
- {
- "name": "命名实体识别任务-2",
- "data": {
- "text": "李明是清华大学的教授,他在上海交通大学获得了博士学位。"
- }
- },
- {
- "name": "文本高亮任务-1",
- "data": {
- "text": "机器学习是人工智能的一个重要分支,它使计算机能够从数据中学习并做出决策。深度学习是机器学习的一个子领域,使用神经网络来模拟人脑的工作方式。"
- }
- }
- ]
- def create_project(name, description, config):
- """创建项目"""
- url = f"{BASE_URL}/api/projects"
- data = {
- "name": name,
- "description": description,
- "config": config
- }
-
- response = requests.post(url, json=data)
- if response.status_code == 201:
- project = response.json()
- print(f"✓ 创建项目成功: {project['name']} (ID: {project['id']})")
- return project
- else:
- print(f"✗ 创建项目失败: {response.status_code} - {response.text}")
- return None
- def create_task(project_id, task_name, task_data):
- """创建任务"""
- url = f"{BASE_URL}/api/tasks"
- data = {
- "project_id": project_id,
- "name": task_name,
- "data": task_data
- }
-
- response = requests.post(url, json=data)
- if response.status_code == 201:
- task = response.json()
- print(f" ✓ 创建任务: {task['name']} (ID: {task['id']})")
- return task
- else:
- print(f" ✗ 创建任务失败: {response.status_code} - {response.text}")
- return None
- def main():
- """主函数"""
- print("=" * 60)
- print("初始化标注平台示例数据")
- print("=" * 60)
- print()
-
- # 1. 创建文本分类项目
- print("1. 创建文本分类项目...")
- classification_project = create_project(
- name="情感分析标注项目",
- description="对用户评论进行情感分类(正面/负面/中性)",
- config=TEXT_CLASSIFICATION_CONFIG
- )
-
- if classification_project:
- print(" 创建文本分类任务...")
- for i in range(3):
- create_task(
- classification_project['id'],
- SAMPLE_TASKS[i]['name'],
- SAMPLE_TASKS[i]['data']
- )
- print()
-
- # 2. 创建命名实体识别项目
- print("2. 创建命名实体识别项目...")
- ner_project = create_project(
- name="命名实体识别项目",
- description="识别文本中的人名、地名、机构名和时间等实体",
- config=NER_CONFIG
- )
-
- if ner_project:
- print(" 创建命名实体识别任务...")
- for i in range(3, 5):
- create_task(
- ner_project['id'],
- SAMPLE_TASKS[i]['name'],
- SAMPLE_TASKS[i]['data']
- )
- print()
-
- # 3. 创建文本高亮项目
- print("3. 创建文本高亮标注项目...")
- highlight_project = create_project(
- name="文本高亮标注项目",
- description="标记文本中的重要信息、关键词和问题",
- config=TEXT_HIGHLIGHT_CONFIG
- )
-
- if highlight_project:
- print(" 创建文本高亮任务...")
- create_task(
- highlight_project['id'],
- SAMPLE_TASKS[5]['name'],
- SAMPLE_TASKS[5]['data']
- )
- print()
-
- print("=" * 60)
- print("示例数据初始化完成!")
- print("=" * 60)
- print()
- print("你现在可以:")
- print("1. 访问 http://localhost:4200/projects 查看项目列表")
- print("2. 点击项目查看详情和任务")
- print("3. 点击'开始标注'按钮进行标注")
- print()
- if __name__ == "__main__":
- try:
- main()
- except requests.exceptions.ConnectionError:
- print("✗ 错误: 无法连接到后端服务器")
- print(" 请确保后端服务器正在运行: python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000")
- except Exception as e:
- print(f"✗ 发生错误: {e}")
|