Spaces:
Sleeping
Sleeping
Update tools.py
Browse files7 unmodified lines8910111213141516171819202122232425262718 unmodified lines464748495051522 unmodified lines5556575859606162636465666768697071727374757677787912 unmodified lines92939495969798997 unmodified lines1071081091101111121131147 unmodified linesfrom sklearn.metrics.pairwise import cosine_similarityfrom sklearn.cluster import AgglomerativeClustering# --- Persistent Storage Configuration ---# Hugging Face Spaces typically mounts persistent data to /dataPERSISTENT_BASE = "/data" if os.path.exists("/data") else os.path.dirname(os.path.abspath(__file__))CSV_PATH = os.path.join(PERSISTENT_BASE, "papers.csv")OUT_DIR = os.path.join(PERSISTENT_BASE, "outputs")PDF_DIR = os.path.join(PERSISTENT_BASE, "pdfs")SCRATCH_DIR = os.path.join(PERSISTENT_BASE, "scratch")# Ensure all persistent directories existfor _d in [OUT_DIR, PDF_DIR, SCRATCH_DIR]:os.makedirs(_d, exist_ok=True)HEADERS = ["Sr No", "Title", "DOI", "Web Link", "Authors", "Date of Publication", "Journal", "Abstract", "No of Citations", "Keywords"]PAJAIS_TAXONOMY = {"AI & Machine Learning": ["deep learning","neural networks","NLP","computer vision","reinforcement learning","transformers"],18 unmodified lines@tooldef search_academic_source(query: str, source: str) -> str:"""Search academic sources. Valid sources: 'google_scholar', 'arxiv', 'tavily'."""try:src = source.lower()if "google" in src or "scholar" in src:2 unmodified lineselif "arxiv" in src:from langchain_community.utilities import ArxivAPIWrapperreturn ArxivAPIWrapper(top_k_results=5).run(query)[:5000]else:from langchain_community.tools.tavily_search import TavilySearchResults# Tavily is an LLM-friendly in-built web searcherreturn str(TavilySearchResults(max_results=5).invoke(f"{query} academic paper abstract"))[:5000]except Exception as e: return f"Error searching {source}: {str(e)}"@tooldef download_pdf(url: str, filename: str) -> str:"""Download a PDF from the given URL and save it to the pdfs/ folder. Ensure filename ends with .pdf."""try:r = requests.get(url, stream=True, timeout=12)if r.status_code == 200:if not filename.lower().endswith(".pdf"): filename += ".pdf"path = os.path.join(PDF_DIR, filename)with open(path, 'wb') as f:for chunk in r.iter_content(chunk_size=8192): f.write(chunk)return f"Success: Downloaded to {path}"return f"Failed: HTTP {r.status_code}"except Exception as e: return f"Download error: {e}"@tooldef save_papers(papers_json: str) -> str:12 unmodified linesimpact_words = ["societ", "ethic", "bias", "fairness", "privacy", "impact", "policy", "govern", "regulation", "human"]return any(w in txt for w in ai_words) and any(w in txt for w in impact_words)new_rows = [p for p in incoming if p.get("Title","").strip().lower() not in seen and is_relevant(p)]discarded_count = len([p for p in incoming if p.get("Title","").strip().lower() not in seen and not is_relevant(p)])# Enforce MAX_TOTAL_PAPERS = 120 (Prioritize existing CSV papers)total_current = len(existing)7 unmodified linesif final_new_rows:pd.concat([existing, pd.DataFrame(final_new_rows)]).to_csv(CSV_PATH, index=False)enrich_doi.invoke({}) # Auto-fill DOIs for new papersmsg = f"Saved {len(final_new_rows)} papers. Total: {len(existing)+len(final_new_rows)}."if discarded_count > 0: msg += f" (Discarded {discarded_count} papers for not aligning with AI & Societal Impact)."if len(new_rows) > allowed_new: msg += f" (Truncated {len(new_rows) - allowed_new} exceeding cap)."return msg7 unmodified lines89101112131415161718192021222318 unmodified lines424344454647482 unmodified lines5152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110212 unmodified lines1151161171181191201211221231241251261271281297 unmodified lines1371381391401411421431441451461471481491501511521531547 unmodified linesfrom sklearn.metrics.pairwise import cosine_similarityfrom sklearn.cluster import AgglomerativeClustering# Use /data for persistence if running on HF Spaces with a mounted bucket, otherwise use local directoryROOT_DIR = "/data" if os.path.exists("/data") else os.path.dirname(os.path.abspath(__file__))CSV_PATH = os.path.join(ROOT_DIR, "papers.csv")OUT_DIR = os.path.join(ROOT_DIR, "outputs")PDF_DIR = os.path.join(ROOT_DIR, "pdfs")SCRATCH_DIR = os.path.join(ROOT_DIR, "scratch")for _d in [OUT_DIR, PDF_DIR, SCRATCH_DIR]: os.makedirs(_d, exist_ok=True)HEADERS = ["Sr No", "Title", "DOI", "Web Link", "Authors", "Date of Publication", "Journal", "Abstract", "No of Citations"]PAJAIS_TAXONOMY = {"AI & Machine Learning": ["deep learning","neural networks","NLP","computer vision","reinforcement learning","transformers"],18 unmodified lines@tooldef search_academic_source(query: str, source: str) -> str:"""Search academic sources. Valid sources: 'google_scholar', 'arxiv', 'pubmed', 'hf_papers', 'tavily', 'apify', 'semantic_scholar', 'openalex', 'scopus', 'web_of_science'."""try:src = source.lower()if "google" in src or "scholar" in src:2 unmodified lineselif "arxiv" in src:from langchain_community.utilities import ArxivAPIWrapperreturn ArxivAPIWrapper(top_k_results=5).run(query)[:5000]elif "pubmed" in src:from langchain_community.utilities.pubmed import PubMedAPIWrapperreturn PubMedAPIWrapper(top_k_results=5).run(query)[:5000]elif "hf_papers" in src or "huggingface" in src:res = requests.get(f"https://huggingface.co/api/papers/search?q={urllib.parse.quote(query)}").json()return str(res[:5])[:5000] if isinstance(res, list) else "HF Search Error"elif "apify" in src:from langchain_community.utilities import ApifyWrapperapify = ApifyWrapper()return apify.run("apify/arxiv-scraper", run_input={"searchSubj": "all", "searchQuery": query, "maxResults": 5})[:5000]elif "semantic_scholar" in src:from langchain_community.utilities.semanticscholar import SemanticScholarAPIWrapperreturn SemanticScholarAPIWrapper(top_k_results=5).run(query)[:5000]elif "openalex" in src:api_key = os.getenv("OPENALEX_API_KEY")url = f"https://api.openalex.org/works?search={urllib.parse.quote(query)}&per-page=5"headers = {"User-Agent": "AcademicAgent/1.0 (mailto:agent@research-platform.org)"}if api_key: headers["api_key"] = api_key # Higher rate limits if key providedres = requests.get(url, headers=headers).json()return str(res.get("results", []))[:5000]elif "scopus" in src:api_key = os.getenv("SCOPUS_API_KEY")if not api_key: return "Error: SCOPUS_API_KEY missing. Request the user to provide it or switch to 'semantic_scholar' or 'openalex'."res = requests.get(f"https://api.elsevier.com/content/search/scopus?query={urllib.parse.quote(query)}&count=5", headers={"X-ELS-APIKey": api_key, "Accept": "application/json"}).json()return str(res)[:5000]elif "web_of_science" in src or "wos" in src:api_key = os.getenv("WOS_API_KEY")if not api_key: return "Error: WOS_API_KEY missing. Request the user to provide it or switch to 'semantic_scholar' or 'openalex'."res = requests.get(f"https://wos-api.clarivate.com/api/wos?databaseId=WOS&usrQuery={urllib.parse.quote(query)}&count=5&firstRecord=1", headers={"X-ApiKey": api_key}).json()return str(res)[:5000]else:from langchain_community.tools.tavily_search import TavilySearchResultsreturn str(TavilySearchResults(max_results=5).invoke(f"{query} academic peer reviewed paper"))[:5000]except Exception as e: return f"Error searching {source}: {str(e)}"def _download_pdf(url: str, filename: str):try:r = requests.get(url, stream=True, timeout=12)if r.status_code == 200:if not filename.lower().endswith(".pdf"): filename += ".pdf"path = os.path.join(PDF_DIR, "".join(x for x in filename if x.isalnum() or x in "._- "))with open(path, 'wb') as f:for chunk in r.iter_content(chunk_size=8192): f.write(chunk)return Trueexcept: passreturn False@tooldef save_papers(papers_json: str) -> str:12 unmodified linesimpact_words = ["societ", "ethic", "bias", "fairness", "privacy", "impact", "policy", "govern", "regulation", "human"]return any(w in txt for w in ai_words) and any(w in txt for w in impact_words)# Strict Academic Filter: Discard Non-Papersdef is_research_paper(p):txt = (str(p.get("Title", "")) + " " + str(p.get("Journal", "")) + " " + str(p.get("Abstract", ""))).lower()banned_words = ["book", "chapter", "news", "editorial", "encyclopedia", "newspaper", "magazine"]return not any(w in txt.split() for w in banned_words) # split() guarantees strict word matchingvalid_items = [p for p in incoming if p.get("Title","").strip().lower() not in seen and is_relevant(p) and is_research_paper(p)]discarded_count = len([p for p in incoming if p.get("Title","").strip().lower() not in seen and not (is_relevant(p) and is_research_paper(p))])new_rows = valid_items# Enforce MAX_TOTAL_PAPERS = 120 (Prioritize existing CSV papers)total_current = len(existing)7 unmodified linesif final_new_rows:pd.concat([existing, pd.DataFrame(final_new_rows)]).to_csv(CSV_PATH, index=False)# Auto-download PDFs for new rowsdownload_count = 0for p in final_new_rows:link = p.get("Web Link", "")if link and str(link).lower().endswith(".pdf"):if _download_pdf(link, p.get("Title", "paper")[:50]):download_count += 1enrich_doi.invoke({}) # Auto-fill DOIs for new papersmsg = f"Saved {len(final_new_rows)} papers. Total: {len(existing)+len(final_new_rows)}."if download_count > 0: msg += f" (Archived {download_count} PDFs)."if discarded_count > 0: msg += f" (Discarded {discarded_count} papers for not aligning with AI & Societal Impact)."if len(new_rows) > allowed_new: msg += f" (Truncated {len(new_rows) - allowed_new} exceeding cap)."return msg
tools.py+64-24