Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# ๋ชจ๋ธ ๋ก๋
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("noahkim/KoT5_news_summarization")
|
7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("noahkim/KoT5_news_summarization")
|
8 |
+
|
9 |
+
# GPU ์ค์
|
10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
model = model.to(device)
|
12 |
+
|
13 |
+
# ํ
์คํธ ์์ฝ ํจ์
|
14 |
+
def summarize_text(input_text):
|
15 |
+
inputs = tokenizer(input_text, return_tensors="pt", padding="max_length", truncation=True, max_length=2048)
|
16 |
+
inputs = {key: value.to(device) for key, value in inputs.items()}
|
17 |
+
|
18 |
+
summary_text_ids = model.generate(
|
19 |
+
input_ids=inputs['input_ids'],
|
20 |
+
attention_mask=inputs['attention_mask'],
|
21 |
+
max_length=512,
|
22 |
+
min_length=128,
|
23 |
+
num_beams=6,
|
24 |
+
repetition_penalty=1.5,
|
25 |
+
no_repeat_ngram_size=15,
|
26 |
+
)
|
27 |
+
|
28 |
+
summary_text = tokenizer.decode(summary_text_ids[0], skip_special_tokens=True)
|
29 |
+
return summary_text
|
30 |
+
|
31 |
+
# Gradio ์ธํฐํ์ด์ค ์ ์
|
32 |
+
iface = gr.Interface(
|
33 |
+
fn=summarize_text,
|
34 |
+
inputs=gr.Textbox(label="Input Text"),
|
35 |
+
outputs=gr.Textbox(label="Summary")
|
36 |
+
)
|
37 |
+
|
38 |
+
# Space์์ ๋ฐ๋ก ์คํํ ์ ์๋๋ก ์คํ
|
39 |
+
iface.launch()
|