Views
No views yet
1from transformers import T5ForConditionalGeneration, T5Tokenizer
2
3# T5 모델 로드
4model = T5ForConditionalGeneration.from_pretrained("j5ng/et5-typos-corrector")
5tokenizer = T5Tokenizer.from_pretrained("j5ng/et5-typos-corrector")
6
7device = "cuda:0" if torch.cuda.is_available() else "cpu"
8# device = "mps:0" if torch.cuda.is_available() else "cpu" # for mac m1
9
10model = model.to(device)
11
12# 예시 입력 문장
13input_text = "아늬 진짜 무ㅓ하냐고"
14
15# 입력 문장 인코딩
16input_encoding = tokenizer("맞춤법을 고쳐주세요: " + input_text, return_tensors="pt")
17
18input_ids = input_encoding.input_ids.to(device)
19attention_mask = input_encoding.attention_mask.to(device)
20
21# T5 모델 출력 생성
22output_encoding = model.generate(
23 input_ids=input_ids,
24 attention_mask=attention_mask,
25 max_length=128,
26 num_beams=5,
27 early_stopping=True,
28)
29
30# 출력 문장 디코딩
31output_text = tokenizer.decode(output_encoding[0], skip_special_tokens=True)
32
33# 결과 출력
34print(output_text) # 아니 진짜 뭐 하냐고.1from transformers import T5ForConditionalGeneration, T5Tokenizer, pipeline
2
3model = T5ForConditionalGeneration.from_pretrained('j5ng/et5-typos-corrector')
4tokenizer = T5Tokenizer.from_pretrained('j5ng/et5-typos-corrector')
5
6typos_corrector = pipeline(
7 "text2text-generation",
8 model=model,
9 tokenizer=tokenizer,
10 device=0 if torch.cuda.is_available() else -1,
11 framework="pt",
12)
13
14input_text = "완죤 어이업ㅅ네진쨬ㅋㅋㅋ"
15output_text = typos_corrector("맞춤법을 고쳐주세요: " + input_text,
16 max_length=128,
17 num_beams=5,
18 early_stopping=True)[0]['generated_text']
19
20print(output_text) # 완전 어이없네 진짜 ᄏᄏᄏᄏ.