ElenaSenger commited on
Commit
89aaca2
·
verified ·
1 Parent(s): 753188f

Updated example usage

Browse files
Files changed (1) hide show
  1. README.md +35 -10
README.md CHANGED
@@ -26,20 +26,45 @@ This is a fine-tuned version of [meta-llama/Meta-Llama-3-8B-Instruct](https://hu
26
  > The model was trained with *conversation-style* prompts, so the same format should be used for inference.
27
 
28
  ```python
29
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
 
 
 
30
 
31
- model_id = "your-username/DiSTER-Llama-3-8B-Instruct"
32
  tokenizer = AutoTokenizer.from_pretrained(model_id)
33
- model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
 
 
 
 
34
 
35
- pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
36
 
 
37
  prompt = (
38
- "Text: We used dropout regularization and AdamW optimizer to train a CNN on MRI images.\n\n"
39
- "I've read this text.\n"
40
- "What describes (technical or scientific) terms in the text, that are relevant to the domain medical-imaging?"
 
 
 
41
  )
42
 
43
- out = pipe(prompt, max_new_tokens=128, do_sample=False)
44
- print(out[0]['generated_text'])
45
- # ["dropout regularization", "AdamW optimizer", "CNN", "MRI images"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  > The model was trained with *conversation-style* prompts, so the same format should be used for inference.
27
 
28
  ```python
29
+ import torch
30
+ from transformers import AutoTokenizer, AutoModelForCausalLM
31
+
32
+ model_id = "ElenaSenger/DiSTER-Llama-3-8B-Instruct"
33
 
 
34
  tokenizer = AutoTokenizer.from_pretrained(model_id)
35
+ model = AutoModelForCausalLM.from_pretrained(
36
+ model_id,
37
+ device_map="auto",
38
+ torch_dtype="auto"
39
+ )
40
 
 
41
 
42
+ # Use the chat format as in SynTerm-fine-tuning
43
  prompt = (
44
+ '{"id": "test_0", "conversations": [\n'
45
+ ' {"from": "human", "value": "Text: We used dropout regularization and AdamW optimizer to train a CNN on MRI images."},\n'
46
+ ' {"from": "gpt", "value": "I\'ve read this text."},\n'
47
+ ' {"from": "human", "value": "What describes (technical or scientific) terms in the text, that are relevant to the domain medical-imaging?"},\n'
48
+ ' {"from": "gpt", "value": ""}\n'
49
+ ']}\n'
50
  )
51
 
52
+
53
+
54
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
55
+
56
+ with torch.no_grad():
57
+ outputs = model.generate(
58
+ **inputs,
59
+ max_new_tokens=128,
60
+ do_sample=True, # Enable sampling for temperature to take effect
61
+ temperature=0.1,
62
+ top_p=1.0
63
+ )
64
+ decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
65
+ print("=== Decoded output ===")
66
+ print(decoded)
67
+ # Print only the model's reply
68
+ reply = decoded[len(prompt):].strip()
69
+ print("\n=== Model reply only ===")
70
+ print(reply)