gabrielwithhappy's picture
update model and precondition logic
ea6eb55
raw
history blame
3.28 kB
import gradio as gr
from newspaper import Article
from newspaper import Config
from transformers import pipeline
import requests
from bs4 import BeautifulSoup
import re
from bs4 import BeautifulSoup as bs
import requests
from transformers import PreTrainedTokenizerFast, BartForConditionalGeneration
# Load Model and Tokenize
def get_summary(input_text):
tokenizer = PreTrainedTokenizerFast.from_pretrained("ainize/kobart-news")
summary_model = BartForConditionalGeneration.from_pretrained("ainize/kobart-news")
input_ids = tokenizer.encode(input_text, return_tensors="pt")
summary_text_ids = summary_model.generate(
input_ids=input_ids,
bos_token_id=summary_model.config.bos_token_id,
eos_token_id=summary_model.config.eos_token_id,
length_penalty=2.0,
max_length=142,
min_length=56,
num_beams=4,
)
return tokenizer.decode(summary_text_ids[0], skip_special_tokens=True)
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'
config = Config()
config.browser_user_agent = USER_AGENT
config.request_timeout = 10
class news_collector:
def __init__(self):
self.examples = []
def get_new_parser(self, url):
article = Article(url, language='ko')
article.download()
article.parse()
return article
def get_news_links(self, page=''):
url = "https://news.daum.net/breakingnews/economic"
response = requests.get(url)
html_text = response.text
soup = bs(response.text, 'html.parser')
news_titles = soup.select("a.link_txt")
links = [item.attrs['href'] for item in news_titles ]
https_links = [item for item in links if item.startswith('https') == True]
https_links
return https_links[:2]
def update_news_examples(self):
news_links = self.get_news_links()
for news_url in news_links:
article = self.get_new_parser(news_url)
self.examples.append(get_summary(article.text))
title = "๊ท ํ˜•์žกํžŒ ๋‰ด์Šค ์ฝ๊ธฐ (Balanced News Reading)"
with gr.Blocks() as demo:
news = news_collector()
gr.Markdown(
"""
# ๊ท ํ˜•์žกํžŒ ๋‰ด์Šค ์ฝ๊ธฐ (Balanced News Reading)
๊ธ์ •์ ์ธ ๊ธฐ์‚ฌ์™€ ๋ถ€์ •์ ์ธ ๊ธฐ์‚ฌ์˜ ๊ท ํ˜•์„ ๋ณด๋ฉฐ ๋‰ด์Šค๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ๋ชจ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฐ๋ชจ ์‹คํ–‰ ๋‚ ์งœ์˜ Daum๋‰ด์Šค๋ฅผ `Example`์— ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
๋ชจ๋ธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธธ์ด๋ณด๋‹ค ๊ธด ๊ธฐ์‚ฌ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์‚ฌ๋‚ด์šฉ์„ ์š”์•ฝํ•œ ํ›„ ์š”์—ญ๋œ ๋‚ด์šฉ์„ `Example`์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
๋‰ด์Šค๊ธฐ์‚ฌ๋ฅผ ์„ ํƒํ•˜๊ณ  `Submit`๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๊ธฐ์‚ฌ์˜ ๊ฐ์ •ํ‰๊ฐ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
""")
news.update_news_examples()
gr.load("models/gabrielyang/finance_news_classifier-KR_v7",
inputs = gr.Textbox( placeholder="๋‰ด์Šค ๊ธฐ์‚ฌ ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”." ),
examples=news.examples)
# gr.Examples(
# examples=[
# ["images/demo1.jpg"],
# ["images/demo2.jpg"],
# ["images/demo4.jpg"],
# ],
if __name__ == "__main__":
demo.launch()