Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,27 +56,28 @@ def process_input_and_visualize(input_text, input_file):
|
|
| 56 |
raise gr.Error("错误:服务器未配置 LANGEXTRACT_API_KEY。")
|
| 57 |
|
| 58 |
try:
|
| 59 |
-
# --- 错误修复与优化 ---
|
| 60 |
-
# 原代码: max_workers=10,这超出了免费API的每分钟2次的请求限制。
|
| 61 |
-
# 修复: 将 max_workers 降低到 2,以匹配免费套餐的配额。
|
| 62 |
-
# 优化: 切换到 gemini-1.5-flash-latest 模型,它速度更快,成本更低,通常有更宽松的免费额度。
|
| 63 |
result = lx.extract(
|
| 64 |
text_or_documents=source_text,
|
| 65 |
prompt_description=prompt,
|
| 66 |
examples=examples,
|
| 67 |
-
model_id="gemini-
|
| 68 |
api_key=api_key,
|
| 69 |
-
max_workers=2,
|
| 70 |
extraction_passes=2,
|
| 71 |
max_char_buffer=1500
|
| 72 |
)
|
| 73 |
except Exception as e:
|
| 74 |
raise gr.Error(f"信息提取过程中发生错误: {e}")
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# 1. 准备命名实体识别 (NER) 的高亮文本输出
|
| 77 |
highlighted_text = []
|
| 78 |
last_pos = 0
|
| 79 |
-
|
|
|
|
| 80 |
for entity in sorted_extractions:
|
| 81 |
start, end = entity.char_interval.start_pos, entity.char_interval.end_pos
|
| 82 |
if start >= last_pos and end <= len(source_text):
|
|
@@ -87,6 +88,7 @@ def process_input_and_visualize(input_text, input_file):
|
|
| 87 |
|
| 88 |
# 2. 准备关系提取 (RE) 的结构化 Markdown 输出
|
| 89 |
medication_groups = {}
|
|
|
|
| 90 |
for extraction in result.extractions:
|
| 91 |
group_name = extraction.attributes.get("medication_group", "未分组")
|
| 92 |
medication_groups.setdefault(group_name, []).append(extraction)
|
|
@@ -97,8 +99,12 @@ def process_input_and_visualize(input_text, input_file):
|
|
| 97 |
else:
|
| 98 |
for med_name, extractions in medication_groups.items():
|
| 99 |
structured_output += f"#### 药物组: {med_name}\n"
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
structured_output += f"- **{extraction.extraction_class}**: {extraction.extraction_text}{pos_info}\n"
|
| 103 |
structured_output += "\n"
|
| 104 |
|
|
@@ -119,7 +125,7 @@ def process_input_and_visualize(input_text, input_file):
|
|
| 119 |
|
| 120 |
# --- 3. 创建 Gradio 应用界面 (保持不变) ---
|
| 121 |
with gr.Blocks(theme=gr.themes.Soft(), title="药物信息提取器") as demo:
|
| 122 |
-
# ... (界面部分代码无需修改
|
| 123 |
gr.Markdown("# ⚕️ LangExtract 药物信息提取器")
|
| 124 |
gr.Markdown("一个基于大型语言模型的智能工具,可从**临床文本**或 **PDF 文件**中自动提取药物、剂量等信息,并进行结构化关联。")
|
| 125 |
|
|
@@ -158,7 +164,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="药物信息提取器") as demo:
|
|
| 158 |
submit_btn.click(
|
| 159 |
fn=process_input_and_visualize,
|
| 160 |
inputs=[input_textbox, input_file_uploader],
|
| 161 |
-
outputs=[output_highlight, output_structured, output_html_viewer, download_html,
|
| 162 |
).then(
|
| 163 |
lambda: (None, None),
|
| 164 |
inputs=None,
|
|
|
|
| 56 |
raise gr.Error("错误:服务器未配置 LANGEXTRACT_API_KEY。")
|
| 57 |
|
| 58 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
result = lx.extract(
|
| 60 |
text_or_documents=source_text,
|
| 61 |
prompt_description=prompt,
|
| 62 |
examples=examples,
|
| 63 |
+
model_id="gemini-1.5-flash-latest",
|
| 64 |
api_key=api_key,
|
| 65 |
+
max_workers=2,
|
| 66 |
extraction_passes=2,
|
| 67 |
max_char_buffer=1500
|
| 68 |
)
|
| 69 |
except Exception as e:
|
| 70 |
raise gr.Error(f"信息提取过程中发生错误: {e}")
|
| 71 |
|
| 72 |
+
# --- 关键修复:过滤掉无法定位的实体 ---
|
| 73 |
+
# 只有那些 char_interval 不为 None 的实体才是有位置信息的,才能被排序和高亮
|
| 74 |
+
grounded_extractions = [e for e in result.extractions if e.char_interval]
|
| 75 |
+
|
| 76 |
# 1. 准备命名实体识别 (NER) 的高亮文本输出
|
| 77 |
highlighted_text = []
|
| 78 |
last_pos = 0
|
| 79 |
+
# 现在我们对过滤后的、保证有位置信息的列表进行排序
|
| 80 |
+
sorted_extractions = sorted(grounded_extractions, key=lambda e: e.char_interval.start_pos)
|
| 81 |
for entity in sorted_extractions:
|
| 82 |
start, end = entity.char_interval.start_pos, entity.char_interval.end_pos
|
| 83 |
if start >= last_pos and end <= len(source_text):
|
|
|
|
| 88 |
|
| 89 |
# 2. 准备关系提取 (RE) 的结构化 Markdown 输出
|
| 90 |
medication_groups = {}
|
| 91 |
+
# 注意:这里我们仍然遍历所有实体(包括未定位的),因为它们可能仍有有用的属性信息
|
| 92 |
for extraction in result.extractions:
|
| 93 |
group_name = extraction.attributes.get("medication_group", "未分组")
|
| 94 |
medication_groups.setdefault(group_name, []).append(extraction)
|
|
|
|
| 99 |
else:
|
| 100 |
for med_name, extractions in medication_groups.items():
|
| 101 |
structured_output += f"#### 药物组: {med_name}\n"
|
| 102 |
+
# 我们在显示时检查 char_interval 是否存在
|
| 103 |
+
for extraction in sorted(extractions, key=lambda e: e.char_interval.start_pos if e.char_interval else -1):
|
| 104 |
+
pos_info = ""
|
| 105 |
+
if extraction.char_interval:
|
| 106 |
+
pos_info = f" (位置: {extraction.char_interval.start_pos}-{extraction.char_interval.end_pos})"
|
| 107 |
+
# 即使没有位置信息,我们仍然显示实体本身
|
| 108 |
structured_output += f"- **{extraction.extraction_class}**: {extraction.extraction_text}{pos_info}\n"
|
| 109 |
structured_output += "\n"
|
| 110 |
|
|
|
|
| 125 |
|
| 126 |
# --- 3. 创建 Gradio 应用界面 (保持不变) ---
|
| 127 |
with gr.Blocks(theme=gr.themes.Soft(), title="药物信息提取器") as demo:
|
| 128 |
+
# ... (界面部分代码无需修改) ...
|
| 129 |
gr.Markdown("# ⚕️ LangExtract 药物信息提取器")
|
| 130 |
gr.Markdown("一个基于大型语言模型的智能工具,可从**临床文本**或 **PDF 文件**中自动提取药物、剂量等信息,并进行结构化关联。")
|
| 131 |
|
|
|
|
| 164 |
submit_btn.click(
|
| 165 |
fn=process_input_and_visualize,
|
| 166 |
inputs=[input_textbox, input_file_uploader],
|
| 167 |
+
outputs=[output_highlight, output_structured, output_html_viewer, download_html, jsonl_path]
|
| 168 |
).then(
|
| 169 |
lambda: (None, None),
|
| 170 |
inputs=None,
|