-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathinfer_text.py
More file actions
143 lines (121 loc) · 3.88 KB
/
Copy pathinfer_text.py
File metadata and controls
143 lines (121 loc) · 3.88 KB
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
"""
inference/infer_text.py
-----------------------
Text-only medical QA inference with HealthGPT-Pro.
Usage:
python inference/infer_text.py \
--model lintw/HealthGPT-Pro-4B \
--question "Explain the key symptoms and common risk factors of pneumonia."
# Or pipe a list of questions from a file:
python inference/infer_text.py \
--model lintw/HealthGPT-Pro-8B \
--question_file questions.txt
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
import torch
# Allow running from the repo root without installing the package
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from inference.utils import decode_output, load_model
def run_text_inference(
model,
processor,
question: str,
max_new_tokens: int = 512,
temperature: float = 0.7,
) -> str:
"""Run a single text-only medical QA query.
Args:
model: Loaded Qwen3VLForConditionalGeneration model.
processor: Corresponding AutoProcessor.
question: The medical question string.
max_new_tokens: Maximum tokens to generate.
temperature: Sampling temperature (0.0 = greedy decoding).
Returns:
Generated answer string.
"""
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": question},
],
}
]
inputs = processor.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt",
).to(model.device)
generate_kwargs: dict = {"max_new_tokens": max_new_tokens}
if temperature > 0.0:
generate_kwargs.update({"do_sample": True, "temperature": temperature})
else:
generate_kwargs["do_sample"] = False
with torch.inference_mode():
generated_ids = model.generate(**inputs, **generate_kwargs)
return decode_output(generated_ids, inputs.input_ids, processor)
def main() -> None:
parser = argparse.ArgumentParser(
description="HealthGPT-Pro — Text-Only Medical QA Inference"
)
parser.add_argument(
"--model",
type=str,
default="lintw/HealthGPT-Pro-4B",
help="HuggingFace model ID or local path "
"(e.g., 'lintw/HealthGPT-Pro-4B', 'lintw/HealthGPT-Pro-8B')",
)
parser.add_argument(
"--question",
type=str,
default=None,
help="A single medical question to ask the model.",
)
parser.add_argument(
"--question_file",
type=str,
default=None,
help="Path to a plain-text file with one question per line.",
)
parser.add_argument(
"--max_new_tokens",
type=int,
default=512,
help="Maximum number of new tokens to generate (default: 512).",
)
parser.add_argument(
"--temperature",
type=float,
default=0.7,
help="Sampling temperature. 0.0 = greedy (default).",
)
args = parser.parse_args()
if args.question is None and args.question_file is None:
parser.error("Provide at least one of --question or --question_file.")
# Collect questions
questions: list[str] = []
if args.question:
questions.append(args.question)
if args.question_file:
with open(args.question_file, "r", encoding="utf-8") as f:
questions.extend(line.strip() for line in f if line.strip())
# Load model
model, processor = load_model(model_id=args.model)
# Run inference
for i, q in enumerate(questions, 1):
print(f"\n{'='*60}")
print(f"[Question {i}] {q}")
print(f"{'='*60}")
answer = run_text_inference(
model, processor, q,
max_new_tokens=args.max_new_tokens,
temperature=args.temperature,
)
print(f"[Answer]\n{answer}")
if __name__ == "__main__":
main()