-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels_pipeline.py
More file actions
34 lines (32 loc) · 1.35 KB
/
Copy pathmodels_pipeline.py
File metadata and controls
34 lines (32 loc) · 1.35 KB
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
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
device = "cpu"
model_path = "ibm-granite/granite-3.0-1b-a400m-instruct"
tokenizer = AutoTokenizer.from_pretrained(model_path)
# drop device_map if running on CPU
with open("prompt.txt", "r", encoding="utf-8") as file:
prompt = file.read()
model = AutoModelForCausalLM.from_pretrained(model_path)
model.eval()
# change input text as desired
with open("responses_ibm.txt", "w", encoding="utf-8") as output_file:
with open('queries.txt', 'r') as file:
# Read each line in the queries file
for line in file:
# Make a request to the model
chat = [
{
"role": "user", "content": prompt + " " + line.strip()
},
]
chat = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
# tokenize the text
input_tokens = tokenizer(chat, return_tensors="pt").to(device)
# generate output tokens
output = model.generate(**input_tokens,
max_new_tokens=100)
# decode output tokens into text
output = tokenizer.batch_decode(output)
response = output[0].split(">")[-2].split("<")[0]
print(response)
output_file.write(response + "\n")