talexm commited on
Commit
f72b5a3
·
1 Parent(s): ceef17e
Files changed (1) hide show
  1. app.py +28 -31
app.py CHANGED
@@ -93,40 +93,37 @@ if st.button("Search News About Me"):
93
  st.warning("Please enter your name or handle to search.")
94
 
95
  # Google Search: Global News Categories
96
- st.subheader("2. Global News Insights")
97
  categories = ["Technology", "Sports", "Politics", "Entertainment", "Science"]
 
 
98
  news_results = {}
99
 
100
- if st.button("Fetch Global News"):
101
- try:
102
- for category in categories:
103
- st.write(f"Fetching news for **{category}**...")
104
- try:
105
- category_results = list(search(f"latest {category} news", num_results=3))
106
- news_results[category] = category_results
107
- except Exception as e:
108
- news_results[category] = [f"Error fetching news: {str(e)}"]
109
- # Convert results into a carousel display
110
- st.subheader("Global News Carousel")
111
- articles = []
112
- for category, urls in news_results.items():
113
- for url in urls:
114
- articles.append({"category": category, "url": url})
115
-
116
- # Implement carousel
117
- if articles:
118
- article_cycle = cycle(articles) # Create a looping iterator
119
- st.write("Use the arrows below to navigate through the articles:")
120
- for _ in range(len(articles)):
121
- article = next(article_cycle)
122
- with st.container():
123
- st.markdown(f"### {article['category']} News")
124
- st.markdown(f"[Read Article Here]({article['url']})")
125
- else:
126
- st.warning("No news articles found.")
127
-
128
- except Exception as e:
129
- st.error(f"An error occurred while fetching global news: {str(e)}")
130
 
131
  # # Display results
132
  # for category, articles in news_results.items():
 
93
  st.warning("Please enter your name or handle to search.")
94
 
95
  # Google Search: Global News Categories
96
+ # Global News Categories
97
  categories = ["Technology", "Sports", "Politics", "Entertainment", "Science"]
98
+
99
+ # News Results
100
  news_results = {}
101
 
102
+ st.title("Global News Insights")
103
+
104
+ try:
105
+ # Fetch News for Each Category
106
+ for category in categories:
107
+ st.markdown(f"Fetching news for **{category}**...")
108
+ try:
109
+ category_results = list(search(f"latest {category} news", num_results=3))
110
+ news_results[category] = category_results
111
+ except Exception as e:
112
+ # Handle Errors and Populate Default Message
113
+ news_results[category] = [f"Error fetching news: {str(e)}"]
114
+
115
+ # Display News Carousel
116
+ st.subheader("Global News Carousel")
117
+ for category, articles in news_results.items():
118
+ st.markdown(f"### {category} News")
119
+ for idx, article in enumerate(articles, start=1):
120
+ if "Error fetching news" in article:
121
+ st.warning(f"News for **{category}** could not be retrieved.")
122
+ else:
123
+ st.markdown(f"{idx}. [Read Article Here]({article})")
124
+
125
+ except Exception as e:
126
+ st.error(f"An unexpected error occurred while fetching global news: {str(e)}")
 
 
 
 
 
127
 
128
  # # Display results
129
  # for category, articles in news_results.items():