File size: 5,664 Bytes
b4592dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import asyncio
import concurrent.futures
from typing import List
import torch
from sklearn.metrics import f1_score
from transformers import AutoTokenizer
from evaluation.text_classifier_CT_pathology import CTPathologyClassifier
from collections import defaultdict


class CTPathologyClassifierF1Tool:
    def __init__(self):
        self.classifier = CTPathologyClassifier()
        self.classifier.eval()  # Set to eval mode for inference
        self.tokenizer = AutoTokenizer.from_pretrained(
            "zzxslp/RadBERT-RoBERTa-4m", do_lower_case=True
        )
        # Batching Config
        self.queue = asyncio.Queue()
        self.batch_size = 24
        self.batch_timeout = 2  # 1500ms window
        self._worker_task = None
        self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)

    async def ensure_worker_started(self):
        if self._worker_task is None or self._worker_task.done():
            self._worker_task = asyncio.create_task(self.worker())

    async def worker(self):
        loop = asyncio.get_running_loop()
        try:
            while True:
                # Wait for the first request
                first_item = await self.queue.get()
                batch = [first_item]

                # Start the timer for the batch
                start_time = loop.time()
                while len(batch) < self.batch_size:
                    time_left = self.batch_timeout - (loop.time() - start_time)
                    if time_left <= 0:
                        break
                    try:
                        item = await asyncio.wait_for(
                            self.queue.get(), timeout=time_left
                        )
                        batch.append(item)
                    except asyncio.TimeoutError:
                        break

                # Extract data for processing
                gt_reports = [b[0] for b in batch]
                cand_reports = [b[1] for b in batch]
                futures = [b[2] for b in batch]

                try:
                    # Run the heavy LLM scoring in the thread pool
                    results = await loop.run_in_executor(
                        self.executor, self.run_batch, gt_reports, cand_reports
                    )

                    for res, fut in zip(results, futures):
                        if not fut.done():
                            fut.set_result(res)
                except Exception as e:
                    for fut in futures:
                        if not fut.done():
                            fut.set_exception(e)
                finally:
                    for _ in range(len(batch)):
                        self.queue.task_done()

        except asyncio.CancelledError:
            self.executor.shutdown(wait=False)
            raise

    def run_batch(self, gt_reports: List[str], cand_reports: List[str]) -> List[float]:
        """Synchronous batch execution on the GPU."""
        output_encodings = defaultdict(list)
        ground_truth_encodings = defaultdict(list)
        batch_size = len(cand_reports)
        for output, ground_truth_report in zip(cand_reports, gt_reports):
            out_enc = self.tokenizer(
                output,
                return_tensors="pt",
                max_length=512,
                padding="max_length",
                truncation=True,
            )
            gt_enc = self.tokenizer(
                ground_truth_report,
                return_tensors="pt",
                max_length=512,
                padding="max_length",
                truncation=True,
            )

            for key in gt_enc:
                output_encodings[key].append(out_enc[key])
                ground_truth_encodings[key].append(gt_enc[key])

        for key in output_encodings:
            output_encodings[key] = torch.cat(output_encodings[key], dim=0)
            ground_truth_encodings[key] = torch.cat(ground_truth_encodings[key], dim=0)

        batch = {
            "predicted": output_encodings,
            "ground_truth": ground_truth_encodings,
        }
        with torch.no_grad():
            result = self.classifier.predict_binary(batch)
        result["ground_truth"] = result["ground_truth"].cpu().numpy()
        result["predicted"] = result["predicted"].cpu().numpy()
        f1_scores = []
        for i in range(batch_size):
            f1_scores.append(
                f1_score(
                    result["ground_truth"][i], result["predicted"][i], zero_division=1.0
                )
            )
        return f1_scores  # Convert to list for easier JSON serialization


# --- 2. MCP Server Setup ---

if __name__ == "__main__":
    from fastmcp import FastMCP
    from tool_configs import args_tools

    args = args_tools()
    mcp = FastMCP("f1_text_server", stateless_http=False)
    f1_tool_instance = CTPathologyClassifierF1Tool()

    @mcp.tool()
    async def f1_text_classifier_tool(
        ground_truth_report: str, candidate_report: str
    ) -> dict:
        """Calculate F1 score for radiology reports using batching."""
        await f1_tool_instance.ensure_worker_started()

        loop = asyncio.get_running_loop()
        future = loop.create_future()

        # Queue the work
        await f1_tool_instance.queue.put(
            (ground_truth_report, candidate_report, future)
        )

        try:
            score = await future
            return {
                "meta": None,
                "outputs": str(score),
            }
        except Exception as e:
            return {"meta": "Error", "outputs": str(e)}

    mcp.run(transport="http", host=args.host, port=args.port)