🚀 ComFormer代码注释生成模型
ComFormer是一个基于Transformer架构的模型,用于代码注释的自动生成。它利用混合代码表示方法,能有效处理代码并生成准确的注释,可应用于代码理解和维护场景。
🚀 快速开始
以下是使用ComFormer模型进行代码注释生成的步骤:
- 安装所需的库,如
transformers
。
- 加载预训练的模型和分词器。
- 准备代码数据并进行必要的转换。
- 对输入进行编码并生成注释。
- 解码生成的注释并输出。
💻 使用示例
基础用法
from transformers import BartForConditionalGeneration, BartTokenizer
model = BartForConditionalGeneration.from_pretrained("NTUYG/ComFormer")
tokenizer = BartTokenizer.from_pretrained("NTUYG/ComFormer")
code = '''
public static void copyFile( File in, File out )
throws IOException
{
FileChannel inChannel = new FileInputStream( in ).getChannel();
FileChannel outChannel = new FileOutputStream( out ).getChannel();
try
{
// inChannel.transferTo(0, inChannel.size(), outChannel); // original -- apparently has trouble copying large files on Windows
// magic number for Windows, 64Mb - 32Kb)
int maxCount = (64 * 1024 * 1024) - (32 * 1024);
long size = inChannel.size();
long position = 0;
while ( position < size )
{
position += inChannel.transferTo( position, maxCount, outChannel );
}
}
finally
{
if ( inChannel != null )
{
inChannel.close();
}
if ( outChannel != null )
{
outChannel.close();
}
}
}
'''
code_seq, sbt = utils.transformer(code)
input_text = code_seq + sbt
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=256, truncation=True)
summary_text_ids = model.generate(
input_ids=input_ids,
bos_token_id=model.config.bos_token_id,
eos_token_id=model.config.eos_token_id,
length_penalty=2.0,
max_length=30,
min_length=2,
num_beams=5,
)
comment = tokenizer.decode(summary_text_ids[0], skip_special_tokens=True)
print(comment)
📄 许可证
本项目采用Apache 2.0许可证。
📚 详细文档
模型信息
属性 |
详情 |
模型类型 |
基于Transformer的条件生成模型 |
训练数据 |
DeepCom数据集 |
评估指标 |
BLEU |
引用信息
如果您使用了本项目,请引用以下论文:
@misc{yang2021comformer,
title={ComFormer: Code Comment Generation via Transformer and Fusion Method-based Hybrid Code Representation},
author={Guang Yang and Xiang Chen and Jinxin Cao and Shuyuan Xu and Zhanqi Cui and Chi Yu and Ke Liu},
year={2021},
eprint={2107.03644},
archivePrefix={arXiv},
primaryClass={cs.SE}
}