negi2725 commited on
Commit
9bcd4bc
·
verified ·
1 Parent(s): d54f69e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +7 -10
main.py CHANGED
@@ -7,8 +7,7 @@ from huggingface_hub import snapshot_download
7
  from app.api.routes import router
8
  from app.core.config import settings
9
 
10
- # ====== Step 1: Safe Hugging Face cache location ======
11
- # For Hugging Face Spaces, use /tmp to ensure write access
12
  HF_CACHE_DIR = "/tmp/hf_cache"
13
  os.makedirs(HF_CACHE_DIR, exist_ok=True)
14
 
@@ -18,17 +17,16 @@ os.environ["HF_HUB_CACHE"] = HF_CACHE_DIR
18
  os.environ["HF_DATASETS_CACHE"] = HF_CACHE_DIR
19
  os.environ["HF_METRICS_CACHE"] = HF_CACHE_DIR
20
 
21
- # ====== Step 2: Logging setup ======
22
  logging.basicConfig(level=logging.INFO)
23
  logger = logging.getLogger(__name__)
24
  logger.info("Downloading model and dataset from Hugging Face Hub...")
25
 
26
- # ====== Step 3: Load your Hugging Face token from env ======
27
  HF_TOKEN = os.getenv("HF_TOKEN")
28
  if not HF_TOKEN:
29
  raise RuntimeError("HF_TOKEN environment variable not set")
30
 
31
- # ====== Step 4: Download model and FAISS index into /tmp ======
32
  MODEL_PATH = snapshot_download(
33
  repo_id="negi2725/legalBert",
34
  token=HF_TOKEN,
@@ -38,7 +36,7 @@ MODEL_PATH = snapshot_download(
38
 
39
  FAISS_INDEX_PATH = snapshot_download(
40
  repo_id="negi2725/dataRag",
41
- repo_type="dataset", # <- CRUCIAL!
42
  token=HF_TOKEN,
43
  local_dir="/tmp/faiss_indexes"
44
  )
@@ -46,11 +44,11 @@ FAISS_INDEX_PATH = snapshot_download(
46
  logger.info(f"FAISS index files downloaded to: {FAISS_INDEX_PATH}")
47
  logger.info(f"Model files downloaded to: {MODEL_PATH}")
48
 
49
- # ====== Step 5: Make paths globally available ======
50
  os.environ["MODEL_PATH"] = MODEL_PATH
51
  os.environ["FAISS_INDEX_PATH"] = FAISS_INDEX_PATH
52
 
53
- # ====== Step 6: Initialize FastAPI ======
54
  app = FastAPI(
55
  title="Legal RAG Analysis API",
56
  description="FastAPI backend for legal case analysis using RAG system with LegalBERT predictions and Gemini AI evaluation",
@@ -65,7 +63,7 @@ app.add_middleware(
65
  allow_headers=["*"],
66
  )
67
 
68
- # Register API routes
69
  app.include_router(router, prefix="/api/v1")
70
 
71
  @app.get("/")
@@ -76,7 +74,6 @@ async def root():
76
  async def health_check():
77
  return {"status": "healthy", "message": "API is running"}
78
 
79
- # ====== Step 7: Run locally if needed ======
80
  if __name__ == "__main__":
81
  uvicorn.run(
82
  "main:app",
 
7
  from app.api.routes import router
8
  from app.core.config import settings
9
 
10
+
 
11
  HF_CACHE_DIR = "/tmp/hf_cache"
12
  os.makedirs(HF_CACHE_DIR, exist_ok=True)
13
 
 
17
  os.environ["HF_DATASETS_CACHE"] = HF_CACHE_DIR
18
  os.environ["HF_METRICS_CACHE"] = HF_CACHE_DIR
19
 
20
+
21
  logging.basicConfig(level=logging.INFO)
22
  logger = logging.getLogger(__name__)
23
  logger.info("Downloading model and dataset from Hugging Face Hub...")
24
 
 
25
  HF_TOKEN = os.getenv("HF_TOKEN")
26
  if not HF_TOKEN:
27
  raise RuntimeError("HF_TOKEN environment variable not set")
28
 
29
+
30
  MODEL_PATH = snapshot_download(
31
  repo_id="negi2725/legalBert",
32
  token=HF_TOKEN,
 
36
 
37
  FAISS_INDEX_PATH = snapshot_download(
38
  repo_id="negi2725/dataRag",
39
+ repo_type="dataset",
40
  token=HF_TOKEN,
41
  local_dir="/tmp/faiss_indexes"
42
  )
 
44
  logger.info(f"FAISS index files downloaded to: {FAISS_INDEX_PATH}")
45
  logger.info(f"Model files downloaded to: {MODEL_PATH}")
46
 
47
+
48
  os.environ["MODEL_PATH"] = MODEL_PATH
49
  os.environ["FAISS_INDEX_PATH"] = FAISS_INDEX_PATH
50
 
51
+
52
  app = FastAPI(
53
  title="Legal RAG Analysis API",
54
  description="FastAPI backend for legal case analysis using RAG system with LegalBERT predictions and Gemini AI evaluation",
 
63
  allow_headers=["*"],
64
  )
65
 
66
+
67
  app.include_router(router, prefix="/api/v1")
68
 
69
  @app.get("/")
 
74
  async def health_check():
75
  return {"status": "healthy", "message": "API is running"}
76
 
 
77
  if __name__ == "__main__":
78
  uvicorn.run(
79
  "main:app",