Views
No views yet
1import torch, transformers
2
3def generate_response():
4 model = transformers.AutoModelForCausalLM.from_pretrained("benchang1110/Taiwan-tinyllama-v1.0-chat", torch_dtype=torch.bfloat16, device_map=device,attn_implementation="flash_attention_2")
5 tokenizer = transformers.AutoTokenizer.from_pretrained("benchang1110/Taiwan-tinyllama-v1.0-chat")
6 streamer = transformers.TextStreamer(tokenizer,skip_prompt=True)
7 while(1):
8 prompt = input('USER:')
9 if prompt == "exit":
10 break
11 print("Assistant: ")
12 message = [
13 {'content': prompt, 'role': 'user'},
14 ]
15 untokenized_chat = tokenizer.apply_chat_template(message,tokenize=False,add_generation_prompt=False)
16 inputs = tokenizer.encode_plus(untokenized_chat, add_special_tokens=True, return_tensors="pt",return_attention_mask=True).to(device)
17 outputs = model.generate(inputs["input_ids"],attention_mask=inputs['attention_mask'],streamer=streamer,use_cache=True,max_new_tokens=512,do_sample=True,temperature=0.1,repetition_penalty=1.2)
18
19
20if __name__ == '__main__':
21 device = 'cuda' if torch.cuda.is_available() else 'cpu'
22 generate_response()
23