File size: 1,541 Bytes
4944c37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import re  # โ† ๋ฌธ์žฅ ๋ถ„๋ฆฌ์šฉ

# 1. ๋””๋ฐ”์ด์Šค ์„ค์ •
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# 2. ํ•œ๊ตญ์–ด GPT-2 ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ
tokenizer = AutoTokenizer.from_pretrained("skt/kogpt2-base-v2")
model = AutoModelForCausalLM.from_pretrained("skt/kogpt2-base-v2").to(device)

# 3. ํ•œ๊ตญ์–ด ์†Œ์„ค ์ƒ์„ฑ ํ•จ์ˆ˜ (4๋ฌธ์žฅ๋งŒ ์ถœ๋ ฅ)
def generate_korean_story(prompt, max_length=300, num_sentences=4):
    input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)

    outputs = model.generate(
        input_ids,
        max_length=max_length,
        min_length=100,
        do_sample=True,
        temperature=0.9,
        top_k=50,
        top_p=0.95,
        repetition_penalty=1.2,
        no_repeat_ngram_size=3,
        eos_token_id=tokenizer.eos_token_id
    )

    full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)

    # ๋ฌธ์žฅ ๋‹จ์œ„๋กœ ์ž๋ฅด๊ธฐ (์ •๊ทœํ‘œํ˜„์‹์œผ๋กœ ๋งˆ์นจํ‘œ/๋ฌผ์Œํ‘œ/๋А๋‚Œํ‘œ ๊ธฐ์ค€)
    sentences = re.split(r'(?<=[.?!])\s+', full_text.strip())

    # ์•ž์—์„œ 4๋ฌธ์žฅ๋งŒ ์„ ํƒ ํ›„ ํ•ฉ์น˜๊ธฐ
    story = " ".join(sentences[:num_sentences])
    return story

# 4. ์‹คํ–‰
if __name__ == "__main__":
    user_prompt = input("๐Ÿ“œ ์†Œ์„ค์˜ ์‹œ์ž‘ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์„ธ์š” (ํ•œ๊ตญ์–ด): ")
    result = generate_korean_story(user_prompt, max_length=500, num_sentences=4)
    
    print("\n๐Ÿ“– ์ƒ์„ฑ๋œ ํ•œ๊ตญ์–ด ์†Œ์„ค (4๋ฌธ์žฅ):\n")
    print(result)