import streamlit as st import openai import requests import os from dotenv import load_dotenv load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") NEWS_API_KEY = os.getenv("NEWS_API_KEY") st.set_page_config( page_title="Senator Successes", layout="centered" ) st.title("🇺🇸 Senator Successes") st.markdown("Highlighting recent positive achievements and bipartisan efforts by U.S. Senators.") query = "Senator success OR achievement" @st.cache_data def fetch_news(q): url = "https://newsapi.org/v2/everything" params = { "q": q, "language": "en", "pageSize": 50, "sortBy": "publishedAt", "apiKey": NEWS_API_KEY, } response = requests.get(url, params=params) return response.json().get('articles', []) articles = fetch_news(query) if articles: for idx, article in enumerate(articles): st.subheader(article['title']) st.caption(f"Source: {article['source']['name']} | Published: {article['publishedAt'][:10]}") if st.button(f"Summarize Article {idx+1}"): with st.spinner('Generating summary...'): prompt = f"Summarize this article highlighting positive bipartisan successes or achievements by US senators:\n\n{article['url']}" summary_response = openai.ChatCompletion.create( model="gpt-4o", messages=[ {"role": "system", "content": "You summarize news articles concisely, emphasizing positive bipartisan achievements by US senators."}, {"role": "user", "content": prompt} ], max_tokens=150, temperature=0.2, ) summary = summary_response.choices[0].message.content.strip() st.write(summary) st.markdown("---") else: st.info("No articles found. Try again later.")