Spaces:
Runtime error
Runtime error
Download Text2Long_text.py from LLKIM/project: direct link, hf CLI and curl.
- Browser
- Download file 1.19 kB
-
https://huggingface.co/spaces/LLKIM/project/resolve/main/Text2Long_text.py
- Command line
-
hf download hf://spaces/LLKIM/project/Text2Long_text.py
-
curl -L -o Text2Long_text.py https://huggingface.co/spaces/LLKIM/project/resolve/main/Text2Long_text.py
1.19 kB
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| # 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. νκ΅μ΄ μμ€ μμ± ν¨μ | |
| def generate_korean_story(prompt, max_length=100): | |
| 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 | |
| ) | |
| story = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return story | |
| # 4. μ€ν | |
| if __name__ == "__main__": | |
| user_prompt = input("π μμ€μ μμ λ¬Έμ₯μ μ λ ₯νμΈμ (νκ΅μ΄): ") | |
| result = generate_korean_story(user_prompt, max_length=100) | |
| print("\nπ μμ±λ νκ΅μ΄ μμ€:\n") | |
| print(result) | |