Update README.md
Browse files
README.md
CHANGED
|
@@ -18,12 +18,9 @@ extra_gated_fields:
|
|
| 18 |
|
| 19 |
This model ranks candidate disease-causing genes based on a patient’s phenotypic information. During inference, it does not generate a full free-text response. Instead, it performs gene-token subspace ranking on the logits at the final position and outputs a prioritized list of genes.
|
| 20 |
|
| 21 |
-
##
|
| 22 |
-
|
| 23 |
Use `https://phenogemini.org` to generate a prompt that includes similar patients.
|
| 24 |
|
| 25 |
-
## Model Loading
|
| 26 |
-
|
| 27 |
```python
|
| 28 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 29 |
import torch
|
|
@@ -31,46 +28,58 @@ import torch
|
|
| 31 |
model_name = "DISCOStudy/PhenoGemini"
|
| 32 |
|
| 33 |
# Load tokenizer
|
| 34 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name
|
| 35 |
|
| 36 |
# Load base model
|
| 37 |
model = AutoModelForCausalLM.from_pretrained(
|
| 38 |
model_name,
|
| 39 |
torch_dtype="auto",
|
| 40 |
device_map="auto",
|
| 41 |
-
trust_remote_code=True
|
| 42 |
).eval()
|
| 43 |
-
```
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
```python
|
| 50 |
with torch.no_grad():
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
}
|
| 59 |
-
]
|
| 60 |
-
text = tokenizer.apply_chat_template(
|
| 61 |
-
messages,
|
| 62 |
-
tokenize=False,
|
| 63 |
-
add_generation_prompt=True,
|
| 64 |
-
)
|
| 65 |
-
inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 66 |
-
outputs = model(inputs["input_ids"])
|
| 67 |
# Take logits for the last token only
|
| 68 |
-
logits = outputs.logits[0
|
| 69 |
# Mask non-gene tokens
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
scores = logits[token_ids].tolist()
|
| 76 |
```
|
|
|
|
| 18 |
|
| 19 |
This model ranks candidate disease-causing genes based on a patient’s phenotypic information. During inference, it does not generate a full free-text response. Instead, it performs gene-token subspace ranking on the logits at the final position and outputs a prioritized list of genes.
|
| 20 |
|
| 21 |
+
## Generate Prompt
|
|
|
|
| 22 |
Use `https://phenogemini.org` to generate a prompt that includes similar patients.
|
| 23 |
|
|
|
|
|
|
|
| 24 |
```python
|
| 25 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 26 |
import torch
|
|
|
|
| 28 |
model_name = "DISCOStudy/PhenoGemini"
|
| 29 |
|
| 30 |
# Load tokenizer
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 32 |
|
| 33 |
# Load base model
|
| 34 |
model = AutoModelForCausalLM.from_pretrained(
|
| 35 |
model_name,
|
| 36 |
torch_dtype="auto",
|
| 37 |
device_map="auto",
|
|
|
|
| 38 |
).eval()
|
|
|
|
| 39 |
|
| 40 |
+
WRAP_BREAK_1 = "\nWe can find some patients with similar phenotypes, such as:"
|
| 41 |
+
WRAP_BREAK_2 = "\nBased on all the information above, and my own general knowledge, this patient is likely to have a mutation in"
|
| 42 |
+
GENE_PREFIX = "<|PhenoGemini-Special-Token-Entrez-ID-"
|
| 43 |
|
| 44 |
+
def wrap_prompt(prompt: str, tokenizer) -> str:
|
| 45 |
+
try:
|
| 46 |
+
part1, remainder = prompt.split(WRAP_BREAK_1, 1)
|
| 47 |
+
between, part3 = remainder.split(WRAP_BREAK_2, 1)
|
| 48 |
+
part2 = WRAP_BREAK_1 + between
|
| 49 |
+
part3 = WRAP_BREAK_2 + part3
|
| 50 |
+
messages = [{"role": "user", "content": part1 + " We want to find the pathogenic gene mutation."}]
|
| 51 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 52 |
+
text = text + part2[1:] + "\n</think>" + part3 + " "
|
| 53 |
+
return text
|
| 54 |
+
except ValueError:
|
| 55 |
+
messages = [{"role": "user", "content": prompt}]
|
| 56 |
+
return tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 57 |
+
|
| 58 |
+
def detect_gene_token_space(tokenizer):
|
| 59 |
+
vocab = tokenizer.get_vocab()
|
| 60 |
+
gene_ids = sorted(idx for tok, idx in vocab.items() if tok.startswith(GENE_PREFIX))
|
| 61 |
+
if not gene_ids:
|
| 62 |
+
raise RuntimeError("No gene tokens found in tokenizer")
|
| 63 |
+
base_cutoff = gene_ids[0]
|
| 64 |
+
tensor_ids = torch.tensor(gene_ids, dtype=torch.long)
|
| 65 |
+
return base_cutoff, len(gene_ids), tensor_ids
|
| 66 |
+
|
| 67 |
+
_, _, gene_token_tensor = detect_gene_token_space(tokenizer)
|
| 68 |
|
|
|
|
| 69 |
with torch.no_grad():
|
| 70 |
+
text=wrap_prompt(
|
| 71 |
+
"[GET EXAMPLE PROMPT AT https://github.com/discostudy/phenogemini OR USE `https://phenogemini.org` TO GENERATE A PROMPT OF PATIENT'S PHENOTYPES.]",
|
| 72 |
+
tokenizer
|
| 73 |
+
)
|
| 74 |
+
inputs = tokenizer([text], return_tensors="pt")
|
| 75 |
+
inputs = {k: v.to(next(model.parameters()).device) for k, v in inputs.items()}
|
| 76 |
+
outputs = model(**inputs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# Take logits for the last token only
|
| 78 |
+
logits = outputs.logits[0, -1, :].to("cpu", dtype=torch.float32)
|
| 79 |
# Mask non-gene tokens
|
| 80 |
+
gene_token_tensor = gene_token_tensor.to(dtype=torch.long, device=logits.device)
|
| 81 |
+
gene_logits = logits[gene_token_tensor]
|
| 82 |
+
order = torch.argsort(gene_logits, descending=True)
|
| 83 |
+
sorted_gene_ids = gene_token_tensor[order]
|
| 84 |
+
sorted_gene_tok = tokenizer.convert_ids_to_tokens(sorted_gene_ids.tolist())
|
|
|
|
| 85 |
```
|