File size: 15,775 Bytes
d63774a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5551585
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd9252d
 
 
 
5551585
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd9252d
 
 
 
 
 
 
 
 
 
 
 
5551585
 
 
d63774a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5551585
d63774a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5551585
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d63774a
 
5551585
 
 
 
 
 
 
 
d63774a
5551585
 
 
d63774a
 
 
5551585
 
 
 
 
 
 
 
d63774a
5551585
 
d63774a
 
 
 
 
5551585
d63774a
 
 
 
5551585
d63774a
 
 
 
5551585
d63774a
 
 
 
 
 
 
 
5551585
d63774a
 
 
 
5551585
d63774a
 
 
 
5551585
d63774a
 
 
5551585
 
 
d63774a
 
 
 
 
 
 
 
 
 
5551585
 
 
 
 
 
 
 
 
 
d63774a
 
 
5551585
 
 
d63774a
5551585
 
d63774a
 
 
5551585
 
 
 
 
 
 
d63774a
 
 
 
 
 
 
 
 
 
 
 
 
5551585
 
 
 
 
 
d63774a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5551585
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import os
from dataclasses import dataclass

import torch

from src.utils.text_utils import postprocess_answer


def _as_bool(value: object, default: bool = False) -> bool:
    if value is None:
        return default
    if isinstance(value, bool):
        return value
    return str(value).strip().lower() in {"1", "true", "yes", "y", "on"}


@dataclass
class RewriteConfig:
    enabled: bool = False
    model_id: str = ""
    use_4bit: bool = True
    max_new_tokens: int = 28
    max_words: int = 10


_REWRITE_STYLE_BY_MODEL = {
    "A1": {
        "vi": "Diễn đạt đơn giản, trực tiếp, gần với đáp án gốc.",
        "en": "Use simple, direct wording close to the raw answer.",
    },
    "A2": {
        "vi": "Diễn đạt như một quan sát ngắn trên hình ảnh.",
        "en": "Word it as a short imaging observation.",
    },
    "B1": {
        "vi": "Diễn đạt tự nhiên, mềm hơn, dễ đọc.",
        "en": "Use natural, softer, easy-to-read wording.",
    },
    "B2": {
        "vi": "Diễn đạt hay hơn A1/A2, theo phong cách lâm sàng súc tích.",
        "en": "Use stronger concise clinical wording than A1/A2.",
    },
    "DPO": {
        "vi": "Diễn đạt hay nhất theo hướng thận trọng, chuyên nghiệp.",
        "en": "Use the most careful, professional wording.",
    },
    "PPO": {
        "vi": "Diễn đạt hay nhất theo hướng rõ ràng, mạch lạc.",
        "en": "Use the clearest, most polished wording.",
    },
    "SOUP": {
        "vi": "Diễn đạt cân bằng giữa lâm sàng, thận trọng và rõ ràng.",
        "en": "Use balanced clinical, careful, and clear wording.",
    },
}


