Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import PreTrainedTokenizerFast, BartForConditionalGeneration
|
| 3 |
+
|
| 4 |
+
# 1. ๋ชจ๋ธ & ํ ํฌ๋์ด์ ๋ก๋
|
| 5 |
+
MODEL_NAME = "gangyeolkim/kobart-korean-summarizer-v2"
|
| 6 |
+
tokenizer = PreTrainedTokenizerFast.from_pretrained(MODEL_NAME)
|
| 7 |
+
model = BartForConditionalGeneration.from_pretrained(MODEL_NAME)
|
| 8 |
+
|
| 9 |
+
# 2. ์์ฝ ํจ์
|
| 10 |
+
def summarize(text, min_len, max_len):
|
| 11 |
+
if not text.strip():
|
| 12 |
+
return "โ ๏ธ ์์ฝํ ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์."
|
| 13 |
+
|
| 14 |
+
# ํ ํฐํ
|
| 15 |
+
inputs = tokenizer(
|
| 16 |
+
[text],
|
| 17 |
+
max_length=1024,
|
| 18 |
+
truncation=True,
|
| 19 |
+
return_tensors="pt"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# ๋ชจ๋ธ ์ถ๋ก
|
| 23 |
+
summary_ids = model.generate(
|
| 24 |
+
inputs["input_ids"],
|
| 25 |
+
num_beams=4,
|
| 26 |
+
min_length=min_len,
|
| 27 |
+
max_length=max_len,
|
| 28 |
+
early_stopping=True
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# ๋์ฝ๋ฉ
|
| 32 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 33 |
+
return summary
|
| 34 |
+
|
| 35 |
+
# 3. Gradio UI
|
| 36 |
+
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("## ๐ KoBART ํ๊ตญ์ด ์์ฝ๊ธฐ (CPU ์ต์ ํ ๊ฐ๋ฅ)")
|
| 38 |
+
with gr.Row():
|
| 39 |
+
with gr.Column():
|
| 40 |
+
input_text = gr.Textbox(
|
| 41 |
+
label="์๋ฌธ ์
๋ ฅ (์ต๋ 2000์)",
|
| 42 |
+
lines=15,
|
| 43 |
+
placeholder="์ฌ๊ธฐ์ ์์ฝํ ํ๊ตญ์ด ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์."
|
| 44 |
+
)
|
| 45 |
+
min_len = gr.Slider(50, 500, value=100, step=10, label="์ต์ ์์ฝ ๊ธธ์ด")
|
| 46 |
+
max_len = gr.Slider(100, 1500, value=300, step=10, label="์ต๋ ์์ฝ ๊ธธ์ด")
|
| 47 |
+
btn = gr.Button("์์ฝ ์คํ")
|
| 48 |
+
with gr.Column():
|
| 49 |
+
output_text = gr.Textbox(label="์์ฝ ๊ฒฐ๊ณผ", lines=15)
|
| 50 |
+
|
| 51 |
+
btn.click(summarize, inputs=[input_text, min_len, max_len], outputs=output_text)
|
| 52 |
+
|
| 53 |
+
# 4. ์คํ
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
demo.launch()
|