rajesh1729
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -2,15 +2,17 @@ import streamlit as st
|
|
2 |
import json
|
3 |
import requests
|
4 |
from newspaper import Article
|
|
|
5 |
|
6 |
st.set_page_config(page_title='Short News App',
|
7 |
-
layout = 'wide',
|
8 |
-
initial_sidebar_state = 'expanded',
|
9 |
-
menu_items={
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
})
|
|
|
14 |
st.title('Short Summary News Application Demo with OneAI')
|
15 |
st.markdown('This application takes an input from the user and displays upto five latest news articles along with their summary. This application uses the free quota api calls.')
|
16 |
st.sidebar.image('logo.jpg')
|
@@ -29,47 +31,89 @@ st.sidebar.markdown('[Contact](https://www.oneai.com/contact-us)')
|
|
29 |
st.sidebar.markdown('[Community](https://discord.com/channels/941458663493746698/942326235722309642)')
|
30 |
st.sidebar.markdown('© 2022 Logo rights reserved to One AI')
|
31 |
|
32 |
-
|
33 |
-
|
34 |
def run():
|
|
|
|
|
35 |
|
36 |
@st.cache()
|
37 |
def summary(text1):
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
def get_links(text2):
|
48 |
-
url = "https://free-news.p.rapidapi.com/v1/search"
|
49 |
-
querystring = {"q":text2,"lang":"en", "page":1, "page_size":5}
|
50 |
-
headers = {'x-rapidapi-host': "free-news.p.rapidapi.com",'x-rapidapi-key': "375ffbaab0mshb442ffb69d6f025p117ba0jsn01e8146148e3"}
|
51 |
-
response = requests.request("GET", url, headers=headers, params=querystring)
|
52 |
-
response_dict = json.loads(response.text)
|
53 |
-
links = [response_dict['articles'][i]['link'] for i in range(len(response_dict['articles']))]
|
54 |
-
return links
|
55 |
-
|
56 |
input_text = st.text_input('Search your favorite topic:')
|
57 |
submitted = st.button('Submit')
|
58 |
|
59 |
-
if submitted:
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
if __name__ == '__main__':
|
75 |
run()
|
|
|
2 |
import json
|
3 |
import requests
|
4 |
from newspaper import Article
|
5 |
+
from newsapi import NewsApiClient
|
6 |
|
7 |
st.set_page_config(page_title='Short News App',
|
8 |
+
layout = 'wide',
|
9 |
+
initial_sidebar_state = 'expanded',
|
10 |
+
menu_items={
|
11 |
+
'About':'This is a demo application with One AI',
|
12 |
+
'Get help':'https://studio.oneai.com/docs',
|
13 |
+
'Report a Bug':'https://discord.com/channels/941458663493746698/941458828187287603'
|
14 |
+
})
|
15 |
+
|
16 |
st.title('Short Summary News Application Demo with OneAI')
|
17 |
st.markdown('This application takes an input from the user and displays upto five latest news articles along with their summary. This application uses the free quota api calls.')
|
18 |
st.sidebar.image('logo.jpg')
|
|
|
31 |
st.sidebar.markdown('[Community](https://discord.com/channels/941458663493746698/942326235722309642)')
|
32 |
st.sidebar.markdown('© 2022 Logo rights reserved to One AI')
|
33 |
|
|
|
|
|
34 |
def run():
|
35 |
+
# Initialize NewsAPI client
|
36 |
+
newsapi = NewsApiClient(api_key='e07356679fcb40e98d44a37b323e9dd6')
|
37 |
|
38 |
@st.cache()
|
39 |
def summary(text1):
|
40 |
+
try:
|
41 |
+
api_key = "1c93487c-695c-4089-adfc-5e4b7623718c"
|
42 |
+
url = "https://api.oneai.com/api/v0/pipeline"
|
43 |
+
headers = {'api-key': api_key, 'content-type': 'application/json'}
|
44 |
+
payload = {
|
45 |
+
'input': text1,
|
46 |
+
'input_type': 'article',
|
47 |
+
'steps': [{'skill': 'summarize'}]
|
48 |
+
}
|
49 |
+
r = requests.post(url, json=payload, headers=headers)
|
50 |
+
r.raise_for_status()
|
51 |
+
data = r.json()
|
52 |
+
return data['output'][0]['text']
|
53 |
+
except requests.exceptions.RequestException as e:
|
54 |
+
st.error(f"Error calling One AI API: {str(e)}")
|
55 |
+
return None
|
56 |
+
except (KeyError, IndexError) as e:
|
57 |
+
st.error(f"Error processing One AI response: {str(e)}")
|
58 |
+
return None
|
59 |
+
|
60 |
+
def get_links(query):
|
61 |
+
try:
|
62 |
+
all_articles = newsapi.get_everything(q=query,
|
63 |
+
language='en',
|
64 |
+
sort_by='relevancy',
|
65 |
+
page_size=5,
|
66 |
+
page=1)
|
67 |
+
|
68 |
+
if all_articles['status'] != 'ok':
|
69 |
+
st.error(f"NewsAPI Error: {all_articles.get('message', 'Unknown error')}")
|
70 |
+
return []
|
71 |
+
|
72 |
+
links = [article['url'] for article in all_articles['articles']]
|
73 |
+
return links
|
74 |
+
|
75 |
+
except Exception as e:
|
76 |
+
st.error(f"Error fetching news: {str(e)}")
|
77 |
+
return []
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
input_text = st.text_input('Search your favorite topic:')
|
80 |
submitted = st.button('Submit')
|
81 |
|
82 |
+
if submitted and input_text:
|
83 |
+
with st.spinner('Fetching news articles...'):
|
84 |
+
links = get_links(input_text)
|
85 |
+
|
86 |
+
if not links:
|
87 |
+
st.warning("No articles found. Please try a different search term.")
|
88 |
+
return
|
89 |
+
|
90 |
+
for link in links:
|
91 |
+
try:
|
92 |
+
with st.spinner(f'Processing article from {link}...'):
|
93 |
+
news_article = Article(link, language='en')
|
94 |
+
news_article.download()
|
95 |
+
news_article.parse()
|
96 |
+
|
97 |
+
if news_article.top_image:
|
98 |
+
st.image(news_article.top_image)
|
99 |
+
|
100 |
+
st.header(news_article.title)
|
101 |
+
st.markdown('*Summary of the Article:*')
|
102 |
+
|
103 |
+
article_summary = summary(news_article.text)
|
104 |
+
if article_summary:
|
105 |
+
st.markdown(article_summary)
|
106 |
+
else:
|
107 |
+
st.warning("Could not generate summary for this article")
|
108 |
+
|
109 |
+
with st.expander('Full Article'):
|
110 |
+
st.markdown(news_article.text)
|
111 |
+
|
112 |
+
except Exception as e:
|
113 |
+
st.error(f"Error processing article from {link}: {str(e)}")
|
114 |
+
continue
|
115 |
+
elif submitted:
|
116 |
+
st.warning("Please enter a search term")
|
117 |
+
|
118 |
if __name__ == '__main__':
|
119 |
run()
|