Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
+
from utils.nyt_api import get_top_stories
|
| 4 |
+
from utils.imgflip_api import create_meme
|
| 5 |
+
from config.meme_templates import MEME_TEMPLATES, get_template_for_article
|
| 6 |
+
from config.settings import NYT_CATEGORIES
|
| 7 |
+
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="The Meme York Times",
|
| 10 |
+
page_icon="📰",
|
| 11 |
+
layout="wide"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
def main():
|
| 15 |
+
# App header
|
| 16 |
+
st.title("The Meme York Times")
|
| 17 |
+
st.subheader("Where News Meets Memes")
|
| 18 |
+
|
| 19 |
+
# Sidebar for settings
|
| 20 |
+
st.sidebar.title("Settings")
|
| 21 |
+
|
| 22 |
+
# Category selection
|
| 23 |
+
category = st.sidebar.selectbox(
|
| 24 |
+
"Select News Category",
|
| 25 |
+
options=NYT_CATEGORIES
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Fetch news button
|
| 29 |
+
if st.sidebar.button("Get Meme News"):
|
| 30 |
+
with st.spinner("Fetching the latest news and creating memes..."):
|
| 31 |
+
# Get top stories from NYT API
|
| 32 |
+
articles = get_top_stories(category)
|
| 33 |
+
|
| 34 |
+
if not articles:
|
| 35 |
+
st.error("Unable to fetch articles. Please try again later.")
|
| 36 |
+
return
|
| 37 |
+
|
| 38 |
+
# Display article memes in a grid
|
| 39 |
+
col1, col2 = st.columns(2)
|
| 40 |
+
|
| 41 |
+
for i, article in enumerate(articles[:6]): # Limit to 6 articles
|
| 42 |
+
# Get a suitable meme template for this article
|
| 43 |
+
template_id, text_fields = get_template_for_article(article)
|
| 44 |
+
|
| 45 |
+
# Create the meme
|
| 46 |
+
meme_url = create_meme(
|
| 47 |
+
template_id=template_id,
|
| 48 |
+
text_fields=text_fields,
|
| 49 |
+
article_title=article['title'],
|
| 50 |
+
article_abstract=article.get('abstract', '')
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Display in alternating columns
|
| 54 |
+
current_col = col1 if i % 2 == 0 else col2
|
| 55 |
+
|
| 56 |
+
with current_col:
|
| 57 |
+
st.subheader(article['title'])
|
| 58 |
+
if meme_url:
|
| 59 |
+
st.image(meme_url, use_column_width=True)
|
| 60 |
+
else:
|
| 61 |
+
st.error("Failed to create meme")
|
| 62 |
+
|
| 63 |
+
st.markdown(f"[Read the full article]({article['url']})")
|
| 64 |
+
st.markdown("---")
|
| 65 |
+
else:
|
| 66 |
+
# Display welcome message
|
| 67 |
+
st.info("Select a news category and click 'Get Meme News' to start!")
|
| 68 |
+
|
| 69 |
+
# Show random meme templates as examples
|
| 70 |
+
st.subheader("Example Meme Templates")
|
| 71 |
+
cols = st.columns(3)
|
| 72 |
+
sample_templates = random.sample(list(MEME_TEMPLATES.values()), 3)
|
| 73 |
+
|
| 74 |
+
for col, template in zip(cols, sample_templates):
|
| 75 |
+
with col:
|
| 76 |
+
st.image(f"https://i.imgflip.com/thumbnail/{template['id']}.jpg",
|
| 77 |
+
use_column_width=True)
|
| 78 |
+
st.caption(template['name'])
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
main()
|