Spaces:
Sleeping
Sleeping
| 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." | |
| ) | |