Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,42 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# 加载模型
|
| 5 |
+
print("正在加载病理检测NER模型...")
|
| 6 |
+
ner = pipeline(
|
| 7 |
+
"token-classification",
|
| 8 |
+
model="OpenMed/OpenMed-NER-PathologyDetect-BigMed-560M",
|
| 9 |
+
aggregation_strategy="max"
|
| 10 |
+
)
|
| 11 |
+
print("模型加载完成!")
|
| 12 |
+
|
| 13 |
+
# 处理函数
|
| 14 |
+
def process_text(text):
|
| 15 |
+
if not text:
|
| 16 |
+
return "请输入医学文本"
|
| 17 |
+
|
| 18 |
+
results = ner(text)
|
| 19 |
+
output = ""
|
| 20 |
+
|
| 21 |
+
for result in results:
|
| 22 |
+
entity = result["entity_group"]
|
| 23 |
+
word = result["word"]
|
| 24 |
+
score = round(result["score"], 2)
|
| 25 |
+
output += f"检测到病理实体: {word} (类型: {entity}, 置信度: {score})\n"
|
| 26 |
+
|
| 27 |
+
if not output:
|
| 28 |
+
output = "未检测到任何病理相关实体"
|
| 29 |
+
|
| 30 |
+
return output
|
| 31 |
+
|
| 32 |
+
# 创建界面
|
| 33 |
+
demo = gr.Interface(
|
| 34 |
+
fn=process_text,
|
| 35 |
+
inputs=gr.Textbox(placeholder="请输入医学文本...", lines=5),
|
| 36 |
+
outputs="text",
|
| 37 |
+
title="OpenMed 病理检测 NER 模型演示",
|
| 38 |
+
description="使用OpenMed-NER-PathologyDetect-BigMed-560M模型识别文本中的病理实体"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# 启动服务
|
| 42 |
+
demo.launch()
|