Spaces:
Build error
Build error
Create app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from transformers import pipeline, AutoModel, AutoTokenizer
|
5 |
+
from monai.transforms import Compose, LoadImage, ScaleIntensity, EnsureChannelFirst
|
6 |
+
import SimpleITK as sitk
|
7 |
+
|
8 |
+
# 初始化组件
|
9 |
+
llm = pipeline("text-generation", model="microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract")
|
10 |
+
seg_model = AutoModel.from_pretrained("Project-MONAI/model-zoo/hnfnet_brats21").eval()
|
11 |
+
|
12 |
+
# 医学图像预处理
|
13 |
+
preprocess = Compose([
|
14 |
+
LoadImage(image_only=True),
|
15 |
+
EnsureChannelFirst(channel_dim='no_channel'),
|
16 |
+
ScaleIntensity(minv=0.0, maxv=1.0)
|
17 |
+
])
|
18 |
+
|
19 |
+
def analyze_image(image_path, clinical_note):
|
20 |
+
# 生成分割提示
|
21 |
+
prompt = f"根据临床报告生成分割提示:{clinical_note}"
|
22 |
+
guidance = llm(prompt, max_length=200)[0]['generated_text']
|
23 |
+
|
24 |
+
# 图像处理
|
25 |
+
img = preprocess(image_path)
|
26 |
+
|
27 |
+
# 分割推理
|
28 |
+
with torch.no_grad():
|
29 |
+
seg = seg_model(img.unsqueeze(0))[0]
|
30 |
+
|
31 |
+
# 后处理
|
32 |
+
result = sitk.GetArrayFromImage(seg.squeeze().numpy())
|
33 |
+
return (result > 0.5).astype(np.uint8), guidance
|
34 |
+
|
35 |
+
# 创建交互界面
|
36 |
+
demo = gr.Interface(
|
37 |
+
fn=analyze_image,
|
38 |
+
inputs=[
|
39 |
+
gr.File(label="上传DICOM/NIfTI文件"),
|
40 |
+
gr.Textbox(label="临床描述", placeholder="输入影像学检查报告...")
|
41 |
+
],
|
42 |
+
outputs=[
|
43 |
+
gr.Image(label="分割结果", colormap="viridis"),
|
44 |
+
gr.Textbox(label="生成的分割提示")
|
45 |
+
],
|
46 |
+
examples=[
|
47 |
+
["assets/sample1.nii.gz", "左侧基底节区可见直径2cm占位,T1低信号,T2高信号"],
|
48 |
+
["assets/sample2.dcm", "右肺上叶结节,边缘毛刺,考虑恶性肿瘤可能"]
|
49 |
+
]
|
50 |
+
)
|
51 |
+
|
52 |
+
demo.launch()
|