| |
| from transformers import AutoModelForCausalLM, AutoTokenizer |
| from transformers.generation import GenerationConfig |
| import torch |
| import json |
| import os |
| from tqdm import tqdm |
| from functools import partial |
|
|
| tokenizer = AutoTokenizer.from_pretrained("internlm/internlm2-chat-7b", trust_remote_code=True) |
|
|
| model = AutoModelForCausalLM.from_pretrained( |
| "internlm/internlm2-chat-7b", |
| device_map="cuda:1", |
| trust_remote_code=True, torch_dtype=torch.float16 |
| ).eval() |
|
|
|
|
| def generate_output(file_path, max_new_tokens=128, prefix=""): |
| with open(file_path, 'r', encoding='utf-8') as file: |
| json_data = json.load(file) |
| output = {} |
| for index in tqdm(range(len(json_data)), desc=file_path): |
| item = json_data[index] |
| instruction = item.get("instruction") |
| question = item.get("question") |
| answer = item.get("answer") |
|
|
| chat_input = instruction + "\n" + question |
| chat_input = chat_input[:2000] |
| prediction, history = model.chat(tokenizer, chat_input, history=[], max_new_tokens=64, |
| do_sample=False, use_cache=True,) |
| |
| output[str(index)] = { |
| "origin_prompt": chat_input, |
| "prediction": prediction, |
| "refr": answer |
| } |
| return output |
|
|
|
|
| folder_path = "../national_test" |
| output_path = "./InternLM2-7B-Chat" |
| if not os.path.exists(output_path): |
| os.mkdir(output_path) |
| id_to_task = { |
| "4-1": generate_output, |
| "4-2": generate_output, |
| "4-3": generate_output, |
| "4-4": generate_output, |
| "4-5": generate_output, |
| } |
| for filename in os.listdir(folder_path): |
| if filename.endswith('.json'): |
| task_name = filename.split(".")[0] |
| if task_name not in id_to_task: |
| continue |
| file_path = os.path.join(folder_path, filename) |
| if os.path.exists(os.path.join(output_path, filename)): |
| continue |
| output = id_to_task[task_name](file_path) |
| if not os.path.exists(output_path): |
| os.mkdir(output_path) |
| with open(os.path.join(output_path, filename), "w", encoding='utf-8') as file: |
| json.dump(output, file, ensure_ascii=False) |
|
|
| |
|
|