Spaces:
Runtime error
Runtime error
feat: collect news once
#3
by
Hyeonseo
- opened
app.py
CHANGED
|
@@ -1,99 +1,103 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from newspaper import Article
|
| 3 |
-
from newspaper import Config
|
| 4 |
-
|
| 5 |
-
from transformers import pipeline
|
| 6 |
-
import requests
|
| 7 |
-
from bs4 import BeautifulSoup
|
| 8 |
-
import re
|
| 9 |
-
|
| 10 |
-
from bs4 import BeautifulSoup as bs
|
| 11 |
-
import requests
|
| 12 |
-
from transformers import PreTrainedTokenizerFast, BartForConditionalGeneration
|
| 13 |
-
|
| 14 |
-
# Load Model and Tokenize
|
| 15 |
-
def get_summary(input_text):
|
| 16 |
-
tokenizer = PreTrainedTokenizerFast.from_pretrained("ainize/kobart-news")
|
| 17 |
-
summary_model = BartForConditionalGeneration.from_pretrained("ainize/kobart-news")
|
| 18 |
-
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
| 19 |
-
summary_text_ids = summary_model.generate(
|
| 20 |
-
input_ids=input_ids,
|
| 21 |
-
bos_token_id=summary_model.config.bos_token_id,
|
| 22 |
-
eos_token_id=summary_model.config.eos_token_id,
|
| 23 |
-
length_penalty=2.0,
|
| 24 |
-
max_length=142,
|
| 25 |
-
min_length=56,
|
| 26 |
-
num_beams=4,
|
| 27 |
-
)
|
| 28 |
-
return tokenizer.decode(summary_text_ids[0], skip_special_tokens=True)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'
|
| 33 |
-
config = Config()
|
| 34 |
-
config.browser_user_agent = USER_AGENT
|
| 35 |
-
config.request_timeout = 10
|
| 36 |
-
|
| 37 |
-
class news_collector:
|
| 38 |
-
def __init__(self):
|
| 39 |
-
self.examples = []
|
| 40 |
-
|
| 41 |
-
def get_new_parser(self, url):
|
| 42 |
-
article = Article(url, language='ko')
|
| 43 |
-
article.download()
|
| 44 |
-
article.parse()
|
| 45 |
-
return article
|
| 46 |
-
|
| 47 |
-
def get_news_links(self, page=''):
|
| 48 |
-
url = "https://news.daum.net/breakingnews/economic"
|
| 49 |
-
response = requests.get(url)
|
| 50 |
-
html_text = response.text
|
| 51 |
-
|
| 52 |
-
soup = bs(response.text, 'html.parser')
|
| 53 |
-
news_titles = soup.select("a.link_txt")
|
| 54 |
-
links = [item.attrs['href'] for item in news_titles ]
|
| 55 |
-
https_links = [item for item in links if item.startswith('https') == True]
|
| 56 |
-
https_links
|
| 57 |
-
return https_links
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def update_news_examples(self):
|
| 61 |
-
news_links = self.get_news_links()
|
| 62 |
-
for news_url in news_links:
|
| 63 |
-
article = self.get_new_parser(news_url)
|
| 64 |
-
self.examples.append(get_summary(article.text[:1000]))
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from newspaper import Article
|
| 3 |
+
from newspaper import Config
|
| 4 |
+
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
import requests
|
| 7 |
+
from bs4 import BeautifulSoup
|
| 8 |
+
import re
|
| 9 |
+
|
| 10 |
+
from bs4 import BeautifulSoup as bs
|
| 11 |
+
import requests
|
| 12 |
+
from transformers import PreTrainedTokenizerFast, BartForConditionalGeneration
|
| 13 |
+
|
| 14 |
+
# Load Model and Tokenize
|
| 15 |
+
def get_summary(input_text):
|
| 16 |
+
tokenizer = PreTrainedTokenizerFast.from_pretrained("ainize/kobart-news")
|
| 17 |
+
summary_model = BartForConditionalGeneration.from_pretrained("ainize/kobart-news")
|
| 18 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
| 19 |
+
summary_text_ids = summary_model.generate(
|
| 20 |
+
input_ids=input_ids,
|
| 21 |
+
bos_token_id=summary_model.config.bos_token_id,
|
| 22 |
+
eos_token_id=summary_model.config.eos_token_id,
|
| 23 |
+
length_penalty=2.0,
|
| 24 |
+
max_length=142,
|
| 25 |
+
min_length=56,
|
| 26 |
+
num_beams=4,
|
| 27 |
+
)
|
| 28 |
+
return tokenizer.decode(summary_text_ids[0], skip_special_tokens=True)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'
|
| 33 |
+
config = Config()
|
| 34 |
+
config.browser_user_agent = USER_AGENT
|
| 35 |
+
config.request_timeout = 10
|
| 36 |
+
|
| 37 |
+
class news_collector:
|
| 38 |
+
def __init__(self):
|
| 39 |
+
self.examples = []
|
| 40 |
+
|
| 41 |
+
def get_new_parser(self, url):
|
| 42 |
+
article = Article(url, language='ko')
|
| 43 |
+
article.download()
|
| 44 |
+
article.parse()
|
| 45 |
+
return article
|
| 46 |
+
|
| 47 |
+
def get_news_links(self, page=''):
|
| 48 |
+
url = "https://news.daum.net/breakingnews/economic"
|
| 49 |
+
response = requests.get(url)
|
| 50 |
+
html_text = response.text
|
| 51 |
+
|
| 52 |
+
soup = bs(response.text, 'html.parser')
|
| 53 |
+
news_titles = soup.select("a.link_txt")
|
| 54 |
+
links = [item.attrs['href'] for item in news_titles ]
|
| 55 |
+
https_links = [item for item in links if item.startswith('https') == True]
|
| 56 |
+
https_links
|
| 57 |
+
return https_links
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def update_news_examples(self):
|
| 61 |
+
news_links = self.get_news_links()
|
| 62 |
+
for news_url in news_links:
|
| 63 |
+
article = self.get_new_parser(news_url)
|
| 64 |
+
self.examples.append(get_summary(article.text[:1000]))
|
| 65 |
+
|
| 66 |
+
def collect_news():
|
| 67 |
+
news = news_collector()
|
| 68 |
+
news.update_news_examples()
|
| 69 |
+
return news.examples
|
| 70 |
+
|
| 71 |
+
examples = collect_news()
|
| 72 |
+
|
| 73 |
+
title = "๊ท ํ์กํ ๋ด์ค ์ฝ๊ธฐ (Balanced News Reading)"
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
with gr.Blocks() as demo:
|
| 78 |
+
# news = news_collector()
|
| 79 |
+
# news.update_news_examples()
|
| 80 |
+
|
| 81 |
+
with gr.Tab("์๊ฐ"):
|
| 82 |
+
gr.Markdown(
|
| 83 |
+
"""
|
| 84 |
+
# ๊ท ํ์กํ ๋ด์ค ์ฝ๊ธฐ (Balanced News Reading)
|
| 85 |
+
|
| 86 |
+
๊ธ์ ์ ์ธ ๊ธฐ์ฌ์ ๋ถ์ ์ ์ธ ๊ธฐ์ฌ์ธ์ง ํ์ธํ์ฌ ๋ด์ค๋ฅผ ์ฝ์ ์ ์์ต๋๋ค. ์ต๊ทผ ๊ฒฝ์ ๋ด์ค๊ธฐ์ฌ๋ฅผ ๊ฐ์ ธ์ Example์์ ๋ฐ๋ก ํ์ธํ ์ ์๋๋ก ๊ตฌ์ฑํ์ต๋๋ค.
|
| 87 |
+
|
| 88 |
+
## 1. ์ฌ์ฉ๋ฐฉ๋ฒ
|
| 89 |
+
Daum๋ด์ค์ ๊ฒฝ์ ๊ธฐ์ฌ๋ฅผ ๊ฐ์ ธ์ ๋ด์ฉ์ ์์ฝํ๊ณ `Example`์ ๊ฐ์ ธ์ต๋๋ค. ๊ฐ์ ๋ถ์์ ํ๊ณ ์ถ์ ๊ธฐ์ฌ๋ฅผ `Examples`์์ ์ ํํด์ `Submit`์ ๋๋ฅด๋ฉด `Classification`์
|
| 90 |
+
ํด๋น ๊ธฐ์ฌ์ ๊ฐ์ ํ๊ฐ ๊ฒฐ๊ณผ๊ฐ ํ์๋ฉ๋๋ค. ๊ฐ์ ํ๊ฐ๋ ๊ฐ ์ํ์ ํ๋ฅ ์ ๋ณด์ ํจ๊ป `neutral`, `positive`, `negative` 3๊ฐ์ง๋ก ํ์๋ฉ๋๋ค.
|
| 91 |
+
|
| 92 |
+
## 2. ๊ตฌ์กฐ ์ค๋ช
|
| 93 |
+
๋ด์ค๊ธฐ์ฌ๋ฅผ ํฌ๋กค๋ง ๋ฐ ์์ฝ ๋ชจ๋ธ์ ์ด์ฉํ ๊ธฐ์ฌ ์์ฝ >> ๊ธฐ์ฌ ์์ฝ์ ๋ณด Example์ ์ถ๊ฐ >> ํ๊ตญ์ด fine-tunningํ ๊ฐ์ ํ๊ฐ ๋ชจ๋ธ์ ์ด์ฉํด ์
๋ ฅ๋ ๊ธฐ์ฌ์ ๋ํ ๊ฐ์ ํ๊ฐ ์งํ
|
| 94 |
+
""")
|
| 95 |
+
|
| 96 |
+
with gr.Tab("๋ฐ๋ชจ"):
|
| 97 |
+
gr.load("models/gabrielyang/finance_news_classifier-KR_v7",
|
| 98 |
+
inputs = gr.Textbox( placeholder="๋ด์ค ๊ธฐ์ฌ ๋ด์ฉ์ ์
๋ ฅํ์ธ์." ),
|
| 99 |
+
examples=examples)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
demo.launch()
|