-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarize.py
More file actions
55 lines (43 loc) · 1.68 KB
/
Copy pathsummarize.py
File metadata and controls
55 lines (43 loc) · 1.68 KB
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
import validators
from langchain.prompts import PromptTemplate
from langchain.document_loaders import UnstructuredURLLoader
from langchain.chains import LLMChain
from bs4 import BeautifulSoup
async def summarize(ctx, url: str, llm):
# Validate URL
if not validators.url(url): # type: ignore
await ctx.send("Please enter a valid URL.")
return
# Create headers for the URL request
headers = {"User-Agent": "Mozilla/5.0"}
# Initialize the loader with the URL and headers
loader = UnstructuredURLLoader(urls=[url], ssl_verify=False, headers=headers)
try:
# Load data from the URL
data = loader.load()[0]
except Exception as e:
await ctx.send(f"An error occurred while loading the URL: {e}")
return
# Extract text content from the HTML
soup = BeautifulSoup(data.page_content, "html.parser")
text_content = soup.get_text()
# Create the summarization prompt
prompt = PromptTemplate(
template="Write a summary of the following in 250-300 words:\n\n{text}\n\nSummary:\n",
input_variables=["text"],
)
# Initialize the summarization process
llm_chain = LLMChain(llm=llm, prompt=prompt)
# Create the data dictionary for the LLM chain
data_for_chain = {"text": text_content}
try:
# Run the LLM chain to get the summary
async with ctx.typing():
summary = llm_chain.run(data_for_chain)
except Exception as e:
await ctx.send(f"An error occurred while summarizing the URL: {e}")
return
# Send the summary to the Discord context
await ctx.send(summary)
# React with a thumbs up
await ctx.message.add_reaction("👍")