Spaces:
Sleeping
Sleeping
Download scripts/index_fact_data.py from thanhnv2323/humanize-text-python: direct link, hf CLI and curl.
- Browser
- Download file 3.47 kB
-
https://huggingface.co/spaces/thanhnv2323/humanize-text-python/resolve/main/scripts/index_fact_data.py
- Command line
-
hf download hf://spaces/thanhnv2323/humanize-text-python/scripts/index_fact_data.py
-
curl -L -o index_fact_data.py https://huggingface.co/spaces/thanhnv2323/humanize-text-python/resolve/main/scripts/index_fact_data.py
3.47 kB
| """Script to chunk and index fact data markdown files into a RAG system.""" | |
| import argparse | |
| from pathlib import Path | |
| import sys | |
| from loguru import logger | |
| from adsp.data_pipeline.embedding_utils import get_configured_embedding_model_name | |
| from adsp.data_pipeline.fact_data_pipeline.extract_raw.config import FactDataExtractionConfig | |
| from adsp.data_pipeline.fact_data_pipeline.rag import ( | |
| documents_to_context_prompt, | |
| run_fact_data_indexing_pipeline, | |
| ) | |
| def main(): | |
| """Chunk and index fact data markdown files.""" | |
| parser = argparse.ArgumentParser( | |
| description="Chunk and index fact data markdown files for RAG retrieval." | |
| ) | |
| parser.add_argument( | |
| "--markdown-dir", | |
| type=str, | |
| default=None, | |
| help="Directory containing markdown files (default: from config)", | |
| ) | |
| parser.add_argument( | |
| "--embedding-model", | |
| type=str, | |
| default=get_configured_embedding_model_name(), | |
| help="Embedding model name for the configured backend", | |
| ) | |
| parser.add_argument( | |
| "--chunk-size", | |
| type=int, | |
| default=1200, | |
| help="Maximum chunk size in characters (default: 1200)", | |
| ) | |
| parser.add_argument( | |
| "--chunk-overlap", | |
| type=int, | |
| default=50, | |
| help="Overlap between chunks in characters (default: 50)", | |
| ) | |
| parser.add_argument( | |
| "--pattern", | |
| type=str, | |
| default="*.md", | |
| help="Glob pattern for markdown files (default: *.md, recursive)", | |
| ) | |
| parser.add_argument( | |
| "--top-k", | |
| type=int, | |
| default=10, | |
| help="Number of results to return for test query (default: 10)", | |
| ) | |
| parser.add_argument( | |
| "--skip-test-query", | |
| action="store_true", | |
| help="Skip running test queries after indexing (queries run by default)" | |
| ) | |
| args = parser.parse_args() | |
| # Determine markdown directory | |
| if args.markdown_dir: | |
| markdown_dir = Path(args.markdown_dir) | |
| else: | |
| config = FactDataExtractionConfig() | |
| markdown_dir = config.fact_data_output_dir / "pages" | |
| if not markdown_dir.exists(): | |
| logger.error(f"Markdown directory not found: {markdown_dir}") | |
| return 1 | |
| # Run the indexing pipeline | |
| try: | |
| rag = run_fact_data_indexing_pipeline( | |
| markdown_dir=markdown_dir, | |
| embedding_model_name=args.embedding_model, | |
| chunk_size=args.chunk_size, | |
| chunk_overlap=args.chunk_overlap, | |
| pattern=args.pattern, | |
| ) | |
| except Exception as e: | |
| logger.error(f"Pipeline failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return 1 | |
| if not args.skip_test_query: | |
| queries = [ | |
| "What are the demographics of Curious Connoisseurs?", | |
| "What coffee brands do they prefer?", | |
| "Tell me about their lifestyle and interests", | |
| ] | |
| for query in queries: | |
| print(f"\nQuery: {query}") | |
| print("=" * 60) | |
| # Search for relevant chunks | |
| results = rag.search(query, k=10) | |
| # Convert to context prompt format | |
| context = documents_to_context_prompt(results) | |
| print(f"\nRetrieved {len(results)} relevant chunks:\n") | |
| print(context) | |
| print("\n" + "=" * 60) | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |