memenews / app.py
rushankg's picture
Create app.py
8d5ab2e verified
import streamlit as st
import random
from utils.nyt_api import get_top_stories
from utils.imgflip_api import create_meme
from config.meme_templates import MEME_TEMPLATES, get_template_for_article
from config.settings import NYT_CATEGORIES
st.set_page_config(
page_title="The Meme York Times",
page_icon="πŸ“°",
layout="wide"
)
def main():
# App header
st.title("The Meme York Times")
st.subheader("Where News Meets Memes")
# Sidebar for settings
st.sidebar.title("Settings")
# Category selection
category = st.sidebar.selectbox(
"Select News Category",
options=NYT_CATEGORIES
)
# Fetch news button
if st.sidebar.button("Get Meme News"):
with st.spinner("Fetching the latest news and creating memes..."):
# Get top stories from NYT API
articles = get_top_stories(category)
if not articles:
st.error("Unable to fetch articles. Please try again later.")
return
# Display article memes in a grid
col1, col2 = st.columns(2)
for i, article in enumerate(articles[:6]): # Limit to 6 articles
# Get a suitable meme template for this article
template_id, text_fields = get_template_for_article(article)
# Create the meme
meme_url = create_meme(
template_id=template_id,
text_fields=text_fields,
article_title=article['title'],
article_abstract=article.get('abstract', '')
)
# Display in alternating columns
current_col = col1 if i % 2 == 0 else col2
with current_col:
st.subheader(article['title'])
if meme_url:
st.image(meme_url, use_column_width=True)
else:
st.error("Failed to create meme")
st.markdown(f"[Read the full article]({article['url']})")
st.markdown("---")
else:
# Display welcome message
st.info("Select a news category and click 'Get Meme News' to start!")
# Show random meme templates as examples
st.subheader("Example Meme Templates")
cols = st.columns(3)
sample_templates = random.sample(list(MEME_TEMPLATES.values()), 3)
for col, template in zip(cols, sample_templates):
with col:
st.image(f"https://i.imgflip.com/thumbnail/{template['id']}.jpg",
use_column_width=True)
st.caption(template['name'])
if __name__ == "__main__":
main()