你好，这是我原来训练和验证模型的代码，请根据这些代码修改我的run.py，使模型输出的id和label能够被储存在一个csv文件中
训练代码
import pandas as pd
from sklearn import metrics
from tqdm import tqdm
import numpy as np
import os
os.environ['CUDA_VISIBLE_DEVICES']='6'


import torch
from torch.utils.data import Dataset, DataLoader
import torch.nn as nn
import transformers

from torch import cuda

device = 'cuda' if cuda.is_available() else 'cpu'


df_train = pd.read_csv("/data/fl/distillbert-qa/train.csv")[[
    "id", "Ofiicial Account Name","Title","News Url","Image Url","Report Content","label"

]]



def create_text(row):
    return (f"请判断以下新闻是真还是假，假新闻输出1，真新闻输出0.以下是一则新闻，新闻的账号发布信息为{row['Ofiicial Account Name']}，"
            f"标题为{row['Title']}，新闻链接为{row['News Url']}，"
            f"标题图片链接为{row['Image Url']}，举报内容为{row['Report Content']}.//")

# 应用该函数到 DataFrame 的每一行，创建新的 'text' 列
df_train['text'] = df_train.apply(create_text, axis=1)

# 创建一个新的 DataFrame，只包含 'text' 和 'label' 列
df_new = df_train[['text', 'label']]

# 如果需要，可以将新的 DataFrame 保存为 CSV 文件
# df_new.to_csv("/data/fl/distillbert-qa/transformed_train.csv", index=False)

# 显示前几行以确认结果

# print(df_new['text'].apply(len).max())
print(df_new.head())
MAX_LEN = 512
BATCH_SIZE = 32
LEARNING_RATE = 1e-05

DISTILL_BERT_PATH = '/data/fl/distillbert-qa/load'
MODEL_PATH = "pytorch_model"
tokenizer = transformers.DistilBertTokenizer.from_pretrained(
    DISTILL_BERT_PATH,
    do_lower_case=True
)


class tweet_Dataset(Dataset):

    def __init__(self, dataframe, tokenizer, max_len):
        self.data = dataframe
        self.tokenizer = tokenizer
        self.max_len = max_len

    def __getitem__(self, index):
        tweet = str(self.data.text[index])
        tweet = " ".join(tweet.split())
        inputs = self.tokenizer.encode_plus(tweet,
                                            None,
                                            add_special_tokens=True,
                                            max_length=self.max_len,
                                            pad_to_max_length=True,
                                            return_token_type_ids=True)
        ids = inputs['input_ids']
        mask = inputs['attention_mask']
        return {
            'ids': torch.tensor(ids, dtype=torch.long),
            'mask': torch.tensor(mask, dtype=torch.long),
            'labels': torch.tensor(self.data.label[index], dtype=torch.float)
        }

    def __len__(self):
        return len(self.data)

train_size = 0.85
train_dataset=df_new.sample(frac=train_size,random_state=200).reset_index(drop=True)
valid_dataset=df_new.drop(train_dataset.index).reset_index(drop=True)


print("FULL Dataset: {}".format(df_new.shape))
print("TRAIN Dataset: {}".format(train_dataset.shape))
print("VALID Dataset: {}".format(valid_dataset.shape))

training_set = tweet_Dataset(train_dataset, tokenizer, MAX_LEN)
testing_set = tweet_Dataset(valid_dataset, tokenizer, MAX_LEN)


training_set[0]

train_params = {'batch_size': BATCH_SIZE,
                'shuffle': True,
                'num_workers': 0
                }

valid_params = {'batch_size': BATCH_SIZE,
                'shuffle': True,
                'num_workers': 0
                }

train_dl = DataLoader(training_set, **train_params)
valid_dl = DataLoader(testing_set, **valid_params)

