dindizz commited on
Commit
53ecad3
·
verified ·
1 Parent(s): c00b8ec

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import feedparser
3
+ import gradio as gr
4
+
5
+ # Define the RSS feed URLs
6
+ news_sources = {
7
+ "Times of India": "https://timesofindia.indiatimes.com/rssfeedmostrecent.cms",
8
+ "The Hindu": "https://www.thehindu.com/feeder/default.rss",
9
+ "Live Mint": "https://www.livemint.com/rss/news",
10
+ "Business Standard": "https://www.business-standard.com/rss/latest.rss",
11
+ "Indian Express": "https://indianexpress.com/print/front-page/feed/",
12
+ "Money Control": "https://www.moneycontrol.com/rss/latestnews.xml",
13
+ "Hindustan Times": "https://www.hindustantimes.com/feeds/rss/latest/rssfeed.xml",
14
+ "Business Line": "https://www.thehindubusinessline.com/feeder/default.rss"
15
+ }
16
+
17
+ # Function to fetch and parse RSS feeds
18
+ def fetch_news(source):
19
+ try:
20
+ feed = feedparser.parse(requests.get(source).content)
21
+ news_items = [
22
+ f"{entry.title}: {entry.link}"
23
+ for entry in feed.entries[:10] # Fetch top 10 headlines
24
+ ]
25
+ return "\n\n".join(news_items) if news_items else "No news available."
26
+ except Exception as e:
27
+ return f"Error fetching news: {str(e)}"
28
+
29
+ # Gradio interface function
30
+ def display_news(selected_source):
31
+ source_url = news_sources[selected_source]
32
+ headlines = fetch_news(source_url)
33
+ return headlines
34
+
35
+ # Create Gradio interface
36
+ def create_interface():
37
+ interface = gr.Interface(
38
+ fn=display_news,
39
+ inputs=gr.Dropdown(choices=list(news_sources.keys()), label="Select News Source"),
40
+ outputs=gr.Textbox(label="Top Headlines"),
41
+ title="Top News from Peer Sites",
42
+ description="Select a news source from the dropdown to view its latest headlines."
43
+ )
44
+ return interface
45
+
46
+ # Deploy Gradio app
47
+ if __name__ == "__main__":
48
+ app = create_interface()
49
+ app.launch()