Spaces:
Paused
Paused
lojol469-cmd
Initial commit: Kibali AI with RTX 5090 Blackwell support and CUDA 13.0 Nightly
f3a56a5
| from tavily import TavilyClient | |
| from duckduckgo_search import DDGS | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| TAVILY_API_KEY = os.getenv("TAVILY_API_KEY") | |
| def web_search(query: str): | |
| results = [] | |
| images = [] | |
| # 1. TENTATIVE AVEC TAVILY | |
| if TAVILY_API_KEY: | |
| try: | |
| print(f"🔍 Recherche Tavily pour : {query}") | |
| tavily = TavilyClient(api_key=TAVILY_API_KEY) | |
| res = tavily.search( | |
| query=query, | |
| max_results=5, | |
| search_depth="advanced", | |
| include_images=True | |
| ) | |
| results = res.get('results', []) | |
| images = res.get('images', []) | |
| if len(results) >= 2: | |
| return {"results": results, "images": images, "query": query, "source": "Tavily"} | |
| except Exception as e: | |
| print(f"⚠️ Tavily Error: {e}") | |
| # 2. FALLBACK AVEC DUCKDUCKGO | |
| print(f"🦆 Fallback DuckDuckGo pour : {query}") | |
| try: | |
| with DDGS() as ddgs: | |
| ddg_gen = ddgs.text(query, max_results=5) | |
| if ddg_gen: | |
| for r in ddg_gen: | |
| results.append({ | |
| "title": r.get('title'), | |
| "content": r.get('body'), | |
| "url": r.get('href') | |
| }) | |
| try: | |
| ddg_img_gen = ddgs.images(query, max_results=3) | |
| if ddg_img_gen: | |
| images = [img.get('image') for img in ddg_img_gen if img.get('image')] | |
| except Exception: | |
| pass | |
| except Exception as e: | |
| print(f"❌ DuckDuckGo Error: {e}") | |
| return { | |
| "results": results, | |
| "images": images, | |
| "query": query, | |
| "source": "Fallback/DDG" | |
| } |