_MODEL_SPECIFIC_EXAMPLES = {
    "A1": {
        "vi": {
            "question": "Ảnh có khối u không?",
            "answer": "có",
            "rewrite": "Có, có khối u.",
        },
        "en": {
            "question": "Is there a mass?",
            "answer": "yes",
            "rewrite": "Yes, there is a mass.",
        },
    },
    "A2": {
        "vi": {
            "question": "Ảnh có khối u không?",
            "answer": "có",
            "rewrite": "Có, thấy khối u trên ảnh.",
        },
        "en": {
            "question": "Is there a mass?",
            "answer": "yes",
            "rewrite": "Yes, a mass is seen.",
        },
    },
    "B2": {
        "vi": {
            "question": "Ảnh có khối u không?",
            "answer": "có",
            "rewrite": "Có, hình ảnh gợi ý khối u.",
        },
        "en": {
            "question": "Is there a mass?",
            "answer": "yes",
            "rewrite": "Yes, imaging suggests a mass.",
        },
    },
    "DPO": {
        "vi": {
            "question": "Ảnh có khối u không?",
            "answer": "có",
            "rewrite": "Có, có dấu hiệu gợi ý khối u.",
        },
        "en": {
            "question": "Is there a mass?",
            "answer": "yes",
            "rewrite": "Yes, findings suggest a mass.",
        },
    },
    "PPO": {
        "vi": {
            "question": "Ảnh có khối u không?",
            "answer": "có",
            "rewrite": "Có, kết quả gợi ý khối u rõ.",
        },
        "en": {
            "question": "Is there a mass?",
            "answer": "yes",
            "rewrite": "Yes, results clearly suggest a mass.",
        },
    },
    "SOUP": {
        "vi": {
            "question": "Ảnh có khối u không?",
            "answer": "có",
            "rewrite": "Có, hình ảnh gợi ý khối u rõ.",
        },
        "en": {
            "question": "Is there a mass?",
            "answer": "yes",
            "rewrite": "Yes, imaging clearly suggests a mass.",
        },
    },
}


