inference_prompt_style = """Below is an instruction that describes a task, paired with an input that provides further context.
Write a response that appropriately completes the request.
Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.
Instruction:
아래 뉴스를 읽고 중요한 키워드 중심으로 짧게 요약하세요.
Question:
{}
Response:
{}
{}"""
인퍼런스 함수
THEME_HIST만 입력으로 받아서 요약 생성
def predict_label(text: str) -> str:
# THEME_HIST(뉴스 본문)만 question에 넣습니다
question = text.strip()
# inference_prompt_style에 question만 첫 번째 {}에, 나머지 두 자리는 빈 문자열("")로 채워줍니다
prompt = inference_prompt_style.format(question, "", "") + tokenizer.eos_token
summaries = []
with ThreadPoolExecutor(max_workers=8) as executor:
futures = [executor.submit(infer_one, p) for p in prompts]
for fut in tqdm(as_completed(futures), total=len(futures), desc="Parallel inference"):
summaries.append(fut.result())
후처리 코드
summary 컬럼에서 태그 이후만 추출
df['summary'] = (
df['summary']
.astype(str)
.str.split(r'', n=1) # 로 한 번만 분할
.str[-1] # 뒤쪽 부분 선택
.str.strip() # 앞뒤 공백 제거
)