🚀 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}
}