Spaces:
Runtime error
Runtime error
Download app.py from pchuri/slack_thread_summarizer: direct link, hf CLI and curl.
- Browser
- Download file 2.22 kB
-
https://huggingface.co/spaces/pchuri/slack_thread_summarizer/resolve/main/app.py
- Command line
-
hf download hf://spaces/pchuri/slack_thread_summarizer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/pchuri/slack_thread_summarizer/resolve/main/app.py
2.22 kB
| 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() | |