Spaces:
Build error
Build error
Download scripts/manage_documents.py from anwarkxc/trackademi: direct link, hf CLI and curl.
- Browser
- Download file 2.65 kB
-
https://huggingface.co/spaces/anwarkxc/trackademi/resolve/main/scripts/manage_documents.py
- Command line
-
hf download hf://spaces/anwarkxc/trackademi/scripts/manage_documents.py
-
curl -L -o manage_documents.py https://huggingface.co/spaces/anwarkxc/trackademi/resolve/main/scripts/manage_documents.py
2.65 kB
| #!/usr/bin/env python3 | |
| """Manage RAG documents and ingest operations.""" | |
| import asyncio | |
| import argparse | |
| from pathlib import Path | |
| from dotenv import load_dotenv | |
| import asyncpg | |
| import os | |
| load_dotenv('.env') | |
| async def list_documents(): | |
| """List all documents in database.""" | |
| db_url = os.getenv('DATABASE_URL').replace('postgresql+asyncpg://', 'postgresql://') | |
| conn = await asyncpg.connect(db_url) | |
| docs = await conn.fetch(""" | |
| SELECT id, title, title_ar, status, chunk_count, file_id, created_at | |
| FROM rag_documents | |
| ORDER BY created_at DESC | |
| LIMIT 10 | |
| """) | |
| print("\n=== RAG Documents ===\n") | |
| if not docs: | |
| print("No documents found") | |
| else: | |
| for d in docs: | |
| print(f"ID: {d['id']}") | |
| print(f"Title: {d['title']}") | |
| print(f"Title AR: {d['title_ar']}") | |
| print(f"Status: {d['status']} | Chunks: {d['chunk_count']}") | |
| print(f"File ID: {d['file_id']}") | |
| print(f"Date: {d['created_at']}") | |
| print("-" * 70) | |
| await conn.close() | |
| async def create_document(title: str, file_key: str, doc_type: str = "TEXTBOOK", grade_level: str = "PRIMARY_6"): | |
| """Create a new document entry.""" | |
| import uuid | |
| db_url = os.getenv('DATABASE_URL').replace('postgresql+asyncpg://', 'postgresql://') | |
| conn = await asyncpg.connect(db_url) | |
| doc_id = str(uuid.uuid4()) | |
| await conn.execute(""" | |
| INSERT INTO rag_documents (id, title, document_type, status, grade_level, chunk_count) | |
| VALUES ($1, $2, $3, 'PENDING', $4, 0) | |
| """, doc_id, title, doc_type, grade_level) | |
| print(f"✓ Created document: {doc_id}") | |
| print(f" Title: {title}") | |
| print(f" Type: {doc_type}") | |
| await conn.close() | |
| return doc_id | |
| async def main(): | |
| parser = argparse.ArgumentParser(description="Manage RAG documents") | |
| subparsers = parser.add_subparsers(dest='command') | |
| # List command | |
| subparsers.add_parser('list', help='List all documents') | |
| # Create command | |
| create_parser = subparsers.add_parser('create', help='Create a new document entry') | |
| create_parser.add_argument('title', help='Document title') | |
| create_parser.add_argument('--file-key', required=True, help='File key/path') | |
| create_parser.add_argument('--grade', default='PRIMARY_6', help='Grade level') | |
| args = parser.parse_args() | |
| if args.command == 'list': | |
| await list_documents() | |
| elif args.command == 'create': | |
| await create_document(args.title, args.file_key, args.grade) | |
| else: | |
| parser.print_help() | |
| if __name__ == '__main__': | |
| asyncio.run(main()) | |