-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllama2_inf.py
89 lines (71 loc) · 2.53 KB
/
llama2_inf.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import json
import torch
import logging
import transformers
from tqdm import tqdm
from langchain_huggingface import HuggingFacePipeline
from transformers import AutoTokenizer
from langchain.prompts import PromptTemplate
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
INPUT_DIR = "data"
RESULT_DIR = "results"
ERROR_DIR = "error"
os.makedirs(RESULT_DIR, exist_ok=True)
os.makedirs(ERROR_DIR, exist_ok=True)
MODEL_NAME = "meta-llama/Llama-2-7b-chat-hf"
logging.info(f"Loading model: {MODEL_NAME}")
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
pipeline = transformers.pipeline(
"text-generation",
model=MODEL_NAME,
tokenizer=tokenizer,
torch_dtype=torch.float16,
trust_remote_code=True,
device_map="auto",
max_length=5000,
do_sample=True,
truncation=True,
top_k=10,
top_p=0.9,
num_return_sequences=1,
repetition_penalty=1.1,
eos_token_id=tokenizer.eos_token_id,
)
llm = HuggingFacePipeline(pipeline=pipeline, model_kwargs={'temperature': 0.3})
TEMPLATE = """[INST]
Consider the overall structure and logic of the following two code snippets and determine if they perform a similar task.
Respond with 'yes' if they perform similar tasks or 'no' otherwise.
Code A:
{codeA}
Code B:
{codeB}
[/INST]"""
prompt = PromptTemplate(template=TEMPLATE, input_variables=["codeA", "codeB"])
llm_chain = prompt | llm
def get_valid_files(directory):
files = sorted(os.listdir(directory))
return [file for file in files if not file.startswith(('.', '~'))]
def process_file(file_path, output_path, error_path):
try:
with open(file_path, "r") as f:
data = json.load(f)
for element in tqdm(data, desc=f"Processing {os.path.basename(file_path)}"):
inputs = {"codeA": element['codeA'], "codeB": element['codeB']}
element["result"] = llm_chain.invoke(inputs)
with open(output_path, "w") as f:
json.dump(data, f, indent=4)
except Exception as e:
logging.error(f"Error processing {file_path}: {e}")
with open(error_path, "w") as f:
json.dump(data, f, indent=4)
def main():
files = get_valid_files(INPUT_DIR)
for file in files:
file_path = os.path.join(INPUT_DIR, file)
output_path = os.path.join(RESULT_DIR, f"llama2_{file}")
error_path = os.path.join(ERROR_DIR, f"llma_error_{file}")
logging.info(f"Processing file: {file}")
process_file(file_path, output_path, error_path)
if __name__ == "__main__":
main()