File size: 1,927 Bytes
2e3df2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00aad34
2e3df2c
 
 
 
 
 
 
e358e7c
2e3df2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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.")