File size: 1,724 Bytes
6041787
60894af
 
 
6041787
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
962fa43
6041787
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: gemma
library_name: transformers
pipeline_tag: text-generation
datasets:
- Norod78/hebrew_lyrics_prompting
- Norod78/hebrew_lyrics_prompting_finetune
language:
- he
base_model:
- google/gemma-2-2b-it
---

# 诪讞讜诇诇 砖讬专讬诐 诪讟讜驻砖讬诐 :)

```python
from transformers import AutoTokenizer, AutoModelForCausalLM
from transformers import TextStreamer
import torch


model_id = "Norod78/hebrew_lyrics-gemma2_2b"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    device_map="auto",
    torch_dtype=torch.bfloat16,
)

print(f"model.device = {model.device}")
input_text = "讻转讜讘 诇讬 砖讬专 注诇 转驻讜讞 讗讚诪讛 注诐 讞专讚讛 讞讘专转讬转"

input_template = tokenizer.apply_chat_template([{"role": "user", "content": input_text}], tokenize=False, add_generation_prompt=True)
input_ids = tokenizer(input_template, return_tensors="pt").to(model.device)
outputs = model.generate(**input_ids, max_new_tokens=256, repetition_penalty=1.05, temperature=0.5, no_repeat_ngram_size = 4, do_sample = True)
decoded_output = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
result = decoded_output.replace("user\n", "诪砖转诪砖:\n").replace("model\n", "\n诪讜讚诇:\n")
print("result = ", result)


chat = [
  {"role": "user", "content": input_text},
  {"role": "asistant"}
]
chat_with_template = tokenizer.apply_chat_template(chat, tokenize=False)
inputs = tokenizer(
[
    chat_with_template
], return_tensors = "pt").to(model.device)


text_streamer = TextStreamer(tokenizer)
_ = model.generate(**inputs, streamer = text_streamer, max_new_tokens=256 , repetition_penalty=1.1, temperature=0.6, top_p=0.4, top_k=40, do_sample = True)


```