Builds PaperTrail from the base skeleton into a full evidence-verification platform for the "Built with Claude: Life Sciences" hackathon (Builder Track). New named-user features (Build Beyond the Bench): - Lab Notebook Companion — rough bench notes -> structured, searchable, grounded experiment records (lib/labNotebook, /console/lab-notebook, migration 0059) - Clinical Trial Matcher — de-identified patient notes -> ranked eligible ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning (lib/trialMatcher, /console/trial-matcher, migrations 0060) Platform: - Deterministic verification moat (biostats recompute, exact-span grounding) - 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar, PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier) - Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise governance (RBAC, /api/v1 gateway, metering, audit chain, retention) - 17 OSS projects assimilated into native TypeScript on our stack (backend/engines retained + credited in NOTICE) - Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site - Next.js 16 upgrade, benchmark harness (honest smoke-test caveat) Licensing: Apache-2.0 (LICENSE + NOTICE).
92 lines
3.1 KiB
Python
92 lines
3.1 KiB
Python
import os
|
|
import aiohttp
|
|
import tempfile
|
|
from langchain_community.document_loaders import (
|
|
PyMuPDFLoader,
|
|
TextLoader,
|
|
UnstructuredCSVLoader,
|
|
UnstructuredExcelLoader,
|
|
UnstructuredMarkdownLoader,
|
|
UnstructuredPowerPointLoader,
|
|
UnstructuredWordDocumentLoader
|
|
)
|
|
|
|
|
|
class OnlineDocumentLoader:
|
|
|
|
def __init__(self, urls):
|
|
self.urls = urls
|
|
|
|
async def load(self) -> list:
|
|
docs = []
|
|
for url in self.urls:
|
|
pages = await self._download_and_process(url)
|
|
for page in pages:
|
|
if page.page_content:
|
|
docs.append({
|
|
"raw_content": page.page_content,
|
|
"url": page.metadata.get("source")
|
|
})
|
|
|
|
if not docs:
|
|
raise ValueError("🤷 Failed to load any documents!")
|
|
|
|
return docs
|
|
|
|
async def _download_and_process(self, url: str) -> list:
|
|
try:
|
|
headers = {
|
|
"User-Agent": "Mozilla/5.0"
|
|
}
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.get(url, headers=headers, timeout=6) as response:
|
|
if response.status != 200:
|
|
print(f"Failed to download {url}: HTTP {response.status}")
|
|
return []
|
|
|
|
content = await response.read()
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=self._get_extension(url)) as tmp_file:
|
|
tmp_file.write(content)
|
|
tmp_file_path = tmp_file.name
|
|
|
|
return await self._load_document(tmp_file_path, self._get_extension(url).strip('.'))
|
|
except aiohttp.ClientError as e:
|
|
print(f"Failed to process {url}")
|
|
print(e)
|
|
return []
|
|
except Exception as e:
|
|
print(f"Unexpected error processing {url}")
|
|
print(e)
|
|
return []
|
|
|
|
async def _load_document(self, file_path: str, file_extension: str) -> list:
|
|
ret_data = []
|
|
try:
|
|
loader_dict = {
|
|
"pdf": PyMuPDFLoader(file_path),
|
|
"txt": TextLoader(file_path),
|
|
"doc": UnstructuredWordDocumentLoader(file_path),
|
|
"docx": UnstructuredWordDocumentLoader(file_path),
|
|
"pptx": UnstructuredPowerPointLoader(file_path),
|
|
"csv": UnstructuredCSVLoader(file_path, mode="elements"),
|
|
"xls": UnstructuredExcelLoader(file_path, mode="elements"),
|
|
"xlsx": UnstructuredExcelLoader(file_path, mode="elements"),
|
|
"md": UnstructuredMarkdownLoader(file_path)
|
|
}
|
|
|
|
loader = loader_dict.get(file_extension, None)
|
|
if loader:
|
|
ret_data = loader.load()
|
|
|
|
except Exception as e:
|
|
print(f"Failed to load document : {file_path}")
|
|
print(e)
|
|
finally:
|
|
os.remove(file_path) # 删除临时文件
|
|
|
|
return ret_data
|
|
|
|
@staticmethod
|
|
def _get_extension(url: str) -> str:
|
|
return os.path.splitext(url.split("?")[0])[1]
|