File size: 1,183 Bytes
076db85
 
 
 
 
2688ff5
076db85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os

# Real-time web search using SerpAPI (Optional)
try:
    from serpapi import GoogleSearch
    SERP_API_KEY = os.environ.get("SERPAPI_API_KEY", "e41a265c89513f03e569eda056f6f50374332cd6c76feb1662baf401c7adb564")  # Add via HF secrets
    serpapi_available = True
except:
    serpapi_available = False

# 🔎 Get online info for query
def web_search(query):
    if serpapi_available and SERP_API_KEY != "your-serpapi-key":
        params = {
            "q": query,
            "api_key": SERP_API_KEY,
            "num": 3,
        }
        search = GoogleSearch(params)
        results = search.get_dict()
        snippets = []

        for result in results.get("organic_results", []):
            snippet = result.get("snippet")
            if snippet:
                snippets.append(snippet)

        return "\n".join(snippets[:3])
    else:
        # Simulated fallback for Hugging Face or offline use
        return (
            "Recent market reports show increased volatility in tech sector.\n"
            "Analysts predict weak Q3 earnings due to supply chain issues.\n"
            "Risk models suggest lowering exposure to tech-heavy portfolios."
        )