Spaces:
Build error

File size: 2,645 Bytes
7e8cd1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/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())