File size: 741 Bytes
2a1cdbf
a3e70bc
2a1cdbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from transformers import pipeline
import os 
summarizer = pipeline("summarization", model="t5-small")

def summarize_code(code_dir, headings_path):
    sections = {}
    with open(headings_path, "r") as hfile:
        headings = [line.strip() for line in hfile if line.strip()]

    for heading in headings:
        combined_code = ""
        for root, _, files in os.walk(code_dir):
            for file in files:
                with open(os.path.join(root, file), "r", encoding="utf-8", errors="ignore") as f:
                    combined_code += f.read() + "\n"

        summary = summarizer(combined_code[:1000], max_length=120, min_length=30, do_sample=False)[0]["summary_text"]
        sections[heading] = summary

    return sections