Views
No views yet
transformers library and use it to generate feedback for Korean self-introductions.1
2import torch
3from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
4from peft import LoraConfig, PeftModel
5
6# Load the pre-trained model and tokenizer
7model = AutoModelForCausalLM.from_pretrained("BanAPP/gemma2-2b-kor-resume-feedback")
8tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it", add_special_tokens=True)
9
10# Create a text generation pipeline using the fine-tuned model and tokenizer
11pipe_finetuned = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512)
12
13# Define placeholders for the job, question, and answer
14job = ""
15question = ""
16answer = ""
17
18# Construct a list of messages to be used in the input prompt
19messages = [
20 {
21 "role": "user",
22 "content": (
23 f"자기소개서 문항에 대해서 지원자가 작성한 자기소개서 답변을 지원 직무를 고려하여, 채용 담당자 관점에서 개선점을 피드백 해주세요.\n"
24 f"지원 직무: {job}\n"
25 f"자기소개서 문항: {question}\n"
26 f"자기소개서 답변: {answer}"
27 )
28 }
29]
30
31# Prepare the input prompt using the tokenizer and chat template
32# Note: Apply chat template is used to format the messages as per the chat-based input
33prompt = pipe_finetuned.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
34
35# Generate feedback by passing the formatted prompt to the pipeline
36# Configure sampling parameters to control text generation
37outputs = pipe_finetuned(
38 prompt,
39 do_sample=True, # Enable sampling to generate diverse outputs
40 temperature=0.2, # Control randomness in text generation (lower value makes the output more focused)
41 top_k=50, # Limit the sampling pool to the top 50 tokens
42 top_p=0.95, # Use nucleus sampling to focus on the top 95% of probability mass
43 add_special_tokens=True # Include special tokens as per the model's requirements
44)
45
46# Print the generated feedback, excluding the input prompt from the output
47print(outputs[0]["generated_text"][len(prompt):])
48