randeom commited on
Commit
dbec3ef
1 Parent(s): 31fcb8c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from wordcloud import WordCloud
3
+ import requests
4
+ import xml.etree.ElementTree as ET
5
+ from io import BytesIO
6
+ from datetime import datetime, timedelta
7
+
8
+ # Set page configuration
9
+ st.set_page_config(page_title="Word Cloud from News Headlines", layout="wide")
10
+
11
+ # Custom CSS for unique design and to remove the white bar on top
12
+ st.markdown("""
13
+ <style>
14
+ .stApp {
15
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
16
+ color: white;
17
+ }
18
+ .css-18e3th9 {
19
+ background-color: rgba(255, 255, 255, 0.1);
20
+ padding: 20px;
21
+ border-radius: 10px;
22
+ }
23
+ .css-1d391kg {
24
+ background-color: rgba(255, 255, 255, 0.1);
25
+ padding: 20px;
26
+ border-radius: 10px;
27
+ }
28
+ .st-emotion-cache-18ni7ap {
29
+ display: none;
30
+ }
31
+ .stButton button {
32
+ color: black !important;
33
+ background-color: #667eea;
34
+ border-radius: 10px;
35
+ padding: 10px 20px;
36
+ font-weight: bold;
37
+ }
38
+ .headline-container {
39
+ background-color: rgba(255, 255, 255, 0.1);
40
+ padding: 20px;
41
+ border-radius: 10px;
42
+ margin-top: 20px;
43
+ }
44
+ .st-emotion-cache-7ym5gk {
45
+ color: black !important;
46
+ }
47
+ </style>
48
+ """, unsafe_allow_html=True)
49
+
50
+ # Title and description
51
+ st.title("Word Cloud from News Headlines")
52
+ st.markdown("### Generating a word cloud from live news headlines")
53
+
54
+ # Sidebar for user inputs
55
+ st.sidebar.title("Customize Your Word Cloud")
56
+ bg_color = st.sidebar.color_picker("Background Color", "#ffffff")
57
+ max_words = st.sidebar.slider("Maximum Number of Words", 10, 200, 100)
58
+ keyword = st.sidebar.text_input("Search Keyword (Optional)")
59
+ date_range = st.sidebar.date_input("Date Range", [datetime.now() - timedelta(days=7), datetime.now()])
60
+
61
+ # Function to fetch news headlines from Google News RSS feed
62
+ def fetch_news_headlines(keyword=None, date_range=None):
63
+ url = "https://news.google.com/rss"
64
+ if keyword:
65
+ url += f"/search?q={keyword}"
66
+ response = requests.get(url)
67
+ root = ET.fromstring(response.content)
68
+ headlines = [item.find('title').text for item in root.findall('./channel/item')]
69
+ return headlines
70
+
71
+ # Generate word cloud
72
+ headlines = fetch_news_headlines(keyword, date_range)
73
+ if headlines:
74
+ wordcloud_text = ' '.join(headlines)
75
+ wordcloud = WordCloud(width=800, height=400, background_color=bg_color, max_words=max_words).generate(wordcloud_text)
76
+ st.image(wordcloud.to_array(), use_column_width=True)
77
+
78
+ # Download option
79
+ img = BytesIO()
80
+ wordcloud.to_image().save(img, format='PNG')
81
+ st.download_button(label="Download Word Cloud", data=img, file_name="wordcloud.png", mime="image/png")
82
+
83
+ # Display headlines
84
+ st.markdown("### Fetched Headlines")
85
+ with st.expander("Show Headlines"):
86
+ st.markdown('<div class="headline-container">', unsafe_allow_html=True)
87
+ for headline in headlines:
88
+ st.markdown(f"- {headline}")
89
+ st.markdown('</div>', unsafe_allow_html=True)
90
+ else:
91
+ st.warning("No headlines fetched. Please try again later.")
92
+
93
+ # Footer
94
+ st.markdown("""
95
+ <hr>
96
+ <div style="text-align: center;">
97
+ Created by randeom
98
+ </div>
99
+ """, unsafe_allow_html=True)