leonsimon23 commited on
Commit
e4819a3
·
verified ·
1 Parent(s): 16bb26c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -32
app.py CHANGED
@@ -4,39 +4,52 @@ import json
4
  import os
5
  import tempfile
6
  import textwrap
7
- from datetime import datetime
8
 
9
- # --- 默认模板和示例 ---
10
- # 为了方便用户快速上手,我们提供一个默认的提取模板。
11
- # 用户可以在界面上自由修改。
12
 
13
  # 1. 默认提取指令 (Prompt)
14
  DEFAULT_PROMPT = textwrap.dedent("""\
15
- 请按顺序提取文本中出现的角色、他们的情绪以及他们之间的关系。
16
- 提取时请使用原文中的确切文本,不要转述或重叠实体。
17
- 为每个实体提供有意义的属性以增加上下文信息。""")
 
18
 
19
  # 2. 默认提取示例 (Examples)
20
- # Gradio 界面中我们使用 JSON 格式让用户输入,后端再将其转换为 LangExtract 需要的格式。
21
  DEFAULT_EXAMPLES_DICT = [
22
  {
23
- "text": "罗密欧. 但是轻声!那边窗子里亮起来的是什么光?那就是东方,朱丽叶就是太阳。",
24
  "extractions": [
25
  {
26
- "extraction_class": "character",
27
- "extraction_text": "罗密欧",
28
- "attributes": {"emotional_state": "惊奇"}
29
  },
30
  {
31
- "extraction_class": "emotion",
32
- "extraction_text": "但是轻声!",
33
- "attributes": {"feeling": "温柔的敬畏"}
34
  },
35
  {
36
- "extraction_class": "relationship",
37
- "extraction_text": "朱丽叶就是太阳",
38
- "attributes": {"type": "隐喻", "subject": "朱丽叶", "object": "太阳"}
39
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  ]
41
  }
42
  ]
@@ -45,8 +58,7 @@ DEFAULT_EXAMPLES_DICT = [
45
  DEFAULT_EXAMPLES_JSON = json.dumps(DEFAULT_EXAMPLES_DICT, ensure_ascii=False, indent=2)
46
 
47
 
48
- # --- 后端处理函数 ---
49
- # 这个函数是整个应用的核心,它接收前端输入并调用 LangExtract。
50
 
51
  def extract_information(api_key, prompt, examples_json, input_text):
52
  """
@@ -86,14 +98,12 @@ def extract_information(api_key, prompt, examples_json, input_text):
86
  )
87
 
88
  # 将结果转换为可序列化的字典以便在 Gradio 中显示
89
- # 注意:result 是一个 AnnotatedDocument 对象
90
  output_for_display = {
91
  "source_text": result.source_text,
92
  "extractions": [ext.to_dict() for ext in result.extractions]
93
  }
94
 
95
  # 4. 创建可供下载的文件
96
- # 使用临时文件来保存结果
97
  with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.jsonl', encoding='utf-8') as tmp_file:
98
  lx.io.save_annotated_documents([result], file_path=tmp_file.name)
99
  download_path = tmp_file.name
@@ -105,9 +115,9 @@ def extract_information(api_key, prompt, examples_json, input_text):
105
  raise gr.Error(f"提取过程中发生错误: {e}")
106
 
107
 
108
- # --- Gradio UI 界面 ---
109
 
110
- with gr.Blocks(theme=gr.themes.Soft(), title="LangExtract Web UI") as demo:
111
  gr.Markdown("# LangExtract 交互式信息提取工具")
112
  gr.Markdown(
113
  "在左侧定义您的提取���则和输入文本,然后点击“开始提取”在右侧查看结果。\n"
@@ -131,25 +141,23 @@ with gr.Blocks(theme=gr.themes.Soft(), title="LangExtract Web UI") as demo:
131
  label="提取指令 (Prompt)",
132
  value=DEFAULT_PROMPT,
133
  lines=5,
134
- # info="告诉模型您想提取什么,以及遵循什么规则。" <- 已移除
135
  )
136
- gr.Markdown("告诉模型您想提取什么,以及遵循什么规则。") # 使用 Markdown 替代 info
137
 
138
  examples_input = gr.Code(
139
  label="提取示例 (JSON 格式)",
140
  value=DEFAULT_EXAMPLES_JSON,
141
  language="json",
142
- lines=15,
143
- # info="提供一两个高质量的示例,指导模型的输出格式。" <- 已移除
144
  )
145
- gr.Markdown("提供一两个高质量的示例,指导模型的输出格式。") # 使用 Markdown 替代 info
146
 
147
  gr.Markdown("## 3. 输入待提取的文本")
148
 
149
  text_input = gr.Textbox(
150
  label="源文本",
151
  lines=10,
152
- placeholder="在此处粘贴您要从中提取信息的文本..."
153
  )
154
 
155
  submit_btn = gr.Button("🚀 开始提取", variant="primary")
@@ -160,12 +168,10 @@ with gr.Blocks(theme=gr.themes.Soft(), title="LangExtract Web UI") as demo:
160
 
161
  json_output = gr.JSON(
162
  label="结构化输出 (JSON)",
163
- # info="这里显示从文本中提取出的结构化数据。" <- 已移除
164
  )
165
 
166
  file_output = gr.File(
167
  label="⬇️ 下载结果文件",
168
- # info="点击此处下载包含所有元数据的 .jsonl 文件。" <- 已移除
169
  )
170
 
171
  # --- 事件绑定 ---
 
4
  import os
5
  import tempfile
6
  import textwrap
 
7
 
8
+ # --- 默认模板和示例 (已更新为临床影像报告场景) ---
 
 
9
 
10
  # 1. 默认提取指令 (Prompt)
11
  DEFAULT_PROMPT = textwrap.dedent("""\
12
+ 请从影像检查报告中,按顺序提取关键的影像学发现、涉及的解剖部位、尺寸测量、影像学特征以及阴性发现。
13
+ - 提取时必须使用报告中的确切文本。
14
+ - 不要转述或概括。
15
+ - 为每个提取的实体提供详细的属性,以增加结构化信息。""")
16
 
17
  # 2. 默认提取示例 (Examples)
18
+ # 提供一个高质量的CT报告提取示例
19
  DEFAULT_EXAMPLES_DICT = [
20
  {
21
+ "text": "腹部CT平扫增强检查显示:肝脏右叶可见一大小约3.2 x 2.8 cm的低密度占位灶,边缘清晰,增强扫描后呈轻度环形强化。胰腺及双肾未见明确异常。",
22
  "extractions": [
23
  {
24
+ "extraction_class": "anatomy",
25
+ "extraction_text": "肝脏右叶",
26
+ "attributes": {"organ": "肝脏", "lobe": "右叶"}
27
  },
28
  {
29
+ "extraction_class": "size_measurement",
30
+ "extraction_text": "3.2 x 2.8 cm",
31
+ "attributes": {"value": "3.2 x 2.8", "unit": "cm"}
32
  },
33
  {
34
+ "extraction_class": "finding",
35
+ "extraction_text": "低密度占位灶",
36
+ "attributes": {"density": "低密度", "type": "占位灶"}
37
  },
38
+ {
39
+ "extraction_class": "radiologic_feature",
40
+ "extraction_text": "边缘清晰",
41
+ "attributes": {"feature_type": "边缘", "description": "清晰"}
42
+ },
43
+ {
44
+ "extraction_class": "radiologic_feature",
45
+ "extraction_text": "轻度环形强化",
46
+ "attributes": {"feature_type": "增强扫描", "degree": "轻度", "pattern": "环形强化"}
47
+ },
48
+ {
49
+ "extraction_class": "normal_finding",
50
+ "extraction_text": "胰腺及双肾未见明确异常",
51
+ "attributes": {"organs": ["胰腺", "双肾"]}
52
+ }
53
  ]
54
  }
55
  ]
 
58
  DEFAULT_EXAMPLES_JSON = json.dumps(DEFAULT_EXAMPLES_DICT, ensure_ascii=False, indent=2)
59
 
60
 
61
+ # --- 后端处理函数 (无需修改) ---
 
62
 
63
  def extract_information(api_key, prompt, examples_json, input_text):
64
  """
 
98
  )
99
 
100
  # 将结果转换为可序列化的字典以便在 Gradio 中显示
 
101
  output_for_display = {
102
  "source_text": result.source_text,
103
  "extractions": [ext.to_dict() for ext in result.extractions]
104
  }
105
 
106
  # 4. 创建可供下载的文件
 
107
  with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.jsonl', encoding='utf-8') as tmp_file:
108
  lx.io.save_annotated_documents([result], file_path=tmp_file.name)
109
  download_path = tmp_file.name
 
115
  raise gr.Error(f"提取过程中发生错误: {e}")
116
 
117
 
118
+ # --- Gradio UI 界面 (无需修改) ---
119
 
120
+ with gr.Blocks(theme=gr.themes.Soft(), title="LangExtract 交互式信息提取工具") as demo:
121
  gr.Markdown("# LangExtract 交互式信息提取工具")
122
  gr.Markdown(
123
  "在左侧定义您的提取���则和输入文本,然后点击“开始提取”在右侧查看结果。\n"
 
141
  label="提取指令 (Prompt)",
142
  value=DEFAULT_PROMPT,
143
  lines=5,
 
144
  )
145
+ gr.Markdown("告诉模型您想提取什么,以及遵循什么规则。")
146
 
147
  examples_input = gr.Code(
148
  label="提取示例 (JSON 格式)",
149
  value=DEFAULT_EXAMPLES_JSON,
150
  language="json",
151
+ lines=20, # 增加了行数以更好地显示复杂的JSON
 
152
  )
153
+ gr.Markdown("提供一两个高质量的示例,指导模型的输出格式。")
154
 
155
  gr.Markdown("## 3. 输入待提取的文本")
156
 
157
  text_input = gr.Textbox(
158
  label="源文本",
159
  lines=10,
160
+ placeholder="在此处粘贴您要从中提取信息的临床病历或影像报告..."
161
  )
162
 
163
  submit_btn = gr.Button("🚀 开始提取", variant="primary")
 
168
 
169
  json_output = gr.JSON(
170
  label="结构化输出 (JSON)",
 
171
  )
172
 
173
  file_output = gr.File(
174
  label="⬇️ 下载结果文件",
 
175
  )
176
 
177
  # --- 事件绑定 ---