트랜스포머 문장 생성하기
트랜스포머 관련 사이트
- https://huggingface.co/
- https://huggingface.co/models
- https://huggingface.co/models?pipeline_tag=text-generation&sort=trending
- https://huggingface.co/distilbert/distilgpt2
- https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation
필요한 모듈 설치하기
distilgpt2 model을 사용하여 문장 생성하기
from transformers import pipeline
generator = pipeline("text-generation", model="distilgpt2")
generator(
"In this course, we will teach you how to",
max_length=30,
num_return_sequences=2,
)
실행결과)
[{'generated_text': 'In this course, we will teach you how to manipulate the world and '
'move your mental and physical capabilities to your advantage.'},
{'generated_text': 'In this course, we will teach you how to become an expert and '
'practice realtime, and with a hands on experience on both real '
'time and real'}]
FacebookAI 모델 사용하여 문장 생성하기
한글 문장 생성기
- text-generation에서 한글 찾을 것 (korean)
- https://huggingface.co/cateto/korean-gpt-neox-125M
cateto/korean-gpt-neox-125M (한글 문장 생성기)
from transformers import pipeline
generator = pipeline(model="cateto/korean-gpt-neox-125M")
generator("우리나라의 미래는 ")
korean-gpt-neox-125M (세분화된 예제: 한글 문장 생성기)
# Import the transformers library
from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("cateto/korean-gpt-neox-125M")
model = AutoModelForCausalLM.from_pretrained("cateto/korean-gpt-neox-125M")
# Get user input
user_input = "우리는 앞으로 더나은 미래를"
# Encode the prompt using the tokenizer
input_ids = tokenizer.encode(user_input, return_tensors="pt")
# Generate chatbot output using the model
output_ids = model.generate(
input_ids,
num_beams=4,
repetition_penalty=1.5,
no_repeat_ngram_size=3
)
# Decode chatbot output ids as text
bot_output = tokenizer.decode(output_ids.tolist()[0], skip_special_tokens=True)
# Print chatbot output
print(f"출력 ## ", bot_output)
# 출력 ## 우리는 앞으로 더나은 미래를 향해 나아갈 수 있다.