Spaces:
Sleeping

File size: 2,396 Bytes
5a642b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import fitz
from tqdm import tqdm

def process_missing_books():
    anatomy_dir = r"d:\sample chatbot\MBBS\books\anatomy"
    json_path = r"d:\sample chatbot\anatomy_data.json"
    
    if os.path.exists(json_path):
        print(f"Loading existing data from {json_path}...")
        with open(json_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
    else:
        print(f"Creating new data list.")
        data = []

    processed_books = set(item.get("book_name") for item in data)
    print(f"Found {len(processed_books)} books already in JSON.")

    new_items_added = 0

    for root, dirs, files in os.walk(anatomy_dir):
        for filename in files:
            if not filename.lower().endswith(".pdf"):
                continue
            if filename.startswith("._"):
                continue

            if filename in processed_books:
                continue

            pdf_path = os.path.join(root, filename)
            print(f"Processing missing book: {filename}")
            
            try:
                doc = fitz.open(pdf_path)
                total_pages = doc.page_count
                
                for page_idx in tqdm(range(total_pages), desc=f"Extracting {filename}"):
                    page = doc[page_idx]
                    text = page.get_text().strip()
                    
                    if len(text) < 50:
                        continue
                        
                    page_data = {
                        "book_name": filename,
                        "page_number": page_idx + 1,
                        "content": text,
                        "track": "mbbs",
                        "subject": "anatomy"
                    }
                    data.append(page_data)
                    new_items_added += 1
                    
                doc.close()
                processed_books.add(filename)
            except Exception as e:
                print(f"Error processing {filename}: {e}")

    if new_items_added > 0:
        print(f"Added {new_items_added} new pages. Saving to {json_path}...")
        with open(json_path, 'w', encoding='utf-8') as f:
            json.dump(data, f, indent=2, ensure_ascii=False)
        print("Save complete.")
    else:
        print("No new books found. JSON is up to date.")

if __name__ == "__main__":
    process_missing_books()