Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
-
from serpapi import SerpApiClient
|
| 4 |
from openai import OpenAI
|
| 5 |
|
| 6 |
# ==============================
|
|
@@ -13,24 +13,24 @@ if not OPENAI_API_KEY or not SERPAPI_API_KEY:
|
|
| 13 |
st.error("❌ Missing API keys! Add OPENAI_API_KEY and SERPAPI_API_KEY in Settings → Secrets.")
|
| 14 |
st.stop()
|
| 15 |
|
| 16 |
-
# Initialize OpenAI client
|
| 17 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 18 |
|
| 19 |
# ==============================
|
| 20 |
-
# 🔍 Function: Fetch AI news using SerpAPI
|
| 21 |
# ==============================
|
| 22 |
def fetch_ai_news(query="latest artificial intelligence news"):
|
|
|
|
| 23 |
params = {
|
| 24 |
"q": query,
|
| 25 |
-
"api_key": SERPAPI_API_KEY,
|
| 26 |
"engine": "google",
|
|
|
|
| 27 |
"num": 5,
|
| 28 |
}
|
| 29 |
-
|
| 30 |
-
|
| 31 |
news_list = []
|
| 32 |
|
| 33 |
-
for result in
|
| 34 |
title = result.get("title", "No title")
|
| 35 |
link = result.get("link", "")
|
| 36 |
snippet = result.get("snippet", "")
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
import streamlit as st
|
|
|
|
| 4 |
from openai import OpenAI
|
| 5 |
|
| 6 |
# ==============================
|
|
|
|
| 13 |
st.error("❌ Missing API keys! Add OPENAI_API_KEY and SERPAPI_API_KEY in Settings → Secrets.")
|
| 14 |
st.stop()
|
| 15 |
|
|
|
|
| 16 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 17 |
|
| 18 |
# ==============================
|
| 19 |
+
# 🔍 Function: Fetch AI news using SerpAPI REST API
|
| 20 |
# ==============================
|
| 21 |
def fetch_ai_news(query="latest artificial intelligence news"):
|
| 22 |
+
url = "https://serpapi.com/search.json"
|
| 23 |
params = {
|
| 24 |
"q": query,
|
|
|
|
| 25 |
"engine": "google",
|
| 26 |
+
"api_key": SERPAPI_API_KEY,
|
| 27 |
"num": 5,
|
| 28 |
}
|
| 29 |
+
response = requests.get(url, params=params)
|
| 30 |
+
data = response.json()
|
| 31 |
news_list = []
|
| 32 |
|
| 33 |
+
for result in data.get("organic_results", []):
|
| 34 |
title = result.get("title", "No title")
|
| 35 |
link = result.get("link", "")
|
| 36 |
snippet = result.get("snippet", "")
|