Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import BartForConditionalGeneration, PreTrainedTokenizerFast | |
import requests | |
from bs4 import BeautifulSoup | |
# KoBART ๋ชจ๋ธ ๋ก๋ฉ | |
model = BartForConditionalGeneration.from_pretrained("digit82/kobart-summarization") | |
tokenizer = PreTrainedTokenizerFast.from_pretrained("digit82/kobart-summarization") | |
# ๋ด์ค ๋ณธ๋ฌธ ์ถ์ถ ํจ์ | |
def get_article_text(naver_url): | |
headers = {"User-Agent": "Mozilla/5.0"} | |
response = requests.get(naver_url, headers=headers) | |
soup = BeautifulSoup(response.text, "html.parser") | |
article_body = soup.select_one("article") or soup.select_one("#dic_area") | |
return article_body.get_text(strip=True) if article_body else None | |
# ๋ด์ค ์์ฝ ํจ์ | |
def summarize_korean_news(text): | |
input_ids = tokenizer.encode(text, return_tensors="pt", max_length=1024, truncation=True) | |
summary_ids = model.generate(input_ids, max_length=128, min_length=30, num_beams=4, early_stopping=True) | |
return tokenizer.decode(summary_ids[0], skip_special_tokens=True) | |
# Streamlit ์ฑ UI | |
st.title("HL๋ง๋ ๋ด์ค ์์ฝ๊ธฐ") | |
st.write("HL๋ง๋ ๊ด๋ จ ์ต์ ๋ด์ค๋ฅผ ์์ฝํด ๋๋ฆฝ๋๋ค!") | |
# ์ฌ์ฉ์๋ก๋ถํฐ URL ์ ๋ ฅ ๋ฐ๊ธฐ | |
url = st.text_input("๋ด์ค URL์ ์ ๋ ฅํ์ธ์:") | |
if url: | |
article = get_article_text(url) | |
if article: | |
summary = summarize_korean_news(article) | |
st.subheader("๋ด์ค ์์ฝ") | |
st.write(summary) | |
else: | |
st.write("๊ธฐ์ฌ ๋ณธ๋ฌธ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.") | |