class MedicalAnswerRewriter:
    """
    Rewrite lớp cuối cho VQA output.

    Mục tiêu:
    - Giữ nguyên ý nghĩa gốc.
    - Làm câu trả lời tự nhiên và đầy đủ hơn một chút.
    - Vẫn giới hạn tối đa số từ theo cấu hình.

    Mô hình này không thay thế VQA model chính.
    """

    def __init__(self, config: RewriteConfig | None = None) -> None:
        self.config = config or self._load_config()
        self._load_attempted = False
        self._ready = False
        self._tokenizer = None
        self._model = None
        self._device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    @staticmethod
    def _load_config() -> RewriteConfig:
        model_id = (
            os.getenv("ANSWER_REWRITE_MODEL_ID", "").strip()
            or os.getenv("QWEN_REWRITE_MODEL_ID", "").strip()
            or "Qwen/Qwen2.5-14B-Instruct"
        )
        enabled = _as_bool(os.getenv("ANSWER_REWRITE_ENABLED"), default=True)
        use_4bit = _as_bool(os.getenv("ANSWER_REWRITE_USE_4BIT"), default=True)
        max_new_tokens = int(os.getenv("ANSWER_REWRITE_MAX_NEW_TOKENS", "28"))
        max_words = int(os.getenv("ANSWER_REWRITE_MAX_WORDS", "10"))
        return RewriteConfig(
            enabled=enabled,
            model_id=model_id,
            use_4bit=use_4bit,
            max_new_tokens=max_new_tokens,
            max_words=max_words,
        )

    @property
    def enabled(self) -> bool:
        return bool(self.config.enabled and self.config.model_id)

    @property
    def model_id(self) -> str:
        return self.config.model_id

    @property
    def ready(self) -> bool:
        return self._ready

    def _lazy_load(self) -> None:
        if self._load_attempted:
            return
        self._load_attempted = True

        if not self.enabled:
            return

        try:
            from transformers import AutoModelForCausalLM, AutoTokenizer
            hf_token = (
                os.getenv("ANSWER_REWRITE_HF_TOKEN", "").strip()
                or os.getenv("HF_TOKEN", "").strip()
                or os.getenv("HUGGINGFACE_HUB_TOKEN", "").strip()
                or None
            )

            tokenizer = AutoTokenizer.from_pretrained(self.config.model_id, trust_remote_code=True, token=hf_token)
            model_kwargs = {
                "trust_remote_code": True,
                "low_cpu_mem_usage": True,
            }

            if self._device.type == "cuda":
                if self.config.use_4bit:
                    try:
                        from transformers import BitsAndBytesConfig

                        model_kwargs["quantization_config"] = BitsAndBytesConfig(
                            load_in_4bit=True,
                            bnb_4bit_use_double_quant=True,
                            bnb_4bit_quant_type="nf4",
                            bnb_4bit_compute_dtype=torch.bfloat16,
                        )
                    except Exception as exc:
                        print(f"[WARNING] Rewrite 4-bit config unavailable, falling back to bf16: {exc}")
                        model_kwargs["torch_dtype"] = torch.bfloat16
                else:
                    model_kwargs["torch_dtype"] = torch.bfloat16
                model_kwargs["device_map"] = "auto"
            else:
                model_kwargs["torch_dtype"] = torch.float32

            if hf_token is not None:
                model_kwargs["token"] = hf_token

            model = AutoModelForCausalLM.from_pretrained(self.config.model_id, **model_kwargs)
            model.eval()

            self._tokenizer = tokenizer
            self._model = model
            self._ready = True
            print(f"[INFO] ✅ Answer rewriter ready: {self.config.model_id}")
        except Exception as exc:
            self._ready = False
            print(f"[WARNING] ❌ Answer rewriter load failed: {exc}")

    def _get_style_instruction(self, source_model: str | None, language: str) -> str:
        if not source_model:
            return ""
        style = _REWRITE_STYLE_BY_MODEL.get(source_model.upper())
        if not style:
            return ""
        lang_key = "en" if language.lower().startswith("en") else "vi"
        return style[lang_key]

    def _get_model_specific_example(self, source_model: str | None, language: str) -> dict[str, str] | None:
        if not source_model:
            return None
        examples = _MODEL_SPECIFIC_EXAMPLES.get(source_model.upper())
        if not examples:
            return None
        lang_key = "en" if language.lower().startswith("en") else "vi"
        return examples[lang_key]

    def _build_messages(
        self,
        question: str,
        answer: str,
        language: str = "vi",
        source_model: str | None = None,
    ) -> list[dict[str, str]]:
        style_instruction = self._get_style_instruction(source_model, language)
        model_example = self._get_model_specific_example(source_model, language)
        system_prompt = (
            "Bạn là bộ biên tập câu trả lời cho hệ thống Medical VQA. "
            "Nhiệm vụ của bạn là mở rộng đáp án gốc thành một câu trả lời đầy đủ, "
            "tự nhiên và rõ nghĩa hơn, nhưng vẫn phải bám sát đáp án gốc. "
            "KHÔNG thêm thông tin y khoa mới, KHÔNG suy diễn ngoài đáp án gốc. "
            "Có thể dùng câu hỏi để xác định đối tượng y khoa đang được hỏi, "
            "nhưng đáp án gốc quyết định ý nghĩa đúng/sai/có/không. "
            "Nếu nhiều model có cùng đáp án gốc, vẫn dùng phong cách riêng của model hiện tại. "
            "CÂU TRẢ LỜI BẮT BUỘC PHẢI DƯỚI 10 TỪ, ÍT NHẤT 3 TỪ. "
            "Chỉ trả về câu trả lời cuối cùng."
        )
        if style_instruction:
            system_prompt += f" Phong cách riêng cho model này: {style_instruction}"

        if language.lower().startswith("en"):
            system_prompt = (
                "You are an editor for a Medical VQA system. "
                "Expand the raw answer into a fuller, natural, clearer answer "
                "while staying strictly based on the raw answer. "
                "Do not add new medical facts or infer beyond the raw answer. "
                "You may use the question to identify the medical target, "
                "but the raw answer controls yes/no/presence/absence. "
                "If several models share the same raw answer, still use this model's wording style. "
                "THE ANSWER MUST BE UNDER 10 WORDS and at least 3 words. "
                "Return only the final answer."
            )
            if style_instruction:
                system_prompt += f" Model-specific wording style: {style_instruction}"

        examples = [
            {
                "question": "Ảnh này có tràn dịch màng phổi không?",
                "answer": "không",
                "rewrite": "Không, không thấy tràn dịch màng phổi.",
            },
            {
                "question": "Hình ảnh có tim to không?",
                "answer": "có",
                "rewrite": "Có, hình ảnh cho thấy tim to.",
            },
            {
                "question": "Đây là loại ảnh gì?",
                "answer": "x quang ngực",
                "rewrite": "Đây là ảnh X-quang ngực.",
            },
        ]

        if language.lower().startswith("en"):
            examples = [
                {
                    "question": "Is there pleural effusion?",
                    "answer": "no",
                    "rewrite": "No, pleural effusion is not seen.",
                },
                {
                    "question": "Is the heart enlarged?",
                    "answer": "yes",
                    "rewrite": "Yes, the heart appears enlarged.",
                },
                {
                    "question": "What modality is this?",
                    "answer": "chest x ray",
                    "rewrite": "This is a chest X-ray.",
                },
            ]

        if model_example:
            examples.append(model_example)

        messages: list[dict[str, str]] = [{"role": "system", "content": system_prompt}]
        for ex in examples:
            messages.append(
                {
                    "role": "user",
                    "content": f"Câu hỏi: {ex['question']}\nĐáp án gốc: {ex['answer']}",
                }
            )
            messages.append({"role": "assistant", "content": ex["rewrite"]})

        user_prompt = (
            f"Câu hỏi: {question}\n"
            f"Đáp án gốc: {answer}\n"
            f"Model nguồn: {source_model or 'unknown'}\n"
            "Viết lại thành câu đầy đủ hơn, tự nhiên hơn, dưới 10 từ. "
            "CHỈ DÙNG THÔNG TIN TỪ ĐÁP ÁN GỐC."
        )
        if style_instruction:
            user_prompt += f"\nPhong cách diễn đạt: {style_instruction}"

        if language.lower().startswith("en"):
            user_prompt = (
                f"Question: {question}\nRaw answer: {answer}\n"
                f"Source model: {source_model or 'unknown'}\n"
                "Rewrite it as a fuller, natural answer under 10 words. "
                "Use only information from the raw answer."
            )
            if style_instruction:
                user_prompt += f"\nWording style: {style_instruction}"
        messages.append({"role": "user", "content": user_prompt})
        return messages

    def rewrite(
        self,
        question: str,
        answer: str,
        language: str = "vi",
        source_model: str | None = None,
    ) -> str:
        """
        Rewrite câu trả lời để tự nhiên hơn.
        Nếu rewrite model không sẵn sàng, trả về output đã postprocess.
        """
        if not answer:
            return ""

        self._lazy_load()
        fallback = postprocess_answer(answer, max_words=self.config.max_words)
        if not self.enabled or not self._ready:
            return fallback

        try:
            messages = self._build_messages(
                question=question,
                answer=answer,
                language=language,
                source_model=source_model,
            )
            prompt = self._tokenizer.apply_chat_template(
                messages,
                tokenize=False,
                add_generation_prompt=True,
            )
            inputs = self._tokenizer(prompt, return_tensors="pt", truncation=True)
            inputs = {k: v.to(self._device) for k, v in inputs.items()}

            with torch.inference_mode():
                output_ids = self._model.generate(
                    **inputs,
                    max_new_tokens=self.config.max_new_tokens,
                    do_sample=False,
                    temperature=0.1,
                    repetition_penalty=1.05,
                    pad_token_id=self._tokenizer.eos_token_id,
                )

            prompt_len = inputs["input_ids"].shape[1]
            generated = self._tokenizer.decode(output_ids[0][prompt_len:], skip_special_tokens=True).strip()
            cleaned = postprocess_answer(generated, max_words=self.config.max_words)
            return cleaned or fallback
        except Exception as exc:
            print(f"[WARNING] Rewrite failed: {exc}")
            return fallback


def rewrite_final_answer(
    question: str,
    answer: str,
    language: str = "vi",
    source_model: str | None = None,
) -> str:
    """
    Helper tiện dùng trong notebook / web.
    """
    rewriter = MedicalAnswerRewriter()
    return rewriter.rewrite(
        question=question,
        answer=answer,
        language=language,
        source_model=source_model,
    )