Spaces:
Runtime error
Runtime error
File size: 10,331 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 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 |
import re
import json
import os
import glob
import time
import logging
from datetime import datetime
import torch
from PIL import Image
from transformers import AutoModelForCausalLM, AutoTokenizer
from tqdm import tqdm
# Configure model settings
MODEL_NAME = "StanfordAIMI/CheXagent-2-3b"
DTYPE = torch.bfloat16
DEVICE = "cuda"
# Configure logging
log_filename = f"model_inference_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
logging.basicConfig(filename=log_filename, level=logging.INFO, format="%(message)s")
def initialize_model() -> tuple[AutoModelForCausalLM, AutoTokenizer]:
"""Initialize the CheXagent model and tokenizer.
Returns:
tuple containing:
- AutoModelForCausalLM: The initialized CheXagent model
- AutoTokenizer: The initialized tokenizer
"""
print("Loading model and tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME, device_map="auto", trust_remote_code=True
)
model = model.to(DTYPE)
model.eval()
return model, tokenizer
def create_inference_request(
question_data: dict,
case_details: dict,
case_id: str,
question_id: str,
model: AutoModelForCausalLM,
tokenizer: AutoTokenizer,
) -> str | None:
"""Create and execute an inference request for the CheXagent model.
Args:
question_data: Dictionary containing question details and metadata
case_details: Dictionary containing case information and image paths
case_id: Unique identifier for the medical case
question_id: Unique identifier for the question
model: The initialized CheXagent model
tokenizer: The initialized tokenizer
Returns:
str | None: Single letter answer (A-F) if successful, None if failed
"""
system_prompt = """You are a medical imaging expert. Your task is to provide ONLY a single letter answer.
Rules:
1. Respond with exactly one uppercase letter (A/B/C/D/E/F)
2. Do not add periods, explanations, or any other text
3. Do not use markdown or formatting
4. Do not restate the question
5. Do not explain your reasoning
Examples of valid responses:
A
B
C
Examples of invalid responses:
"A."
"Answer: B"
"C) This shows..."
"The answer is D"
"""
prompt = f"""Given the following medical case:
Please answer this multiple choice question:
{question_data['question']}
Base your answer only on the provided images and case information."""
# Parse required figures
try:
if isinstance(question_data["figures"], str):
try:
required_figures = json.loads(question_data["figures"])
except json.JSONDecodeError:
required_figures = [question_data["figures"]]
elif isinstance(question_data["figures"], list):
required_figures = question_data["figures"]
else:
required_figures = [str(question_data["figures"])]
except Exception as e:
print(f"Error parsing figures: {e}")
required_figures = []
required_figures = [
fig if fig.startswith("Figure ") else f"Figure {fig}" for fig in required_figures
]
# Get image paths
image_paths = []
for figure in required_figures:
base_figure_num = "".join(filter(str.isdigit, figure))
figure_letter = "".join(filter(str.isalpha, figure.split()[-1])) or None
matching_figures = [
case_figure
for case_figure in case_details.get("figures", [])
if case_figure["number"] == f"Figure {base_figure_num}"
]
for case_figure in matching_figures:
subfigures = []
if figure_letter:
subfigures = [
subfig
for subfig in case_figure.get("subfigures", [])
if subfig.get("number", "").lower().endswith(figure_letter.lower())
or subfig.get("label", "").lower() == figure_letter.lower()
]
else:
subfigures = case_figure.get("subfigures", [])
for subfig in subfigures:
if "local_path" in subfig:
image_paths.append("medrax/data/" + subfig["local_path"])
if not image_paths:
print(f"No local images found for case {case_id}, question {question_id}")
return None
try:
start_time = time.time()
# Prepare input for the model
query = tokenizer.from_list_format(
[*[{"image": path} for path in image_paths], {"text": prompt}]
)
conv = [{"from": "system", "value": system_prompt}, {"from": "human", "value": query}]
input_ids = tokenizer.apply_chat_template(
conv, add_generation_prompt=True, return_tensors="pt"
)
# Generate response
with torch.no_grad():
output = model.generate(
input_ids.to(DEVICE),
do_sample=False,
num_beams=1,
temperature=1.0,
top_p=1.0,
use_cache=True,
max_new_tokens=512,
)[0]
response = tokenizer.decode(output[input_ids.size(1) : -1])
duration = time.time() - start_time
# Clean response
clean_answer = validate_answer(response)
# Log response
log_entry = {
"case_id": case_id,
"question_id": question_id,
"timestamp": datetime.now().isoformat(),
"model": MODEL_NAME,
"duration": round(duration, 2),
"model_answer": clean_answer,
"correct_answer": question_data["answer"],
"input": {
"question_data": {
"question": question_data["question"],
"explanation": question_data["explanation"],
"metadata": question_data.get("metadata", {}),
"figures": question_data["figures"],
},
"image_paths": image_paths,
},
}
logging.info(json.dumps(log_entry))
return clean_answer
except Exception as e:
print(f"Error processing case {case_id}, question {question_id}: {str(e)}")
log_entry = {
"case_id": case_id,
"question_id": question_id,
"timestamp": datetime.now().isoformat(),
"model": MODEL_NAME,
"status": "error",
"error": str(e),
"input": {
"question_data": {
"question": question_data["question"],
"explanation": question_data["explanation"],
"metadata": question_data.get("metadata", {}),
"figures": question_data["figures"],
},
"image_paths": image_paths,
},
}
logging.info(json.dumps(log_entry))
return None
def validate_answer(response_text: str) -> str | None:
"""Enforce strict single-letter response format.
Args:
response_text: Raw response text from the model
Returns:
str | None: Single uppercase letter (A-F) if valid, None if invalid
"""
if not response_text:
return None
# Remove all whitespace and convert to uppercase
cleaned = response_text.strip().upper()
# Check if it's exactly one valid letter
if len(cleaned) == 1 and cleaned in "ABCDEF":
return cleaned
# If not, try to extract just the letter
match = re.search(r"([A-F])", cleaned)
return match.group(1) if match else None
def load_benchmark_questions(case_id: str) -> list[str]:
"""Find all question files for a given case ID.
Args:
case_id: Unique identifier for the medical case
Returns:
list[str]: List of paths to question JSON files
"""
benchmark_dir = "../benchmark/questions"
return glob.glob(f"{benchmark_dir}/{case_id}/{case_id}_*.json")
def count_total_questions() -> tuple[int, int]:
"""Count total number of cases and questions in benchmark.
Returns:
tuple containing:
- int: Total number of cases
- int: Total number of questions
"""
total_cases = len(glob.glob("../benchmark/questions/*"))
total_questions = sum(
len(glob.glob(f"../benchmark/questions/{case_id}/*.json"))
for case_id in os.listdir("../benchmark/questions")
)
return total_cases, total_questions
def main():
# Load the cases with local paths
with open("medrax/data/updated_cases.json", "r") as file:
data = json.load(file)
# Initialize model and tokenizer
model, tokenizer = initialize_model()
total_cases, total_questions = count_total_questions()
cases_processed = 0
questions_processed = 0
skipped_questions = 0
print(f"\nBeginning inference with {MODEL_NAME}")
print(f"Found {total_cases} cases with {total_questions} total questions")
# Process each case with progress bar
for case_id, case_details in tqdm(data.items(), desc="Processing cases"):
question_files = load_benchmark_questions(case_id)
if not question_files:
continue
cases_processed += 1
for question_file in tqdm(
question_files, desc=f"Processing questions for case {case_id}", leave=False
):
with open(question_file, "r") as file:
question_data = json.load(file)
question_id = os.path.basename(question_file).split(".")[0]
questions_processed += 1
answer = create_inference_request(
question_data, case_details, case_id, question_id, model, tokenizer
)
if answer is None:
skipped_questions += 1
continue
print(f"\nCase {case_id}, Question {question_id}")
print(f"Model Answer: {answer}")
print(f"Correct Answer: {question_data['answer']}")
print(f"\nInference Summary:")
print(f"Total Cases Processed: {cases_processed}")
print(f"Total Questions Processed: {questions_processed}")
print(f"Total Questions Skipped: {skipped_questions}")
if __name__ == "__main__":
main()
|