-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
230 lines (193 loc) · 8.15 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from concurrent.futures import ThreadPoolExecutor
from pprint import pprint
import newspaper
import requests
import streamlit as st
from together import Together
st.set_page_config(layout="wide")
client = Together(api_key=st.secrets["togetherai"]["api_key"])
config = newspaper.Config()
config.REQUEST_TIMEOUT = 10
config.browser_user_agent = (
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0"
)
config.max_summary = 3000
config.max_summary_sent = 2
# Logic
@st.cache_data
def surf_web(search_query):
base_url = "https://api.search.brave.com/res/v1/web/search"
headers = {
"Accept": "application/json",
"Accept-Encoding": "gzip",
"X-Subscription-Token": st.secrets["brave_search"]["subscription_token"],
}
r = requests.get(base_url, headers=headers, params={"q": search_query})
if not r.status_code == 200:
pprint(f"Error: {r.status_code}")
pprint(r.text)
json_data = r.json()
return json_data
def download_article(url, website_name, title, description):
article = newspaper.Article(url, config=config)
try:
article.download()
article.parse()
article.nlp()
except Exception as e:
pprint(f"Failed to download {url}: {e}")
finally:
article.title = title
article.meta_site_name = website_name
article.meta_description = description
return article
@st.cache_data
def download_articles(search_results):
results = search_results["web"]["results"]
data = {"url": [], "title": [], "website_name": [], "description": []}
for result in results:
data["url"].append(result["url"])
data["title"].append(result["title"])
data["website_name"].append(result["profile"]["name"])
data["description"].append(result["description"])
with ThreadPoolExecutor(max_workers=10) as executor:
articles = list(
executor.map(
download_article,
data["url"],
data["website_name"],
data["title"],
data["description"],
)
)
article_information = []
for article in articles:
article_information.append(
{
"url": article.url,
"title": article.title,
"description": article.meta_description,
"website_name": article.meta_site_name,
"summary": article.summary,
}
)
return article_information
@st.cache_data
def group_articles(articles):
article_information = []
for article in articles:
if not article["summary"] or "Something went wrong" in article["summary"]:
article_information.append(
f"Title: {article['title']}\n Description: {article['description']}\n \
Url: {article['url']}"
)
else:
article_information.append(
f"Title: {article['title']}\n Summary: {article['summary']}\n Url: {article['url']}"
)
messages = [
{
"role": "user",
"content": f"I will give you 20 headlines or article summaries. I want you to \
give me a scale that would let me plot these news articles on a graph. I \
want you to give me five groups for me to place these articles in. Avoid using \
the words Unclear, Miscellaneous, or Unrelated in a category name. Groups that \
include opposing viewpoints should be as far apart on the x-axis as possible. Here \
are the 20 articles: {article_information}.\
Here is the format of your response: \
1. <Group 1> \
- Article: <Article x url> \
- Article: <as many articles as you put in this category> \
2. <Group 2> \
- Article: <Article y url> \
- Article: <as many articles as you put in this category> \
3. <Group 3> \
- Article: <Article z url> \
- Article: <as many articles as you put in this category> \
4. <Group 4> \
- Article: <Article k url> \
- Article: <as many articles as you put in this category> \
5. <Group 5> \
- Article: <Article l url> \
- Article: <as many articles as you put in this category>",
},
]
response = client.chat.completions.create(
model="meta-llama/Llama-3-70b-chat-hf",
messages=messages,
)
lines = response.choices[0].message.content.split("\n")
pprint(articles)
pprint(lines)
curr_group = ""
grouped_articles = {}
for i in range(len(lines)):
line = lines[i].strip("**")
if line.startswith("1.") or line.startswith("2.") or line.startswith("3.") or line.startswith("4.") or line.startswith("5."):
curr_group = line.split(". ")[1]
grouped_articles[curr_group] = []
elif line.startswith("- Article:"):
url = line.split("Article: ")[1]
article = [article for article in articles if article["url"] == url]
if article:
grouped_articles[curr_group].append(article[0])
else:
continue
return grouped_articles
def main():
# UI
query = st.text_input("Search the web privately...")
if query:
st.session_state["search_result"] = None
results = surf_web(query)
articles = download_articles(results)
grouped_articles = group_articles(articles)
group1, group2, group3, group4, group5 = grouped_articles.keys()
group_columns = st.columns(5)
with group_columns[0]:
st.write(f"##### {group1}")
with group_columns[1]:
st.write(f"##### {group2}")
with group_columns[2]:
st.write(f"##### {group3}")
with group_columns[3]:
st.write(f"##### {group4}")
with group_columns[4]:
st.write(f"##### {group5}")
button_columns = st.columns(5, vertical_alignment="bottom")
with st.container():
with button_columns[0]:
for article in grouped_articles[group1]:
if st.button(article["website_name"], help=article["title"], key=article["url"]):
st.session_state["search_result"] = article
with button_columns[1]:
for article in grouped_articles[group2]:
if st.button(article["website_name"], help=article["title"], key=article["url"]):
st.session_state["search_result"] = article
with button_columns[2]:
for article in grouped_articles[group3]:
if st.button(article["website_name"], help=article["title"], key=article["url"]):
st.session_state["search_result"] = article
with button_columns[3]:
for article in grouped_articles[group4]:
if st.button(article["website_name"], help=article["title"], key=article["url"]):
st.session_state["search_result"] = article
with button_columns[4]:
for article in grouped_articles[group5]:
if st.button(article["website_name"], help=article["title"], key=article["url"]):
st.session_state["search_result"] = article
with st.sidebar:
if not st.session_state.get("search_result"):
st.write("Click a button to view the search result here.")
else:
st.write(f'## {st.session_state["search_result"]["title"]}')
if not st.session_state["search_result"]["summary"] or "Something went wrong" in st.session_state["search_result"]["summary"]:
st.markdown(f'{st.session_state["search_result"]["description"]}', unsafe_allow_html=True)
else:
st.write(f'{st.session_state["search_result"]["summary"]}')
st.markdown(
f"[Go to this page]({st.session_state['search_result']['url']})",
unsafe_allow_html=True,
)
if __name__ == "__main__":
main()