Spaces:
Sleeping
Sleeping
File size: 2,093 Bytes
9b37099 3db7aae 9b37099 3db7aae 9b37099 3db7aae 9b37099 |
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# app.py
import gradio as gr
from PIL import Image
from transformers import CLIPProcessor, CLIPModel
import time
# 模型加载(约需2分钟)
def load_model():
start = time.time()
model = CLIPModel.from_pretrained("vinid/plip")
processor = CLIPProcessor.from_pretrained("vinid/plip")
print(f"模型加载完成,耗时:{time.time()-start:.1f}秒")
return model, processor
model, processor = load_model()
# 预测函数
def classify_image(image, text_options):
try:
# 图像预处理
processed_img = image.convert("RGB").resize((224,224))
# 文本处理(自动分割逗号分隔的标签)
labels = [t.strip() for t in text_options.split(',')]
# 模型推理
inputs = processor(
text=labels,
images=processed_img,
return_tensors="pt",
padding=True
)
outputs = model(**inputs)
probs = outputs.logits_per_image.softmax(dim=1).tolist()[0]
# 结果格式化
return {label: round(prob,3) for label, prob in zip(labels, probs)}
except Exception as e:
return f"处理错误:{str(e)}"
# 界面布局
with gr.Blocks(theme=gr.themes.Soft()) as app:
gr.Markdown("# 🩺 医学影像智能诊断系统")
with gr.Row():
image_input = gr.Image(type="pil", label="上传病理切片")
text_input = gr.Textbox(
label="诊断标签(逗号分隔)",
value="恶性肿瘤, 良性病变, 炎症反应"
)
submit_btn = gr.Button("开始分析", variant="primary")
output = gr.Label(label="诊断概率分布", num_top_classes=3)
# 示例数据
gr.Examples(
examples=[
["sample1.jpg", "肺癌, 肺结核, 正常组织"],
["sample2.png", "胃癌, 胃炎, 胃溃疡"]
],
inputs=[image_input, text_input]
)
submit_btn.click(
fn=classify_image,
inputs=[image_input, text_input],
outputs=output
)
app.launch(debug=True)
|