Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
10 |
+
NEWS_API_KEY = os.getenv("NEWS_API_KEY")
|
11 |
+
|
12 |
+
st.set_page_config(
|
13 |
+
page_title="Senator Successes",
|
14 |
+
layout="centered"
|
15 |
+
)
|
16 |
+
|
17 |
+
st.title("🇺🇸 Senator Successes")
|
18 |
+
st.markdown("Highlighting recent positive achievements and bipartisan efforts by U.S. Senators.")
|
19 |
+
|
20 |
+
query = "US Senator success OR achievement OR passed"
|
21 |
+
|
22 |
+
@st.cache_data
|
23 |
+
def fetch_news(q):
|
24 |
+
url = "https://newsapi.org/v2/everything"
|
25 |
+
params = {
|
26 |
+
"q": q,
|
27 |
+
"language": "en",
|
28 |
+
"pageSize": 5,
|
29 |
+
"sortBy": "publishedAt",
|
30 |
+
"apiKey": NEWS_API_KEY,
|
31 |
+
}
|
32 |
+
response = requests.get(url, params=params)
|
33 |
+
return response.json().get('articles', [])
|
34 |
+
|
35 |
+
articles = fetch_news(query)
|
36 |
+
|
37 |
+
if articles:
|
38 |
+
for idx, article in enumerate(articles):
|
39 |
+
st.subheader(article['title'])
|
40 |
+
st.caption(f"Source: {article['source']['name']} | Published: {article['publishedAt'][:10]}")
|
41 |
+
if st.button(f"Summarize Article {idx+1}"):
|
42 |
+
with st.spinner('Generating summary...'):
|
43 |
+
prompt = f"Summarize this article highlighting positive bipartisan successes or achievements by US senators:\n\n{article['url']}"
|
44 |
+
summary_response = openai.ChatCompletion.create(
|
45 |
+
model="gpt-4o",
|
46 |
+
messages=[
|
47 |
+
{"role": "system", "content": "You summarize news articles concisely, emphasizing positive bipartisan achievements by US senators."},
|
48 |
+
{"role": "user", "content": prompt}
|
49 |
+
],
|
50 |
+
max_tokens=150,
|
51 |
+
temperature=0.2,
|
52 |
+
)
|
53 |
+
summary = summary_response.choices[0].message.content.strip()
|
54 |
+
st.write(summary)
|
55 |
+
st.markdown("---")
|
56 |
+
else:
|
57 |
+
st.info("No articles found. Try again later.")
|