negi2725 commited on
Commit
270f7bb
·
verified ·
1 Parent(s): 93a05a4

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -30
main.py CHANGED
@@ -1,58 +1,50 @@
1
  import os
2
-
3
- # Hugging Face Spaces: redirect cache to /tmp
4
- os.environ["HF_HUB_CACHE"] = "/tmp/hf_cache"
5
- os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
6
-
7
- import os
8
-
9
- # Redirect Hugging Face cache to local project directory (not /)
10
- os.environ["HF_HOME"] = "./hf_cache"
11
- os.environ["TRANSFORMERS_CACHE"] = "./hf_cache"
12
- os.environ["HF_DATASETS_CACHE"] = "./hf_cache"
13
- os.environ["HF_METRICS_CACHE"] = "./hf_cache"
14
-
15
  import uvicorn
16
  from fastapi import FastAPI
17
  from fastapi.middleware.cors import CORSMiddleware
 
18
  from app.api.routes import router
19
  from app.core.config import settings
20
- import logging
21
- import os
22
- from huggingface_hub import snapshot_download
23
 
 
 
 
 
 
 
 
 
24
  logging.basicConfig(level=logging.INFO)
25
  logger = logging.getLogger(__name__)
26
-
27
- # Hugging Face model & dataset download
28
  logger.info("Downloading model and dataset from Hugging Face Hub...")
29
 
30
- HF_TOKEN = os.getenv("HF_TOKEN") # Set in environment variables
 
31
 
32
- # Download FAISS index dataset
33
- FAISS_INDEX_PATH = snapshot_download(
34
- repo_id="negi2725/dataRag",
35
- repo_type="dataset",
36
  token=HF_TOKEN,
37
- local_dir="./hf_cache/faiss_index",
38
  local_dir_use_symlinks=False
39
  )
40
 
41
- # Download LegalBERT model
42
- MODEL_PATH = snapshot_download(
43
- repo_id="negi2725/legalBert",
44
  token=HF_TOKEN,
45
- local_dir="./hf_cache/legalbert_model",
46
  local_dir_use_symlinks=False
47
  )
48
 
49
  logger.info(f"FAISS index files downloaded to: {FAISS_INDEX_PATH}")
50
  logger.info(f"Model files downloaded to: {MODEL_PATH}")
51
 
52
- # Make these paths accessible globally in your app if needed
53
- os.environ["FAISS_INDEX_PATH"] = FAISS_INDEX_PATH
54
  os.environ["MODEL_PATH"] = MODEL_PATH
 
55
 
 
56
  app = FastAPI(
57
  title="Legal RAG Analysis API",
58
  description="FastAPI backend for legal case analysis using RAG system with LegalBERT predictions and Gemini AI evaluation",
@@ -67,6 +59,7 @@ app.add_middleware(
67
  allow_headers=["*"],
68
  )
69
 
 
70
  app.include_router(router, prefix="/api/v1")
71
 
72
  @app.get("/")
@@ -77,6 +70,7 @@ async def root():
77
  async def health_check():
78
  return {"status": "healthy", "message": "API is running"}
79
 
 
80
  if __name__ == "__main__":
81
  uvicorn.run(
82
  "main:app",
 
1
  import os
2
+ import logging
 
 
 
 
 
 
 
 
 
 
 
 
3
  import uvicorn
4
  from fastapi import FastAPI
5
  from fastapi.middleware.cors import CORSMiddleware
6
+ 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
+ os.environ["HF_HOME"] = "./hf_cache"
12
+ os.environ["TRANSFORMERS_CACHE"] = "./hf_cache"
13
+ os.environ["HF_HUB_CACHE"] = "./hf_cache"
14
+ os.environ["HF_DATASETS_CACHE"] = "./hf_cache"
15
+ os.environ["HF_METRICS_CACHE"] = "./hf_cache"
16
+
17
+ # ====== Step 2: Logging setup ======
18
  logging.basicConfig(level=logging.INFO)
19
  logger = logging.getLogger(__name__)
 
 
20
  logger.info("Downloading model and dataset from Hugging Face Hub...")
21
 
22
+ # ====== Step 3: Load your Hugging Face token ======
23
+ HF_TOKEN = os.getenv("HF_TOKEN")
24
 
25
+ # ====== Step 4: Download model and FAISS index ======
26
+ MODEL_PATH = snapshot_download(
27
+ repo_id="negi2725/legalBert",
 
28
  token=HF_TOKEN,
29
+ local_dir="./models/legalbert_model",
30
  local_dir_use_symlinks=False
31
  )
32
 
33
+ FAISS_INDEX_PATH = snapshot_download(
34
+ repo_id="negi2725/dataRag",
 
35
  token=HF_TOKEN,
36
+ local_dir="./faiss_indexes",
37
  local_dir_use_symlinks=False
38
  )
39
 
40
  logger.info(f"FAISS index files downloaded to: {FAISS_INDEX_PATH}")
41
  logger.info(f"Model files downloaded to: {MODEL_PATH}")
42
 
43
+ # ====== Step 5: Make paths globally available ======
 
44
  os.environ["MODEL_PATH"] = MODEL_PATH
45
+ os.environ["FAISS_INDEX_PATH"] = FAISS_INDEX_PATH
46
 
47
+ # ====== Step 6: Initialize FastAPI ======
48
  app = FastAPI(
49
  title="Legal RAG Analysis API",
50
  description="FastAPI backend for legal case analysis using RAG system with LegalBERT predictions and Gemini AI evaluation",
 
59
  allow_headers=["*"],
60
  )
61
 
62
+ # Register API routes
63
  app.include_router(router, prefix="/api/v1")
64
 
65
  @app.get("/")
 
70
  async def health_check():
71
  return {"status": "healthy", "message": "API is running"}
72
 
73
+ # ====== Step 7: Run locally if needed ======
74
  if __name__ == "__main__":
75
  uvicorn.run(
76
  "main:app",