train_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.
import os
import pandas as pd
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from tqdm.auto import tqdm
import time
1) 경로 설정
base_dir = ## 설정
test_excel = ## 설정
output_excel = ## 설정
2) 허깅페이스 허브 레포 ID
model_id = ## 설정
3) 모델 & 토크나이저 로드
tokenizer = AutoTokenizer.from_pretrained(
model_id,
use_fast=True,
trust_remote_code=True
)
model = AutoModelForCausalLM.from_pretrained(
model_id,
trust_remote_code=True,
torch_dtype=torch.bfloat16,
device_map={"": "cuda"}, # 전 파라미터를 GPU로만 배치
# low_cpu_mem_usage=True, # (선택) 메모리 사용을 줄이는 로드 옵션
)
model.config.use_cache = True
4) Inference 프롬프트 스타일 정의
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:
{}
{}"""
5) 테스트셋 로드
df = pd.read_excel(test_excel, engine='openpyxl')
print(f"Loaded {len(df)} examples from {test_excel}")
6) 인퍼런스 함수 수정: 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