Anne314159 commited on
Commit
bd14329
·
verified ·
1 Parent(s): 0d7d9fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -46
app.py CHANGED
@@ -1,48 +1,28 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
- import requests
4
- import os
5
 
6
  # Initialize a text generation pipeline
7
  generator = pipeline('text-generation', model='dbmdz/german-gpt2')
8
 
9
- api_key = os.environ['NEWS_API_KEY']
10
-
11
- def fetch_trending_news(topic, api_key, max_items=5):
12
- base_url = "https://newsapi.org/v2/everything"
13
- params = {
14
- "q": topic,
15
- "apiKey": api_key,
16
- "language": "en",
17
- "sortBy": "relevancy",
18
- "pageSize": max_items
19
- }
20
 
21
- try:
22
- response = requests.get(base_url, params=params)
23
- response.raise_for_status()
24
- news_data = response.json()
25
- articles = news_data.get("articles", [])
26
-
27
- formatted_articles = []
28
- for article in articles:
29
- formatted_article = {
30
- "title": article["title"],
31
- "source": article["source"]["name"],
32
- "publishedAt": article["publishedAt"],
33
- "url": article["url"]
34
- }
35
- formatted_articles.append(formatted_article)
36
-
37
- return formatted_articles
38
- except requests.RequestException as e:
39
- st.error(f"Error fetching news data: {e}")
40
- return []
41
 
42
- # Define the pages
43
  def page_trending_niche():
44
- api_key = "YOUR_API_KEY_HERE" # Securely fetch your API key
45
-
46
  col1, col2 = st.columns([3, 1])
47
  with col1:
48
  st.title("What is trending in my niche?")
@@ -51,16 +31,12 @@ def page_trending_niche():
51
 
52
  niche = st.text_input('Enter your niche', 'German clinics')
53
  if niche:
54
- news_items = fetch_trending_news(niche, api_key)
55
- if news_items:
56
- for item in news_items:
57
- st.write(f"**Title:** {item['title']}")
58
- st.write(f"**Source:** {item['source']}")
59
- st.write(f"**Published At:** {item['publishedAt']}")
60
- st.write(f"**URL:** [Read more]({item['url']})")
61
- st.write("---")
62
-
63
-
64
 
65
 
66
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ from pygooglenews import GoogleNews
 
4
 
5
  # Initialize a text generation pipeline
6
  generator = pipeline('text-generation', model='dbmdz/german-gpt2')
7
 
8
+ # Function to fetch top stories related to a specific topic
9
+ def fetch_top_stories(topic, language='de', country='GER'):
10
+ gn = GoogleNews(lang=language, country=country)
11
+ search_results = gn.search(topic)
 
 
 
 
 
 
 
12
 
13
+ top_stories = []
14
+ for story in search_results['entries']:
15
+ story_data = {
16
+ 'title': story.title,
17
+ 'link': story.link,
18
+ 'published': story.published
19
+ }
20
+ top_stories.append(story_data)
21
+
22
+ return top_stories
 
 
 
 
 
 
 
 
 
 
23
 
24
+ # Define the page for trending niche news
25
  def page_trending_niche():
 
 
26
  col1, col2 = st.columns([3, 1])
27
  with col1:
28
  st.title("What is trending in my niche?")
 
31
 
32
  niche = st.text_input('Enter your niche', 'German clinics')
33
  if niche:
34
+ news_items = fetch_top_stories(niche)
35
+ for item in news_items:
36
+ st.write(f"**Title:** {item['title']}")
37
+ st.write(f"**Link:** [Read more]({item['link']})")
38
+ st.write(f"**Published:** {item['published']}")
39
+ st.write("---")
 
 
 
 
40
 
41
 
42