Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -60,10 +60,9 @@ def fetch_full_article(url):
|
|
| 60 |
# Initialize the summarization pipeline with BART
|
| 61 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
summary = summarizer(description, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
| 67 |
return summary
|
| 68 |
|
| 69 |
|
|
@@ -93,50 +92,41 @@ def page_social_media_generator():
|
|
| 93 |
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 |
if niche:
|
|
|
|
| 98 |
google_news = GNews(language='en', country='US')
|
| 99 |
news_list = google_news.get_news(niche)
|
| 100 |
|
| 101 |
if not news_list:
|
| 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 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
if st.button('Generate Social Media Post'):
|
| 126 |
with st.spinner('Generating...'):
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 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():
|
|
|
|
| 60 |
# Initialize the summarization pipeline with BART
|
| 61 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 62 |
|
| 63 |
+
def generate_social_media_post(article_text):
|
| 64 |
+
# Summarize the article text
|
| 65 |
+
summary = summarizer(article_text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
|
|
|
| 66 |
return summary
|
| 67 |
|
| 68 |
|
|
|
|
| 92 |
def page_article_to_social_post():
|
| 93 |
st.title("Article to Social Media Post")
|
| 94 |
|
| 95 |
+
# User input for niche
|
| 96 |
niche = st.text_input('Enter your niche', 'Technology')
|
| 97 |
+
|
| 98 |
if niche:
|
| 99 |
+
# Fetch news articles
|
| 100 |
google_news = GNews(language='en', country='US')
|
| 101 |
news_list = google_news.get_news(niche)
|
| 102 |
|
| 103 |
if not news_list:
|
| 104 |
st.write("No news found for the given niche.")
|
| 105 |
return
|
| 106 |
+
|
| 107 |
+
# Display article titles in a selectbox
|
| 108 |
article_titles = [news['title'] for news in news_list[:5]]
|
| 109 |
selected_title = st.selectbox("Select an article:", article_titles)
|
| 110 |
+
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
if st.button('Fetch Full Article'):
|
| 116 |
+
# Fetch the full article text
|
| 117 |
+
article_text = fetch_full_article(selected_url)
|
| 118 |
+
# Show a snippet of the article to the user
|
| 119 |
+
st.write(article_text[:500], "...")
|
| 120 |
+
|
| 121 |
+
# Store the full article text in session state for later use
|
| 122 |
+
st.session_state.full_article_text = article_text
|
| 123 |
+
|
| 124 |
+
if st.button('Generate Social Media Post') and 'full_article_text' in st.session_state:
|
| 125 |
with st.spinner('Generating...'):
|
| 126 |
+
# Generate a summary based on the full article text
|
| 127 |
+
post_content = generate_social_media_post(st.session_state.full_article_text)
|
| 128 |
+
st.success('Generated Content:')
|
| 129 |
+
st.write(post_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
|
| 132 |
def page_test():
|