diary / app.py
tkdehf2's picture
Update app.py
e59909f verified
raw
history blame
1.11 kB
import gradio as gr
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ „์—ญ ๋ณ€์ˆ˜๋กœ ๋ฏธ๋ฆฌ ๋กœ๋“œ
model = GPT2LMHeadModel.from_pretrained("gpt2")
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
def generate_diary(keywords):
# ํ‚ค์›Œ๋“œ ๊ธฐ๋ฐ˜ fine-tuning
input_ids = tokenizer.encode(" ".join(keywords), 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)
# ์ƒ์„ฑ๋œ ์ผ๊ธฐ ํ…์ŠคํŠธ ๋ฐ˜ํ™˜
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()