11import pyttsx3
22import requests
3+ import os
34from 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
515engine = pyttsx3 .init ("sapi5" )
616voices = 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