Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit e46ef29

Browse files
Merge pull request #437 from darshbaxi/summarizar
Summarizer Added For Medium Article Reader
2 parents 809ae5f + 5ee66f5 commit e46ef29

1 file changed

Lines changed: 39 additions & 14 deletions

File tree

  • projects/Medium Article Reader
Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import pyttsx3
22
import requests
3+
import os
34
from bs4 import BeautifulSoup
5+
import openai
6+
from dotenv import load_dotenv
7+
from langchain.chat_models import ChatOpenAI
8+
from langchain.document_loaders import WebBaseLoader
9+
from langchain.chains.summarize import load_summarize_chain
10+
11+
load_dotenv()
12+
openai_api_key = os.getenv("API_KEY")
13+
openai.openai_api_key = openai_api_key
414

515
engine = pyttsx3.init("sapi5")
616
voices = engine.getProperty("voices")
@@ -11,17 +21,32 @@ def speak(audio):
1121
engine.say(audio)
1222
engine.runAndWait()
1323

14-
15-
text = str(input("Paste article\n"))
16-
res = requests.get(text)
17-
soup = BeautifulSoup(res.text, "html.parser")
18-
19-
articles = []
20-
21-
for i in range(len(soup.select(".p"))):
22-
article = soup.select(".p")[i].getText().strip()
23-
articles.append(article)
24-
25-
text = " ".join(articles)
26-
27-
speak(text)
24+
try:
25+
text = input("Paste article URL: ")
26+
27+
## scraping from URL
28+
res = requests.get(text)
29+
soup = BeautifulSoup(res.text, "html.parser")
30+
articles = []
31+
for p in soup.select("p"):
32+
article = p.getText().strip()
33+
articles.append(article)
34+
txt = " ".join(articles)
35+
36+
37+
## loading text from URL for summarizing..
38+
loader = WebBaseLoader(text)
39+
docs = loader.load()
40+
llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-16k",openai_api_key=openai.openai_api_key)
41+
chain = load_summarize_chain(llm, chain_type="stuff")
42+
print(chain.run(docs))
43+
44+
45+
user_choice=int(input("Enter 1 for summary of article or Enter 2 for whole article: "))
46+
if(user_choice==1):
47+
speak(chain.run(docs))
48+
else:
49+
speak(txt)
50+
51+
except requests.exceptions.RequestException as e:
52+
print(f"An error occurred: {e}")

0 commit comments

Comments
 (0)