File size: 1,873 Bytes
7c93e9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation.utils import GenerationConfig
from peft import PeftModel, PeftConfig
import json
import csv

# lora_path = "/root/lanyun-tmp/output/MiniCPM/checkpoint-9000/"
model_path = '/root/lanyun-tmp/OpenBMB/MiniCPM-2B-sft-fp32'
model = AutoModelForCausalLM.from_pretrained(
    model_path, torch_dtype=torch.float16, device_map="auto", trust_remote_code=True
)
model.generation_config = GenerationConfig.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(
    model_path, use_fast=False, trust_remote_code=True,
)
# model = PeftModel.from_pretrained(model, lora_path)




# 读取JSONL文件
filename = '/root/lanyun-tmp/Dataset/val_triviaqa.csv'
data = []
with open(filename, newline='',encoding="utf-8") as csvfile:
    reader = csv.DictReader(csvfile)
  

    files = 'TriviaQA_MiniCPM_NLoRA.csv'
    with open(files, 'w', newline='',encoding='utf-8') as csvfile:
        writer = csv.writer(csvfile)
        # 提取内容
          # 逐行读取CSV文件
        for row in reader:
            context = row['context']
            question = row['question']

            
            messages = str([{'role': 'system', 'content': 'Don t  output "[" !!!, As a reading comprehension expert, you will receive context and question. Please understand the given Context first and then output the answer of the question based on the Context'}, {'role': 'user', 'content': '{\'context\': \'[DOC] [TLE] richard marx had an 80s No 1 hit with Hold On To The Nights? \', \'question\': \'Who had an 80s No 1 hit with Hold On To The Nights?\'}'}, {'role': 'assistant', 'content': "richard marx"}])
            response = model.chat(tokenizer, messages)
            
            answer = response[0][0]
            print(answer)
            writer.writerow(answer)