diary / app.py
tkdehf2's picture
Update app.py
7662268 verified
import gradio as gr
from transformers import GPT2LMHeadModel, PreTrainedTokenizerFast
# KoGPT2 ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ „์—ญ ๋ณ€์ˆ˜๋กœ ๋ฏธ๋ฆฌ ๋กœ๋“œ
model = GPT2LMHeadModel.from_pretrained("skt/kogpt2-base-v2")
tokenizer = PreTrainedTokenizerFast.from_pretrained("skt/kogpt2-base-v2")
def generate_diary(keywords):
# ํ‚ค์›Œ๋“œ ๊ธฐ๋ฐ˜ ์ผ๊ธฐ ์ƒ์„ฑ
prompt = f"์˜ค๋Š˜์˜ ์ผ๊ธฐ:\n\n{', '.join(keywords.split(','))}์— ๋Œ€ํ•œ ์ผ๊ธฐ๋ฅผ ์จ๋ด…์‹œ๋‹ค."
input_ids = tokenizer.encode(prompt, return_tensors="pt")
output = model.generate(input_ids, max_length=500, num_return_sequences=1, do_sample=True, top_k=50, top_p=0.95, num_beams=5, no_repeat_ngram_size=2)
# ์ƒ์„ฑ๋œ ์ผ๊ธฐ ํ…์ŠคํŠธ ๋ฐ˜ํ™˜
diary = tokenizer.decode(output[0], skip_special_tokens=True)
return diary
def app():
with gr.Blocks() as demo:
gr.Markdown("# ์ž๋™ ์ผ๊ธฐ ์ƒ์„ฑ๊ธฐ")
with gr.Row():
keywords = gr.Textbox(label="5๊ฐœ์˜ ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” (์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„)")
generate_btn = gr.Button("์ผ๊ธฐ ์“ฐ๊ธฐ")
diary = gr.Textbox(label="์ƒ์„ฑ๋œ ์ผ๊ธฐ")
generate_btn.click(generate_diary, inputs=keywords, outputs=diary)
demo.launch()
if __name__ == "__main__":
app()