Spaces:
Runtime error
Runtime error
File size: 2,224 Bytes
10472e3 5062aef 10472e3 a8a2dea 10472e3 cc565c5 10472e3 5062aef 10472e3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import re
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
def remove_reactions(text):
reaction_pattern = re.compile(r':\w+:')
return reaction_pattern.sub('', text)
def remove_urls(text):
url_pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
return url_pattern.sub('', text)
def fetch_text_from_slack_thread(bot_token, thread_link):
try:
client = WebClient(token=bot_token)
channel_id, message_ts = re.search("\/archives\/(.+)\/p(\d+)", thread_link).groups()
thread_ts = f"{message_ts[:-6]}.{message_ts[-6:]}"
response = client.conversations_replies(channel=channel_id, ts=thread_ts)
messages = response["messages"]
thread_text = " ".join([remove_urls(remove_reactions(message["text"])) for message in messages])
return thread_text
except SlackApiError as e:
print(f"Slack API Error: {e}")
return ""
def summarize_article(bot_token, thread_link):
article_text = fetch_text_from_slack_thread(bot_token, thread_link)
WHITESPACE_HANDLER = lambda k: re.sub('\s+', ' ', re.sub('\n+', ' ', k.strip()))
model_name = "csebuetnlp/mT5_multilingual_XLSum"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
input_ids = tokenizer(
[WHITESPACE_HANDLER(article_text)],
return_tensors="pt",
padding="max_length",
truncation=True,
max_length=512
)["input_ids"]
output_ids = model.generate(
input_ids=input_ids,
max_length=84,
no_repeat_ngram_size=2,
num_beams=4
)[0]
summary = tokenizer.decode(
output_ids,
skip_special_tokens=True,
clean_up_tokenization_spaces=False
)
return summary
iface = gr.Interface(
fn=summarize_article,
inputs=["text", "text"],
outputs="text",
title="Slack Thread Summarizer",
description="Slack์ bot ํ ํฐ๊ณผ ์ฐ๋ ๋ ๋งํฌ๋ฅผ ์
๋ ฅ ๋ฐ์์ ๊ทธ ๋ด์ฉ์ ์ฝ๊ณ ์์ฝํด์ค๋๋ค."
)
iface.launch()
|