File size: 1,703 Bytes
d7a7846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import runpod
from main import initialize_agent

# 初始化agent(可以根据需要选择要使用的工具)
selected_tools = [
    "ImageVisualizerTool",
    "DicomProcessorTool",
    "ChestXRayClassifierTool",
    "ChestXRaySegmentationTool",
    "ChestXRayReportGeneratorTool",
    "XRayVQATool",
]

agent, tools_dict = initialize_agent(
    "medrax/docs/system_prompts.txt",
    tools_to_use=selected_tools,
    model_dir="/model-weights"
)

def handler(event):
    """
    处理RunPod API请求的主函数
    """
    try:
        # 获取请求参数
        job_input = event["input"]
        
        # 验证必需的参数
        if "image" not in job_input:
            return {"error": "Missing required parameter: image"}
        
        if "task" not in job_input:
            return {"error": "Missing required parameter: task"}
            
        image_data = job_input["image"]  # 这里假设是base64编码的图像
        task = job_input["task"]         # 任务类型
        
        # 根据任务类型调用相应的工具
        if task == "classification":
            result = tools_dict["ChestXRayClassifierTool"].run(image_data)
        elif task == "segmentation":
            result = tools_dict["ChestXRaySegmentationTool"].run(image_data)
        elif task == "report":
            result = tools_dict["ChestXRayReportGeneratorTool"].run(image_data)
        else:
            return {"error": f"Unsupported task type: {task}"}
            
        return {
            "status": "success",
            "result": result
        }
        
    except Exception as e:
        return {"error": str(e)}

runpod.serverless.start({"handler": handler})