AMdevIA/HFlearningPathAgent
0
1from smolagents import Tool2from duckduckgo_search import DDGS3import re4from typing import Dict5 6class SearchHfRessourcesTool(Tool):7 def __init__(self):8 9 self.name="search_hf_ressources"10 self.description="Search Hugging Face resources by domain and keywords"11 self.inputs = {12 'domain': {13 'type': 'string', 14 'description': 'Main domain (nlp, computer vision, audio, etc.)'15 },16 'keywords': {17 'type': 'string', 18 'description': 'Additional keywords to refine the search',19 'nullable': True20 },21 'ressource_type': {22 'type': 'string', 23 'description': 'Resource type (all, courses, models, datasets, spaces)',24 'nullable': True25 }26 }27 self.output_type="any"28 29 super().__init__()30 31 32 def forward(self, domain: str, keywords: str = "", ressource_type: str = "all") -> Dict:33 # construction of the research request34 search_query = f"{domain} {keywords} site:huggingface.co"35 36 # filter by ressource type37 if ressource_type != "all":38 search_query += f" {ressource_type}"39 40 # use DuckDuckGo search41 results = []42 try:43 ddgs = DDGS()44 results = list(ddgs.text(search_query, max_results=15))45 except Exception as e:46 print(f"Search error: {e}")47 48 # Analyse and categoryze results49 categorized_results = {50 "courses": [],51 "models": [],52 "spaces": [],53 "datasets": [],54 "tutorials": []55 }56 57 for result in results:58 url = result.get("href", "")59 title = result.get("title", "")60 snippet = result.get("body", "")61 62 # categorization by url63 if "/learn/" in url:64 categorized_results["courses"].append({65 "title": title,66 "link": url,67 "description": snippet,68 "type": "course"69 })70 elif "/docs/" in url:71 categorized_results["tutorials"].append({72 "title": title,73 "link": url,74 "description": snippet,75 "type": "tutorial"76 })77 elif "/datasets/" in url:78 categorized_results["datasets"].append({79 "title": title,80 "link": url,81 "description": snippet,82 "type": "dataset"83 })84 elif "/spaces/" in url:85 categorized_results["spaces"].append({86 "title": title,87 "link": url,88 "description": snippet,89 "type": "space"90 })91 92 return categorized_results93 