Anne314159 commited on
Commit
1dd44df
·
verified ·
1 Parent(s): 90a9f61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -11
app.py CHANGED
@@ -94,7 +94,6 @@ def page_article_to_social_post():
94
  st.title("Article to Social Media Post")
95
 
96
  niche = st.text_input('Enter your niche', 'Technology')
97
- article_text = "" # Initialize article_text with an empty string
98
  if niche:
99
  google_news = GNews(language='en', country='US')
100
  news_list = google_news.get_news(niche)
@@ -103,29 +102,43 @@ def page_article_to_social_post():
103
  st.write("No news found for the given niche.")
104
  return
105
 
106
- # Use the titles directly for the selectbox
107
  article_titles = [news['title'] for news in news_list[:5]]
108
  selected_title = st.selectbox("Select an article:", article_titles)
109
 
110
- # Find the selected article's URL based on the title
111
  selected_article = next((item for item in news_list if item['title'] == selected_title), None)
112
  if selected_article:
113
  selected_url = selected_article['url']
114
- # The rest of your code to fetch and display the article or generate the post...
115
- if st.button('Fetch Full Article'):
116
- article_text = fetch_full_article(selected_url)
117
- st.write(article_text[:500], "...") # Display a snippet of the article text
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  if st.button('Generate Social Media Post'):
120
  with st.spinner('Generating...'):
121
- post_content = summarizer(article_text, max_length=150, min_length=30, do_sample=False)[0]['summary_text']
122
- st.success('Generated Content:')
123
- st.write(post_content)
 
 
 
 
 
 
 
 
124
  else:
125
  st.error("Failed to find the selected article.")
126
 
127
 
128
-
129
  def page_test():
130
  st.title('Test Page')
131
  st.write('This is a test page with a test name.')
 
94
  st.title("Article to Social Media Post")
95
 
96
  niche = st.text_input('Enter your niche', 'Technology')
 
97
  if niche:
98
  google_news = GNews(language='en', country='US')
99
  news_list = google_news.get_news(niche)
 
102
  st.write("No news found for the given niche.")
103
  return
104
 
 
105
  article_titles = [news['title'] for news in news_list[:5]]
106
  selected_title = st.selectbox("Select an article:", article_titles)
107
 
 
108
  selected_article = next((item for item in news_list if item['title'] == selected_title), None)
109
  if selected_article:
110
  selected_url = selected_article['url']
 
 
 
 
111
 
112
+ # Use a session state to store the full article text
113
+ if 'full_article_text' not in st.session_state:
114
+ st.session_state.full_article_text = ""
115
+
116
+ if st.button('Fetch Full Article'):
117
+ try:
118
+ # Fetch the full article text and store it in the session state
119
+ st.session_state.full_article_text = fetch_full_article(selected_url)
120
+ # Display the first 500 characters of the article
121
+ st.write(st.session_state.full_article_text[:500], "...")
122
+ except Exception as e:
123
+ st.error(f"Failed to fetch the article. Error: {e}")
124
+
125
  if st.button('Generate Social Media Post'):
126
  with st.spinner('Generating...'):
127
+ try:
128
+ # Ensure there's text to summarize
129
+ if st.session_state.full_article_text.strip():
130
+ # Generate a summary from the full article text
131
+ post_content = summarizer(st.session_state.full_article_text, max_length=150, min_length=30, do_sample=False)[0]['summary_text']
132
+ st.success('Generated Content:')
133
+ st.write(post_content)
134
+ else:
135
+ st.error("Article text is empty. Please fetch the article before generating a post.")
136
+ except Exception as e:
137
+ st.error(f"Failed to generate social media post. Error: {e}")
138
  else:
139
  st.error("Failed to find the selected article.")
140
 
141
 
 
142
  def page_test():
143
  st.title('Test Page')
144
  st.write('This is a test page with a test name.')