karthik18AI's picture
summary-gen
e08b408 verified
raw
history blame
739 Bytes
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
model_name = "Salesforce/codet5-base"
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
def generate_summary(code_snippet):
inputs = tokenizer(code_snippet, return_tensors="pt", truncation=True, padding="max_length", max_length=512)
summary_ids = model.generate(inputs.input_ids, max_length=150, num_beams=4, length_penalty=2.0, early_stopping=True)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
return summary
# Example C# method
code_snippet = """
public int Add(int a, int b) {
return a + b;
}
"""
summary = generate_summary(code_snippet)
print("Summary:", summary)