|
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(): |
|
|
|
st.title("The Meme York Times") |
|
st.subheader("Where News Meets Memes") |
|
|
|
|
|
st.sidebar.title("Settings") |
|
|
|
|
|
category = st.sidebar.selectbox( |
|
"Select News Category", |
|
options=NYT_CATEGORIES |
|
) |
|
|
|
|
|
if st.sidebar.button("Get Meme News"): |
|
with st.spinner("Fetching the latest news and creating memes..."): |
|
|
|
articles = get_top_stories(category) |
|
|
|
if not articles: |
|
st.error("Unable to fetch articles. Please try again later.") |
|
return |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
|
for i, article in enumerate(articles[:6]): |
|
|
|
template_id, text_fields = get_template_for_article(article) |
|
|
|
|
|
meme_url = create_meme( |
|
template_id=template_id, |
|
text_fields=text_fields, |
|
article_title=article['title'], |
|
article_abstract=article.get('abstract', '') |
|
) |
|
|
|
|
|
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: |
|
|
|
st.info("Select a news category and click 'Get Meme News' to start!") |
|
|
|
|
|
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() |