-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessUrls.py
43 lines (40 loc) · 1.25 KB
/
processUrls.py
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
from trafilatura import fetch_url, extract
from langchain.schema import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from readability import lix_score
from googlesearch import search
import json
import os
from dotenv import load_dotenv
load_dotenv()
OPENAI_API= os.getenv('OPENAI_API_KEY_2')
prompt = ChatPromptTemplate.from_template(
"Give an english summary of this article {text}. Keep it within 50 words"
)
model = ChatOpenAI(api_key=OPENAI_API)
chain1 = prompt | model | StrOutputParser()
ans=[]
urls=[]
with open("items.txt", 'r') as file:
urlList = json.load(file)
urlList = urlList[3]
print(urlList)
for url in urlList:
if not url:
continue
article={}
downloaded = fetch_url(url)
result = extract(downloaded,include_formatting=False,include_links=False,include_images=False, include_tables=False,target_language="es")
if not result:
print(url + " failed")
continue
score=lix_score(result)
article["score"]=score
article["summary"]=query=chain1.invoke({"text": result[:16000//4]})
article["url"]=url
ans.append(article)
print("finished searching for articles")
print(ans)
with open("articles3.txt", 'w') as file:
json.dump(ans, file)