Spaces:
Runtime error
Runtime error
heecheol.park commited on
Commit ยท
a8a2dea
1
Parent(s): cc565c5
feat: Remove urls from messages of slack thread
Browse files
app.py
CHANGED
|
@@ -5,23 +5,24 @@ from slack_sdk import WebClient
|
|
| 5 |
from slack_sdk.errors import SlackApiError
|
| 6 |
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def fetch_text_from_slack_thread(bot_token, thread_link):
|
| 9 |
try:
|
| 10 |
client = WebClient(token=bot_token)
|
| 11 |
-
# ์ฐ๋ ๋ ๋งํฌ์์ ์ฑ๋ ID์ ๋ฉ์์ง์ ํ์์คํฌํ๋ฅผ ์ถ์ถ
|
| 12 |
channel_id, message_ts = re.search("\/archives\/(.+)\/p(\d+)", thread_link).groups()
|
| 13 |
thread_ts = f"{message_ts[:-6]}.{message_ts[-6:]}"
|
| 14 |
|
| 15 |
-
# ์ฌ๋ API๋ฅผ ์ฌ์ฉํ์ฌ ์ฐ๋ ๋์ ๋ชจ๋ ๋ฉ์์ง ๊ฐ์ ธ์ค๊ธฐ
|
| 16 |
response = client.conversations_replies(channel=channel_id, ts=thread_ts)
|
| 17 |
|
| 18 |
-
# ์ฐ๋ ๋ ๋ฉ์์ง๋ฅผ ํ๋์ ํ
์คํธ๋ก ๊ฒฐํฉ
|
| 19 |
messages = response["messages"]
|
| 20 |
-
thread_text = " ".join([message["text"] for message in messages])
|
| 21 |
|
| 22 |
return thread_text
|
| 23 |
except SlackApiError as e:
|
| 24 |
-
# API ํธ์ถ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ ์ค๋ฅ ๋ฉ์์ง ์ถ๋ ฅ
|
| 25 |
print(f"Slack API Error: {e}")
|
| 26 |
return ""
|
| 27 |
|
|
@@ -29,7 +30,6 @@ def fetch_text_from_slack_thread(bot_token, thread_link):
|
|
| 29 |
def summarize_article(bot_token, thread_link):
|
| 30 |
article_text = fetch_text_from_slack_thread(bot_token, thread_link)
|
| 31 |
|
| 32 |
-
# ์์ฝ ์ฝ๋
|
| 33 |
WHITESPACE_HANDLER = lambda k: re.sub('\s+', ' ', re.sub('\n+', ' ', k.strip()))
|
| 34 |
model_name = "csebuetnlp/mT5_multilingual_XLSum"
|
| 35 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
@@ -58,7 +58,6 @@ def summarize_article(bot_token, thread_link):
|
|
| 58 |
|
| 59 |
return summary
|
| 60 |
|
| 61 |
-
# Gradio ์ธํฐํ์ด์ค ์ค์
|
| 62 |
iface = gr.Interface(
|
| 63 |
fn=summarize_article,
|
| 64 |
inputs=["text", "text"],
|
|
@@ -67,5 +66,4 @@ iface = gr.Interface(
|
|
| 67 |
description="Slack์ bot ํ ํฐ๊ณผ ์ฐ๋ ๋ ๋งํฌ๋ฅผ ์
๋ ฅ ๋ฐ์์ ๊ทธ ๋ด์ฉ์ ์ฝ๊ณ ์์ฝํด์ค๋๋ค."
|
| 68 |
)
|
| 69 |
|
| 70 |
-
# ์ธํฐํ์ด์ค ์คํ
|
| 71 |
iface.launch()
|
|
|
|
| 5 |
from slack_sdk.errors import SlackApiError
|
| 6 |
|
| 7 |
|
| 8 |
+
def remove_urls(text):
|
| 9 |
+
url_pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
|
| 10 |
+
return url_pattern.sub('', text)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
def fetch_text_from_slack_thread(bot_token, thread_link):
|
| 14 |
try:
|
| 15 |
client = WebClient(token=bot_token)
|
|
|
|
| 16 |
channel_id, message_ts = re.search("\/archives\/(.+)\/p(\d+)", thread_link).groups()
|
| 17 |
thread_ts = f"{message_ts[:-6]}.{message_ts[-6:]}"
|
| 18 |
|
|
|
|
| 19 |
response = client.conversations_replies(channel=channel_id, ts=thread_ts)
|
| 20 |
|
|
|
|
| 21 |
messages = response["messages"]
|
| 22 |
+
thread_text = " ".join([remove_urls(message["text"]) for message in messages])
|
| 23 |
|
| 24 |
return thread_text
|
| 25 |
except SlackApiError as e:
|
|
|
|
| 26 |
print(f"Slack API Error: {e}")
|
| 27 |
return ""
|
| 28 |
|
|
|
|
| 30 |
def summarize_article(bot_token, thread_link):
|
| 31 |
article_text = fetch_text_from_slack_thread(bot_token, thread_link)
|
| 32 |
|
|
|
|
| 33 |
WHITESPACE_HANDLER = lambda k: re.sub('\s+', ' ', re.sub('\n+', ' ', k.strip()))
|
| 34 |
model_name = "csebuetnlp/mT5_multilingual_XLSum"
|
| 35 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 58 |
|
| 59 |
return summary
|
| 60 |
|
|
|
|
| 61 |
iface = gr.Interface(
|
| 62 |
fn=summarize_article,
|
| 63 |
inputs=["text", "text"],
|
|
|
|
| 66 |
description="Slack์ bot ํ ํฐ๊ณผ ์ฐ๋ ๋ ๋งํฌ๋ฅผ ์
๋ ฅ ๋ฐ์์ ๊ทธ ๋ด์ฉ์ ์ฝ๊ณ ์์ฝํด์ค๋๋ค."
|
| 67 |
)
|
| 68 |
|
|
|
|
| 69 |
iface.launch()
|