-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
82 lines (65 loc) · 2.72 KB
/
app.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
import requests
import os
from app_functions import get_top_headlines, search_articles
import streamlit as st
from PIL import Image
API_KEY = ""
#add your news API key here
# sample API_KEY = "0123456789"
image = Image.open(r'..\NewsSummary_Streamlit\news_summary_logo.png')
st.image(image, caption='Smart NEWS')
st.sidebar.image(image, use_column_width=True)
search_choice = st.sidebar.radio('', options=['Top Headlines', 'Search Term'])
sentences_count = st.sidebar.slider('Max sentences per summary:', min_value=1,
max_value=10,
value=3)
if search_choice == 'Top Headlines':
st.markdown(
"""
<style>
div[role=“listbox”] li: {
background-color: red;
}
div[data-baseweb=“select”] > div {
background-color: #99cfdd;
}
div[data-baseweb=“input”] > div {
background-color: #99cfdd;
}
</style>
""",
unsafe_allow_html=True,
)
category = st.sidebar.selectbox('Search By Category:', options=['business',
'entertainment',
'general',
'health',
'science',
'sports',
'technology'], index=2)
summaries = get_top_headlines(sentences_count, apiKey=API_KEY,
sortBy='publishedAt',
country='us',
category=category)
elif search_choice == 'Search Term':
search_term = st.sidebar.text_input('Enter Search Term:')
if not search_term:
summaries = []
st.write('Please enter a search term =)')
else:
summaries = search_articles(sentences_count, apiKey=API_KEY,
sortBy='publishedAt',
q=search_term)
for i in range(len(summaries)):
try:
st.title(summaries[i]['title'])
st.write(f"published at: {summaries[i]['publishedAt']}")
st.write(f"source: {summaries[i]['source']['name']}")
# img
url_image = Image.open(requests.get(summaries[i]['urlToImage'], stream=True).raw)
st.image(url_image, caption='Smart NEWS')
#####
st.write(summaries[i]['summary'])
st.write(f"More details:{summaries[i]['url']}")
except Exception as e:
continue