File size: 1,777 Bytes
e01db41
1aba9ba
e01db41
1aba9ba
e01db41
e863e76
26b1872
 
e01db41
 
efbaf6a
e01db41
 
1aba9ba
e01db41
efbaf6a
ca81c03
fcea153
efbaf6a
 
 
 
fc66a4d
efbaf6a
e01db41
ca81c03
 
 
efbaf6a
e01db41
ca81c03
e01db41
efbaf6a
e01db41
fc66a4d
efbaf6a
fc66a4d
e01db41
 
fc66a4d
 
 
e01db41
 
fc66a4d
 
 
 
e01db41
fc66a4d
e01db41
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
import os
import requests
import streamlit as st
import openai

# Load secrets
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
SERPAPI_API_KEY = os.environ.get("SERPAPI_API_KEY")

if not OPENAI_API_KEY or not SERPAPI_API_KEY:
    st.error("❌ Missing API keys! Add OPENAI_API_KEY and SERPAPI_API_KEY in Settings → Secrets.")
    st.stop()

openai.api_key = OPENAI_API_KEY

# Fetch AI news from SerpAPI
def fetch_ai_news(query="latest AI news"):
    url = "https://serpapi.com/search.json"
    params = {"q": query, "engine": "google", "api_key": SERPAPI_API_KEY, "num": 5}
    resp = requests.get(url, params=params).json()
    return [{"title": r.get("title",""), "snippet": r.get("snippet",""), "link": r.get("link","")}
            for r in resp.get("organic_results", [])]

# Summarize using old OpenAI API
def summarize_news(news_items):
    combined_text = "\n\n".join([f"{n['title']} - {n['snippet']}" for n in news_items])
    prompt = f"Summarize the following latest AI news into 5 concise bullet points:\n\n{combined_text}"

    response = openai.ChatCompletion.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}],
    )
    return response.choices[0].message["content"]

# Streamlit UI
st.title("🧠 AI News Summarizer")
query = st.text_input("Enter topic", "latest AI news")

if st.button("Fetch & Summarize"):
    news = fetch_ai_news(query)
    if not news:
        st.warning("No news found")
    else:
        st.subheader("📰 Top Results")
        for n in news:
            st.markdown(f"**{n['title']}**")
            st.markdown(n['snippet'])
            st.markdown(f"[Read more]({n['link']})\n---")
        st.subheader("🧩 AI Summary")
        summary = summarize_news(news)
        st.success(summary)