Views
No views yet
1!pip install -qU transformers accelerate
2from transformers import AutoTokenizer, AutoModelForCausalLM
3from accelerate import Accelerator
4import transformers
5import torch
6model = "h4rz3rk4s3/TinyPoliticaLlama-1.1B"
7messages = [
8 {
9 "role": "system",
10 "content": "You are an experienced journalist in the political domain and an expert of European politics.",
11 },
12 {"role": "user", "content": "Write a short article explaining how the French yellow vest protests started, how they developed over time and how the French Government reacted to the protests."},
13]
14
15tokenizer = AutoTokenizer.from_pretrained(model)
16prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
17model = AutoModelForCausalLM.from_pretrained(
18 model, trust_remote_code=True, device_map={"": Accelerator().process_index}
19)
20
21pipeline = transformers.pipeline(
22 "text-generation",
23 tokenizer=tokenizer,
24 model=model,
25 torch_dtype=torch.float16,
26 device_map={"": Accelerator().process_index},
27)
28outputs = pipeline(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
29print(outputs[0]["generated_text"])