heecheol.park commited on
Commit
a8a2dea
ยท
1 Parent(s): cc565c5

feat: Remove urls from messages of slack thread

Browse files
Files changed (1) hide show
  1. app.py +6 -8
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()