Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
base_model:
|
| 5 |
+
- meta-llama/Llama-3.2-1B-Instruct
|
| 6 |
+
pipeline_tag: text-generation
|
| 7 |
+
---
|
| 8 |
+
```python
|
| 9 |
+
import onnxruntime_genai as og
|
| 10 |
+
|
| 11 |
+
model = og.Model('soap5_onnx')
|
| 12 |
+
tokenizer = og.Tokenizer(model)
|
| 13 |
+
tokenizer_stream = tokenizer.create_stream()
|
| 14 |
+
|
| 15 |
+
# Search options - exact match to original
|
| 16 |
+
search_options = {
|
| 17 |
+
'max_length': 4096,
|
| 18 |
+
'temperature': 0.1,
|
| 19 |
+
'top_p': 0.9,
|
| 20 |
+
'do_sample': True,
|
| 21 |
+
'batch_size': 1
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
soap_note_prompt = """You are an expert medical professor assisting in the creation of medically accurate SOAP summaries.
|
| 25 |
+
Please ensure the response follows the structured format: S:, O:, A:, P: without using markdown or special formatting.
|
| 26 |
+
Create a Medical SOAP note summary from the dialogue, following these guidelines:\n
|
| 27 |
+
S (Subjective): Summarize the patient's reported symptoms, including chief complaint and relevant history.
|
| 28 |
+
Rely on the patient's statements as the primary source and ensure standardized terminology.\n
|
| 29 |
+
O (Objective): Highlight critical findings such as vital signs, lab results, and imaging, emphasizing important details like the side of the body affected and specific dosages.
|
| 30 |
+
Include normal ranges where relevant.\n
|
| 31 |
+
A (Assessment): Offer a concise assessment combining subjective and objective data. State the primary diagnosis and any differential diagnoses, noting potential complications and the prognostic outlook.\n
|
| 32 |
+
P (Plan): Outline the management plan, covering medication, diet, consultations, and education. Ensure to mention necessary referrals to other specialties and address compliance challenges.\n
|
| 33 |
+
Considerations: Compile the report based solely on the transcript provided. Use concise medical jargon and abbreviations for effective doctor communication.\n
|
| 34 |
+
Please format the summary in a clean, simple list format without using markdown or bullet points. Use 'S:', 'O:', 'A:', 'P:' directly followed by the text. Avoid any styling or special characters.
|
| 35 |
+
TRANSCRIPT: \n"""
|
| 36 |
+
|
| 37 |
+
text = input("Input: ")
|
| 38 |
+
if not text:
|
| 39 |
+
print("Error, input cannot be empty")
|
| 40 |
+
exit()
|
| 41 |
+
|
| 42 |
+
# Method 1: Force generation by adding a SOAP starter after the prompt
|
| 43 |
+
full_prompt = soap_note_prompt + text
|
| 44 |
+
|
| 45 |
+
# Use the most complete Llama format
|
| 46 |
+
chat_template = "<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n{prompt}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\nS: "
|
| 47 |
+
|
| 48 |
+
prompt = chat_template.format(prompt=full_prompt)
|
| 49 |
+
|
| 50 |
+
input_tokens = tokenizer.encode(prompt)
|
| 51 |
+
print(f"Tokens in prompt: {len(input_tokens)}")
|
| 52 |
+
|
| 53 |
+
params = og.GeneratorParams(model)
|
| 54 |
+
params.set_search_options(**search_options)
|
| 55 |
+
generator = og.Generator(model, params)
|
| 56 |
+
generator.append_tokens(input_tokens)
|
| 57 |
+
|
| 58 |
+
print("\nGenerating SOAP note...")
|
| 59 |
+
print("S: ", end='', flush=True) # We already have "S: " in the prompt
|
| 60 |
+
|
| 61 |
+
# Generate the rest of the SOAP note
|
| 62 |
+
generated_text = ""
|
| 63 |
+
token_count = 0
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
while not generator.is_done() and token_count < 2000: # Limit to 2000 tokens for safety
|
| 67 |
+
generator.generate_next_token()
|
| 68 |
+
new_token = generator.get_next_tokens()[0]
|
| 69 |
+
decoded = tokenizer_stream.decode(new_token)
|
| 70 |
+
|
| 71 |
+
# Skip if we're still in the input echo phase
|
| 72 |
+
if token_count < 50 and (text[:20] in generated_text + decoded):
|
| 73 |
+
token_count += 1
|
| 74 |
+
continue
|
| 75 |
+
|
| 76 |
+
print(decoded, end='', flush=True)
|
| 77 |
+
generated_text += decoded
|
| 78 |
+
token_count += 1
|
| 79 |
+
|
| 80 |
+
# Stop if we see end markers
|
| 81 |
+
if any(marker in decoded for marker in ["<|eot_id|>", "<|end_of_text|>", "</s>"]):
|
| 82 |
+
break
|
| 83 |
+
|
| 84 |
+
except KeyboardInterrupt:
|
| 85 |
+
print("\nInterrupted")
|
| 86 |
+
|
| 87 |
+
print()
|
| 88 |
+
|
| 89 |
+
# If that didn't work, try Method 2: Different prompt structure
|
| 90 |
+
if len(generated_text.strip()) < 50 or text[:50] in generated_text:
|
| 91 |
+
print("\n\nMethod 1 didn't work well. Trying alternative method...")
|
| 92 |
+
|
| 93 |
+
del generator # Clean up
|
| 94 |
+
|
| 95 |
+
# Try a simpler approach - maybe the model expects a different format
|
| 96 |
+
simple_prompt = f"{soap_note_prompt}{text}\n\nSOAP Note:\nS: "
|
| 97 |
+
|
| 98 |
+
input_tokens = tokenizer.encode(simple_prompt)
|
| 99 |
+
|
| 100 |
+
params = og.GeneratorParams(model)
|
| 101 |
+
params.set_search_options(**search_options)
|
| 102 |
+
generator = og.Generator(model, params)
|
| 103 |
+
generator.append_tokens(input_tokens)
|
| 104 |
+
|
| 105 |
+
print("\nGenerating with simplified format...")
|
| 106 |
+
print("S: ", end='', flush=True)
|
| 107 |
+
|
| 108 |
+
generated_text = ""
|
| 109 |
+
token_count = 0
|
| 110 |
+
|
| 111 |
+
try:
|
| 112 |
+
while not generator.is_done() and token_count < 2000:
|
| 113 |
+
generator.generate_next_token()
|
| 114 |
+
new_token = generator.get_next_tokens()[0]
|
| 115 |
+
decoded = tokenizer_stream.decode(new_token)
|
| 116 |
+
|
| 117 |
+
print(decoded, end='', flush=True)
|
| 118 |
+
generated_text += decoded
|
| 119 |
+
token_count += 1
|
| 120 |
+
|
| 121 |
+
if any(marker in decoded for marker in ["<|eot_id|>", "<|end_of_text|>", "</s>"]):
|
| 122 |
+
break
|
| 123 |
+
|
| 124 |
+
except KeyboardInterrupt:
|
| 125 |
+
print("\nInterrupted")
|
| 126 |
+
|
| 127 |
+
print()
|
| 128 |
+
del generator
|
| 129 |
+
|
| 130 |
+
print("\n--- Generation Complete ---")
|
| 131 |
+
'''
|