class DistillBERTClass(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.distill_bert = transformers.DistilBertModel.from_pretrained(DISTILL_BERT_PATH)
        self.drop = torch.nn.Dropout(0.3)
        self.out = torch.nn.Linear(768, 1)

    def forward(self, ids, mask):
        distilbert_output = self.distill_bert(ids, mask)
        hidden_state = distilbert_output[0]  # (bs, seq_len, dim)
        pooled_output = hidden_state[:, 0]  # (bs, dim)
        output_1 = self.drop(pooled_output)
        output = self.out(output_1)
        return output



model = DistillBERTClass()
model.to(device)

def loss_fn(outputs, labels):
    return nn.BCEWithLogitsLoss()(outputs, labels)


optimizer = torch.optim.Adam(params=model.parameters(), lr=LEARNING_RATE)


def eval_fn(data_loader, model):
    model.eval()
    fin_labels = []
    fin_outputs = []
    with torch.no_grad():
        for bi, d in tqdm(enumerate(data_loader), total=len(data_loader)):
            ids = d["ids"]
            mask = d["mask"]
            labels = d["labels"]

            ids = ids.to(device, dtype=torch.long)
            mask = mask.to(device, dtype=torch.long)
            labels = labels.to(device, dtype=torch.float)

            outputs = model(ids=ids, mask=mask)
            fin_labels.extend(labels.cpu().detach().numpy().tolist())
            fin_outputs.extend(
                torch.sigmoid(outputs).cpu().detach().numpy().tolist())
        fin_outputs = np.array(fin_outputs) >= 0.5
        f1_score = metrics.f1_score(fin_labels, fin_outputs)
    return f1_score

def fit(num_epochs, model, loss_fn, opt, train_dl, valid_dl):

    for epoch in range(num_epochs):
        model.train()
        for _,data in enumerate(train_dl, 0):
            ids = data['ids'].to(device, dtype = torch.long)
            mask = data['mask'].to(device, dtype = torch.long)
            labels = data['labels'].to(device, dtype = torch.float)
            outputs = model(ids, mask).squeeze()
            loss = loss_fn(outputs, labels)
            loss.backward()
            opt.step()
            opt.zero_grad()

        valid_acc = eval_fn(valid_dl, model)
        print('Epoch [{}/{}], Train Loss: {:.4f} and Validation acc {:.4f} and loss {:.4f}'.format(epoch+1, num_epochs, loss.item(),valid_acc, 1.1))

    torch.save(model.state_dict(), '/data/fl/distillbert-qa/finetuned/distillbert_finetuned.pth')
    print("模型已保存为 distillbert_finetuned.pth")

fit(7, model, loss_fn, optimizer, train_dl, valid_dl)


def sentence_prediction(sentence):
    model = DistillBERTClass()
    model.to(device)

    # 加载状态字典
    model.load_state_dict(
        torch.load(
            '/data/fl/distillbert-qa/finetuned/distillbert_finetuned.pth'))
    max_len = MAX_LEN
    tweet = str(sentence)
    tweet = " ".join(tweet.split())
    inputs = tokenizer.encode_plus(
        tweet,
        None,
        add_special_tokens=True,
        max_length=max_len,
        pad_to_max_length=True,
    )

    ids = inputs["input_ids"]
    mask = inputs["attention_mask"]

    ids = torch.tensor(ids, dtype=torch.long).unsqueeze(0)
    mask = torch.tensor(mask, dtype=torch.long).unsqueeze(0)

    ids = ids.to(device, dtype=torch.long)
    mask = mask.to(device, dtype=torch.long)

    outputs = model(ids=ids, mask=mask)

    outputs = torch.sigmoid(outputs).cpu().detach().numpy()
    return outputs[0][0] > 0.5

print(sentence_prediction(df_new['text'][0]))

run.py
import os
import sys
import cv2
import jieba
import requests
import numpy as np
import pandas as pd
# 以上为依赖包引入部分, 请根据实际情况引入
# 引入的包需要安装的, 请在requirements.txt里列明, 最好请列明版本
class DistillBERTClass(torch.nn.Module):

    def __init__(self):
        super().__init__()
        self.distill_bert = transformers.DistilBertModel.from_pretrained(
            DISTILL_BERT_PATH)
        self.drop = torch.nn.Dropout(0.3)
        self.out = torch.nn.Linear(768, 1)

    def forward(self, ids, mask):
        distilbert_output = self.distill_bert(ids, mask)
        hidden_state = distilbert_output[0]  # (bs, seq_len, dim)
        pooled_output = hidden_state[:, 0]  # (bs, dim)
        output_1 = self.drop(pooled_output)
        output = self.out(output_1)
        return output


class tweet_Dataset(Dataset):

    def __init__(self, dataframe, tokenizer, max_len):
        self.data = dataframe
        self.tokenizer = tokenizer
        self.max_len = max_len

    def __getitem__(self, index):
        tweet = str(self.data.text[index])
        tweet = " ".join(tweet.split())
        inputs = self.tokenizer.encode_plus(tweet,
                                            None,
                                            add_special_tokens=True,
                                            max_length=self.max_len,
                                            pad_to_max_length=True,
                                            return_token_type_ids=True)
        ids = inputs['input_ids']
        mask = inputs['attention_mask']
        return {
            'ids': torch.tensor(ids, dtype=torch.long),
            'mask': torch.tensor(mask, dtype=torch.long),
            'labels': torch.tensor(self.data.label[index], dtype=torch.float)
        }

    def __len__(self):
        return len(self.data)

# 以下为逻辑函数, main函数的入参和最终的结果输出不可修改
def main(to_pred_dir, result_save_path):
    run_py = os.path.abspath(__file__)
    model_dir = os.path.dirname(run_py)

    to_pred_dir = os.path.abspath(to_pred_dir)
    testa_csv_path = os.path.join(to_pred_dir, "testa_x", "testa_x.csv")
    testa_html_dir = os.path.join(to_pred_dir, "testa_x", "html")
    testa_image_dir = os.path.join(to_pred_dir, "testa_x", "image")

    #=-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-==
    # 以下区域为预测逻辑代码, 下面的仅为示例
    # 请选手根据实际模型预测情况修改

    testa = pd.read_csv(testa_csv_path)

    test = testa[["id", "label"]]

    #=-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-==
    model = DistillBERTClass()
    model.to(device)

    # 加载状态字典
    model.load_state_dict(torch.load('finetuned/distillbert_finetuned.pth'))

    # 结果输出到result_save_path
    test.to_csv(result_save_path, index=None)

if __name__ == "__main__":
    # 以下代码请勿修改, 若因此造成的提交失败由选手自负
    to_pred_dir = sys.argv[1]  # 所需预测的文件夹路径
    result_save_path = sys.argv[2]  # 预测结果保存文件路径，已指定格式为csv
    main(to_pred_dir, result_save_path)