Spaces:
Sleeping
Sleeping
Download search.py from AshokGakr/tiny_model: direct link, hf CLI and curl.
- Browser
- Download file 3.29 kB
-
https://huggingface.co/spaces/AshokGakr/tiny_model/resolve/main/search.py
- Command line
-
hf download hf://spaces/AshokGakr/tiny_model/search.py
-
curl -L -o search.py https://huggingface.co/spaces/AshokGakr/tiny_model/resolve/main/search.py
3.29 kB
| #!/usr/bin/env python3 | |
| """ | |
| Complete search pipeline wrapper. | |
| Runs quick_scrape.py β main_content_cleaner.py | |
| Outputs: structured JSON with filtered results | |
| Usage (CLI): | |
| python search.py --query "today hot news" --max 50 --workers 6 --out results.json | |
| This imports `EnterpriseSearchEngine` from `quick_scrape.py` and `process_results` from `main_content_cleaner.py` | |
| """ | |
| import argparse | |
| import json | |
| from dataclasses import asdict | |
| from pathlib import Path | |
| try: | |
| from quick_scrape import EnterpriseSearchEngine | |
| from main_content_cleaner import process_results | |
| except Exception as e: | |
| raise ImportError("Could not import from quick_scrape.py or main_content_cleaner.py: " + str(e)) | |
| def run_search(query: str, max_results: int = 100, workers: int = 8): | |
| """ | |
| Run complete search pipeline: search β clean β filter. | |
| Args: | |
| query: Search query string | |
| max_results: Max results to fetch | |
| workers: Parallel workers | |
| Returns: | |
| (structured_results, stats) tuple | |
| """ | |
| # Phase 1: Search and extract | |
| engine = EnterpriseSearchEngine(max_workers=workers) | |
| raw_results = engine.execute_search(query, max_results) | |
| print(f"π Total chars in raw_results( from search.py): {len(raw_results)}") | |
| # Convert dataclass results to plain dicts | |
| results_dicts = [asdict(r) for r in raw_results] | |
| # Phase 2: Clean and filter by extraction_status == "success" | |
| structured_results, cleaner_stats = process_results(results_dicts) | |
| # Combine stats | |
| combined_stats = { | |
| 'search_engine': engine.stats, | |
| 'cleaner': cleaner_stats | |
| } | |
| print(f"π Total chars in extraction_status( from search.py): {len(structured_results)}") | |
| return structured_results, combined_stats | |
| def main(): | |
| parser = argparse.ArgumentParser(description='Complete search pipeline: search β clean β filter') | |
| parser.add_argument('--query', '-q', required=True, help='Search query') | |
| parser.add_argument('--max', '-m', type=int, default=100, help='Max results') | |
| parser.add_argument('--workers', '-w', type=int, default=8, help='Parallel workers') | |
| parser.add_argument('--out', '-o', default='struct_format_results.json', help='Output structured JSON path') | |
| args = parser.parse_args() | |
| # Run full pipeline | |
| structured_results, stats = run_search(args.query, max_results=args.max, workers=args.workers) | |
| # Build final output | |
| output = { | |
| 'query': args.query, | |
| 'parameters': {'max_results': args.max, 'workers': args.workers}, | |
| 'stats': stats, | |
| 'structured_results': structured_results | |
| } | |
| # Save structured JSON | |
| out_path = Path(args.out) | |
| out_path.write_text(json.dumps(output, indent=2, ensure_ascii=False), encoding='utf-8') | |
| print(f'\nβ SEARCH PIPELINE COMPLETE!') | |
| print(f' π Query: {args.query}') | |
| print(f' π Total results from search: {stats["search_engine"]["total"]}') | |
| print(f' β Successfully extracted: {stats["cleaner"]["successful"]}') | |
| print(f' β Failed (ignored): {stats["cleaner"]["failed"]}') | |
| print(f' π Structured JSON: {out_path}') | |
| print(f' β±οΈ Execution time: {stats["search_engine"]["execution_time"]:.1f}s') | |
| if __name__ == '__main__': | |
| main() | |