CoolFace
Apppublic

krishna0506/Document_Intelligence

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
main.py55 linesDownload Raw Back to root
1from fastapi import FastAPI2from fastapi.middleware.cors import CORSMiddleware3from pathlib import Path4 5from ingest import load_documents6from chunking import fixed_chunk7from embeddings import embed_texts8from vector_store import VectorStore9from api import create_routes10 11# --------------------------------------------------12# SAFE PROJECT ROOT (works in HuggingFace + local)13# --------------------------------------------------14PROJECT_ROOT = Path(__file__).parent15DATA_DIR = PROJECT_ROOT / "data"16 17# --------------------------------------------------18# LOAD DOCUMENTS19# --------------------------------------------------20docs = load_documents(str(DATA_DIR))21 22all_chunks = []23sources = []24 25for doc in docs:26    chunks = fixed_chunk(doc["text"])27    all_chunks.extend(chunks)28    sources.extend([doc["source"]] * len(chunks))29 30# --------------------------------------------------31# CREATE EMBEDDINGS32# --------------------------------------------------33embeddings = embed_texts(all_chunks)34 35# Prevent crash if embeddings empty36dimension = len(embeddings[0]) if len(embeddings) > 0 else 037 38vector_store = VectorStore(dimension)39 40if len(embeddings) > 0:41    vector_store.add(embeddings, all_chunks, sources)42# --------------------------------------------------43# FASTAPI APP44# --------------------------------------------------45app = FastAPI(title="Document Intelligence System")46 47app.add_middleware(48    CORSMiddleware,49    allow_origins=["*"],50    allow_credentials=True,51    allow_methods=["*"],52    allow_headers=["*"],53)54 55app.include_router(create_routes(vector_store))