Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,65 @@
|
|
| 1 |
-
|
| 2 |
-
#
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
import requests
|
| 5 |
-
import trafilatura
|
| 6 |
import gradio as gr
|
|
|
|
|
|
|
| 7 |
from markdownify import markdownify as md
|
| 8 |
-
|
| 9 |
from sumy.parsers.plaintext import PlaintextParser
|
| 10 |
from sumy.nlp.tokenizers import Tokenizer
|
| 11 |
from sumy.summarizers.text_rank import TextRankSummarizer
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
# 1) HTML ํ์ฑ
|
| 27 |
-
r = requests.get(url, headers={"User-Agent": "Mozilla/5.0"}, timeout=10)
|
| 28 |
-
r.raise_for_status()
|
| 29 |
-
plain = trafilatura.extract(
|
| 30 |
-
r.text,
|
| 31 |
-
output_format="txt",
|
| 32 |
-
include_tables=False,
|
| 33 |
-
include_comments=False,
|
| 34 |
-
favor_recall=True
|
| 35 |
-
) or ""
|
| 36 |
-
html = trafilatura.extract(
|
| 37 |
-
r.text,
|
| 38 |
-
output_format="html",
|
| 39 |
-
include_tables=False,
|
| 40 |
-
include_comments=False,
|
| 41 |
-
favor_recall=True
|
| 42 |
-
)
|
| 43 |
-
md_preview = md(html or r.text, heading_style="ATX")
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
# ===== Gradio UI =====
|
| 53 |
iface = gr.Interface(
|
| 54 |
-
fn=
|
| 55 |
-
inputs=
|
| 56 |
-
gr.Textbox(label="URL ์
๋ ฅ", placeholder="https://n.news.naver.com/...")
|
| 57 |
-
],
|
| 58 |
outputs=[
|
| 59 |
-
gr.
|
| 60 |
-
gr.Textbox(label="
|
| 61 |
],
|
| 62 |
-
title="
|
| 63 |
-
description="
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
-
iface.launch()
|
|
|
|
| 1 |
+
import nltk
|
| 2 |
+
# NLTK ํ ํฌ๋์ด์ ๋ฆฌ์์ค ์๋ ๋ค์ด๋ก๋
|
| 3 |
+
nltk.download("punkt")
|
| 4 |
+
nltk.download("punkt_tab")
|
| 5 |
|
|
|
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
+
import trafilatura
|
| 8 |
+
import requests
|
| 9 |
from markdownify import markdownify as md
|
|
|
|
| 10 |
from sumy.parsers.plaintext import PlaintextParser
|
| 11 |
from sumy.nlp.tokenizers import Tokenizer
|
| 12 |
from sumy.summarizers.text_rank import TextRankSummarizer
|
| 13 |
|
| 14 |
+
def summarize_text(text, sentence_count=3):
|
| 15 |
+
# ํ๊ตญ์ด๋ ๋ฌธ์ฅ ๋จ์๋ก ๋๊ธฐ ์ํด english ํ ํฌ๋์ด์ ์ฌ์ฉ
|
| 16 |
+
parser = PlaintextParser.from_string(text, Tokenizer("english"))
|
| 17 |
+
summarizer = TextRankSummarizer()
|
| 18 |
+
summary_sentences = summarizer(parser.document, sentence_count)
|
| 19 |
+
return "\n".join(str(sentence) for sentence in summary_sentences)
|
| 20 |
+
|
| 21 |
+
def extract_and_summarize(url):
|
| 22 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 23 |
try:
|
| 24 |
+
r = requests.get(url, headers=headers, timeout=10)
|
| 25 |
+
r.raise_for_status()
|
| 26 |
+
|
| 27 |
+
# HTML ํํ๋ก ๋ณธ๋ฌธ ์ถ์ถ
|
| 28 |
+
html_content = trafilatura.extract(
|
| 29 |
+
r.text,
|
| 30 |
+
output_format="html",
|
| 31 |
+
include_tables=True,
|
| 32 |
+
favor_recall=True
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
if not html_content:
|
| 36 |
+
return "๋ณธ๋ฌธ์ ์ถ์ถํ ์ ์์ต๋๋ค.", ""
|
| 37 |
|
| 38 |
+
# HTML โ Markdown ๋ณํ
|
| 39 |
+
markdown_text = md(html_content, heading_style="ATX")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# ์์ฝ ์์ฑ
|
| 42 |
+
summary = summarize_text(markdown_text, sentence_count=3)
|
| 43 |
|
| 44 |
+
return markdown_text, summary
|
| 45 |
+
|
| 46 |
+
except requests.exceptions.Timeout:
|
| 47 |
+
return "์์ฒญ์ด ์๊ฐ ์ด๊ณผ๋์์ต๋๋ค.", ""
|
| 48 |
+
except requests.exceptions.RequestException as e:
|
| 49 |
+
return f"์์ฒญ ์คํจ: {e}", ""
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"์๋ฌ ๋ฐ์: {e}", ""
|
| 52 |
|
|
|
|
| 53 |
iface = gr.Interface(
|
| 54 |
+
fn=extract_and_summarize,
|
| 55 |
+
inputs=gr.Textbox(label="URL ์
๋ ฅ", placeholder="https://example.com"),
|
|
|
|
|
|
|
| 56 |
outputs=[
|
| 57 |
+
gr.Markdown(label="์ถ์ถ๋ ๋ณธ๋ฌธ"),
|
| 58 |
+
gr.Textbox(label="์๋ ์์ฝ", lines=5)
|
| 59 |
],
|
| 60 |
+
title="๋ณธ๋ฌธ ์ถ์ถ๊ธฐ + ์๋ ์์ฝ",
|
| 61 |
+
description="์นํ์ด์ง URL์ ์
๋ ฅํ๋ฉด ๋ณธ๋ฌธ์ ์ถ์ถํ๊ณ , TextRank ์๊ณ ๋ฆฌ์ฆ์ผ๋ก 3๋ฌธ์ฅ ์์ฝ์ ์ ๊ณตํฉ๋๋ค."
|
| 62 |
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
+
iface.launch()
|