File size: 1,298 Bytes
9c5279d
 
f242654
 
 
 
225e7ea
 
f242654
9c5279d
 
9af8e61
 
f242654
225e7ea
9af8e61
225e7ea
f242654
 
9c5279d
20cdf25
f242654
 
 
225e7ea
 
9c5279d
f242654
9c5279d
f242654
9c5279d
f242654
 
 
 
 
9c5279d
f242654
9c5279d
f242654
 
 
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
---
library_name: transformers
tags:
- art
datasets:
- gokaygokay/prompt_description_stable_diffusion_3k
language:
- en
pipeline_tag: text2text-generation
---

```
pip install -q -U transformers trl accelerate peft bitsandbytes
```

```
from transformers import AutoModelForCausalLM, GenerationConfig, AutoTokenizer
import torch
import os

model_id = "gokaygokay/tiny_llama_chat_description_to_prompt"
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, load_in_8bit=False,
                                             device_map="auto",
                                             trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token = tokenizer.eos_token

def generate_response(user_input):

  prompt = f"<|im_start|>user\n{user_input}<|im_end|>\n<|im_start|>assistant:"

  inputs = tokenizer([prompt], return_tensors="pt")
  generation_config = GenerationConfig(penalty_alpha=0.6,do_sample = True,
      top_k=5,temperature=0.9,repetition_penalty=1.2,
      max_new_tokens=100,pad_token_id=tokenizer.eos_token_id
  )

  inputs = tokenizer(prompt, return_tensors="pt").to('cuda')

  outputs = model.generate(**inputs, generation_config=generation_config)
  print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```