Views
No views yet
meta-llama/Meta-Llama-3-8B-Instruct, specialized for analyzing patient narratives related to Abdominal Wall Hernia (AWH).1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3import json
4
5# Your model's unique ID on the Hugging Face Hub
6model_name = "Laxmikant17/Llama-3-8B-Hernia-Analyst-600-Patients"
7
8print(f"Loading fine-tuned model: {model_name}")
9
10# For running on a smaller GPU, it's recommended to load in 4-bit
11# from transformers import BitsAndBytesConfig
12# bnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16)
13# model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=bnb_config, device_map="auto")
14
15# For running on a larger GPU or CPU
16model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
17tokenizer = AutoTokenizer.from_pretrained(model_name)
18model.eval()
19
20print("✅ Model loaded successfully!")
21
22# Prepare your test narrative
23test_narrative = """
24The pain is the worst part. It's a constant, burning sensation that gets worse when I stand for more than ten minutes. I can't even lift my grocery bags without feeling a sharp pull. I also feel deformed. I avoid looking at myself without a shirt on. I just want to feel normal again.
25"""
26
27# Format the prompt using the exact Llama 3 Instruct template
28instruction = "Analyze the provided patient narrative about their experience with an Abdominal Wall Hernia (AWH) and generate a structured JSON output that summarizes your findings, adhering to the specified format and terminology."
29prompt = f"<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n{instruction}\n\n**Patient Narrative:**\n{test_narrative}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
30
31inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
32
33# Generate the analysis
34print("\n🚀 Generating analysis...")
35with torch.no_grad():
36 outputs = model.generate(
37 **inputs,
38 max_new_tokens=4096,
39 do_sample=False
40 )
41
42decoded_output = tokenizer.decode(outputs, skip_special_tokens=True)
43
44# Robustly extract and print the JSON from the model's response
45try:
46 assistant_response_start = decoded_output.find('assistant\n\n')
47 response_part = decoded_output[assistant_response_start + len('assistant\n\n'):].strip()
48 json_start = response_part.find('{')
49 json_end = response_part.rfind('}') + 1
50 json_string = response_part[json_start:json_end]
51
52 print("\n--- ✅ MODEL-GENERATED ANALYSIS ---")
53 parsed_json = json.loads(json_string)
54 print(json.dumps(parsed_json, indent=2))
55except Exception as e:
56 print(f"\n--- 🚨 ERROR: Could not parse the model's response. ---")
57 print(f"Error: {e}")
58 print("\nFull output for debugging:")
59 print(decoded_output)output JSON for each patient was generated by a powerful "teacher" model (gemini-1.5-pro-latest). This teacher model was guided by a highly detailed prompt that included the full QoL framework (domains, subthemes, and concepts) derived from the source research papers. This ensured the training data was high-quality, structured, and clinically relevant.transformers, peft, bitsandbytes, trlmeta-llama/Meta-Llama-3-8B-Instructlearning_rate: 2e-4lora_r (rank): 8lora_alpha: 16num_train_epochs: 1per_device_train_batch_size: 1gradient_accumulation_steps: 8 (Effective batch size: 8)optimizer: paged_adamw_8bitlr_scheduler_type: cosinepip.1# requirements.txt
2
3transformers==4.40.1
4datasets==2.18.0
5accelerate==0.29.3
6peft==0.10.0
7bitsandbytes==0.43.0
8trl==0.8.6
9torch1!pip uninstall -y sentence-transformers
2!pip install torch==2.3.1+cu121 torchvision==0.18.1+cu121 torchaudio==2.3.1 --index-url https://download.pytorch.org/whl/cu121
3!pip install -q "transformers==4.43.2" "datasets==2.18.0" "accelerate==0.29.3" "peft==0.10.0" "bitsandbytes==0.43.1" "trl==0.8.6" "protobuf==3.20.3"
4!pip install -q einops scipy sentencepiece tensorboard
5
6import os
7os.kill(os.getpid(), 9)