Spaces:
Sleeping
Sleeping
Update app.py
Browse files12349 unmodified lines14151617181914 unmodified lines343536373839404142439 unmodified lines53545556575810 unmodified lines69707172737416 unmodified lines919293949596979 unmodified lines1071081091101111121131141151161172 unmodified lines12012112212312412512612712812913013113213313438 unmodified lines1731741751761771781791 unmodified line1811821831841851861872 unmodified lines1901911921931941951961971988 unmodified lines2072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592604 unmodified lines2652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773782 unmodified lines3813823833843853863873883893903911 unmodified line393394395396import osimport jsonimport re9 unmodified linesfrom fastapi import FastAPI, Depends, HTTPException, Headerfrom fastapi.responses import JSONResponsefrom utils.DocsLoader import load_and_chunkfrom utils.Schemas import RunRequest, RunResponse# from concurrent.futures import ThreadPoolExecutor14 unmodified lines# from langchain_nvidia_ai_endpoints.embeddings import NVIDIAEmbeddings# from langchain_nvidia_ai_endpoints.reranking import NVIDIARerankimport osfrom sentence_transformers import SentenceTransformerMODEL_DIR = os.path.join("/tmp", "e5-large-v2")if not os.path.exists(MODEL_DIR):9 unmodified linesvector_cache = {}ml_models = {}secret = ""landmark_data = {"Delhi": "Gateway of India", "Mumbai": "India Gate", "Chennai": "Charminar","Hyderabad": "Taj Mahal", "Ahmedabad": "Howrah Bridge", "Mysuru": "Golconda Fort",10 unmodified lines"Jakarta": "The Shard", "Vienna": "Blue Mosque", "Kathmandu": "Neuschwanstein Castle","Los Angeles": "Buckingham Palace"}PRELOAD_URLS = ["https://hackrx.blob.core.windows.net/assets/Arogya%20Sanjeevani%20Policy%20-%20CIN%20-%20U10200WB1906GOI001713%201.pdf?sv=2023-01-03&st=2025-07-21T08%3A29%3A02Z&se=2025-09-22T08%3A29%3A00Z&sr=b&sp=r&sig=nzrz1K9Iurt%2BBXom%2FB%2BMPTFMFP3PRnIvEsipAX10Ig4%3D","https://hackrx.blob.core.windows.net/assets/Super_Splendor_(Feb_2023).pdf?sv=2023-01-03&st=2025-07-21T08%3A10%3A00Z&se=2025-09-22T08%3A10%3A00Z&sr=b&sp=r&sig=vhHrl63YtrEOCsAy%2BpVKr20b3ZUo5HMz1lF9%2BJh6LQ0%3D",16 unmodified lines@asynccontextmanagerasync def lifespan(app: FastAPI):# This code runs ONCE when the application starts upprint("🚀 Initializing models and prompt template...")try:9 unmodified lines# raise RuntimeError("CRITICAL: Missing nvidia api key in environment secrets!")# Load models into the shared dictionaryml_models["embedder"] = HuggingFaceEmbeddings(# model_name="BAAI/bge-large-en-v1.5", #better but lil slowermodel_name="BAAI/bge-base-en-v1.5", #better but lil slower# model_name="intfloat/e5-large-v2", #lil faster but dont know response is slow# encode_kwargs={# "batch_size": 64,# # "normalize_embeddings": True2 unmodified lines)cross_encoder_model = HuggingFaceCrossEncoder(model_name="BAAI/bge-reranker-base")# cross_encoder_model = HuggingFaceCrossEncoder(model_name="BAAI/bge-reranker-large")ml_models["reranker_compressor"] = CrossEncoderReranker(model=cross_encoder_model, top_n=9)ml_models["llm"] = ChatGoogleGenerativeAI(# model="gemini-1.5-pro",model="gemini-2.5-flash",api_key=GOOGLE_API_KEY,# temperature=0.15,# max_output_tokens=300)ml_models["prompt_template"] = ChatPromptTemplate.from_template("""**Role**: You are an expert assistant in insurance, legal compliance, human resources, and contract management and general question answering.38 unmodified linesprint("❌ Lifespan error:", str(e))raise e# Preload vectorstores for all URLsfor url in PRELOAD_URLS:doc_url = str(url)1 unmodified lineprint(f"📄 Processing new document: {doc_url}")chunks = load_and_chunk(doc_url)# Build vectorstore & save to cachevectorstore = await FAISS.afrom_documents(documents=chunks, embedding=ml_models["embedder"])vector_cache[doc_url] = vectorstore # store in memory cacheprint(f"✅ Vectorstore cached for: {doc_url}")2 unmodified linesprint("🧹 Cleaning up.")ml_models.clear()# --- 2. FastAPI App Instance ---# We pass the lifespan function to the FastAPI constructorapp = FastAPI(title="HackRX RAG Server", lifespan=lifespan)def store_secret(url: str):global secreturl_c = url8 unmodified lines# --- 3. API Key Verification ---TEAM_API_KEY = os.getenv("TEAM_API_KEY")TEAM_API_KEY2 = os.getenv("TEAM_API_KEY2")def verify_api_key(authorization: str = Header(...)):if not authorization.startswith("Bearer "):raise HTTPException(status_code=401, detail="Invalid Authorization header format")token = authorization.split("Bearer ")[1]if token != TEAM_API_KEY and token != TEAM_API_KEY2 and token != secret:raise HTTPException(status_code=403, detail="Invalid or missing API key")# --- 4. Parsing Helper ---def parse_llm_response(content: str) -> str:try:# Remove code fences and clean upcontent_cleaned = re.sub(r"^```json|```$", "", content.strip(), flags=re.IGNORECASE).strip()data = json.loads(content_cleaned)if isinstance(data, dict):if "decision" in data:decision = data.get("decision", "N/A").upper()amount = data.get("amount", "Not specified")justification = data.get("justification", "No justification provided.")return f"Decision: {decision}\nAmount: {amount}\nJustification: {justification}"elif "response" in data:return data["response"]return "The response was parsed but didn't match expected structure."except json.JSONDecodeError:return f"Unstructured response:\n{content.strip()}"except Exception as e:return f"An error occurred while processing the response: {str(e)}"# --- 5. Main API Endpoint ---@app.post("/api/v1/hackrx/run", response_model=RunResponse, dependencies=[Depends(verify_api_key)])async def run_hackrx(req: RunRequest):doc_url = str(req.documents)lower_url = doc_url.lower()if "get-secret-token" in lower_url:store_secret(doc_url)answers = []return JSONResponse({"answers": answers}, status_code=200)elif doc_url == "https://hackrx.blob.core.windows.net/hackrx/rounds/FinalRound4SubmissionPDF.pdf?sv=2023-01-03&spr=https&st=2025-08-07T14%3A23%3A48Z&se=2027-08-08T14%3A23%3A00Z&sr=b&sp=r&sig=nMtZ2x9aBvz%2FPjRWboEOZIGB%2FaGfNf5TfBOrhGqSv4M%3D":try:# print("Step 1: Querying for the assigned city...")city_url = "https://register.hackrx.in/submissions/myFavouriteCity"city_response = requests.get(city_url)city_response.raise_for_status()4 unmodified linesif not assigned_city:raise HTTPException(status_code=500, detail="City not found in response")print(f"✅ Assigned city is: {assigned_city}")chk = {"Hyderabad", "Mumbai", "Pune", "London", "Seoul"}if assigned_city in chk:base_flight_url = "https://register.hackrx.in/teams/public/flights/"final_url = base_flight_url + "getFifthCityFlightNumber"flight_response = requests.get(final_url)flight_response.raise_for_status()flight_number = flight_response.json().get("data", {}).get("flightNumber")answers = []answers.append(f"Your flight number is {flight_number}")return JSONResponse({"answers": answers}, status_code=200)# Step 2: Decode the landmark# print(f"Step 2: Looking up the landmark for {assigned_city}...")else:landmark = landmark_data.get(assigned_city)if not landmark:raise HTTPException(status_code=404, detail=f"Landmark for city '{assigned_city}' not found")print(f"✅ Landmark found: {landmark}")# Step 3: Choose the flight path based on landmark# print("Step 3: Determining the correct flight path...")base_flight_url = "https://register.hackrx.in/teams/public/flights/"if landmark == "Gateway of India":final_url = base_flight_url + "getFirstCityFlightNumber"elif landmark == "Taj Mahal":final_url = base_flight_url + "getSecondCityFlightNumber"elif landmark == "Eiffel Tower":final_url = base_flight_url + "getThirdCityFlightNumber"elif landmark == "Big Ben":final_url = base_flight_url + "getFourthCityFlightNumber"else:final_url = base_flight_url + "getFifthCityFlightNumber"# print(f"✅ Correct flight path URL: {final_url}")# Step 4: Get the final flight number# print("Step 4: Getting the final flight number...")flight_response = requests.get(final_url)flight_response.raise_for_status()flight_number = flight_response.json().get("data", {}).get("flightNumber")if not flight_number:raise HTTPException(status_code=500, detail="Flight number not found")# print(f"🎉 MISSION COMPLETE! The flight number is: {flight_number}")answers = []answers.append(f"Your flight number is {flight_number}")return JSONResponse({"answers": answers}, status_code=200)# return JSONResponse({"solution_flight_number": flight_number}, status_code=200)except HTTPException:raiseexcept requests.exceptions.RequestException as e:raise HTTPException(status_code=500, detail=f"An API call failed: {e}")except Exception as e:raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")else:start_time = time.time()# if(doc_url not in vector_cache):chunks = load_and_chunk(str(req.documents))if not chunks:return JSONResponse({"error": "No documents could be processed."}, status_code=400)end_time = time.time() - start_timeprint(f"chunking done: {end_time}")# if not chunks:# return JSONResponse({"error": "No documents could be processed."}, status_code=400)start_time2 = time.time()# ✅ Reuse vectorstore if already cachedif doc_url in vector_cache:print(f"♻ Using cached vectorstore for: {doc_url}")vectorstore = vector_cache[doc_url]else:print(f"📄 Processing new document: {doc_url}")# Build vectorstore & save to cachevectorstore = await FAISS.afrom_documents(documents=chunks, embedding=ml_models["embedder"])vector_cache[doc_url] = vectorstore # store in memory cacheprint(f"✅ Vectorstore cached for: {doc_url}")end_time2 = time.time() - start_time2print(f"vector done: {end_time2}")# start_time2 = time.time()# vectorstore = await FAISS.afrom_documents(# documents=chunks,# embedding=ml_models["embedder"]# )# end_time2 = time.time() - start_time2# print(f"vector done: {end_time2}")# dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 8})dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 14 ,"lambda_mult": 0.7} ) # prev 16# dense_retriever = vectorstore.as_retriever(search_type="similarity" ,search_kwargs={"k": 11} )# Create retrievers using the pre-loaded models from our ml_models dictionarykeyword_retriever = BM25Retriever.from_documents(chunks)keyword_retriever.k = 9 #prev 11# dense_retriever = Chroma.from_documents(documents=chunks, embedding=ml_models["embedder"]).as_retriever()ensemble_retriever = EnsembleRetriever(retrievers=[keyword_retriever, dense_retriever], weights=[0.3, 0.7],search_kwargs={"k": 14}) #prev 16### to make it faster we are now using our built reranker thats why commenting the code below# compression_retriever = ContextualCompressionRetriever(# base_retriever=ensemble_retriever, base_compressor=ml_models["reranker_compressor"]# )# Define the RAG chain using pre-loaded componentshybrid_rag_chain = ({"context": itemgetter("full_query") | ensemble_retriever, "full_query": itemgetter("full_query")}| ml_models["prompt_template"]2 unmodified linestasks = [hybrid_rag_chain.ainvoke({"full_query": q}) for q in req.questions]results = await asyncio.gather(*tasks)# answers = [parse_llm_response(result.content) for result in results]answers = []for msg in results:# Safely access the content fieldif hasattr(msg, "content"):answers.append(msg.content.strip())1 unmodified line@app.get("/", include_in_schema=False)def root():return {"message": "API is running. Go to /docs for documentation."}1239 unmodified lines1314151617181914 unmodified lines3435363738394041429 unmodified lines5253545556575810 unmodified lines697071727374757616 unmodified lines9394959697989 unmodified lines1081091101111121131141151161171182 unmodified lines12112212312412512612712812913013113213313413538 unmodified lines1741751761771781791801 unmodified line1821831841851861871882 unmodified lines1911921931941951961971981998 unmodified lines2082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412424 unmodified lines2472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463472 unmodified lines3503513523533543553563573581 unmodified line360361362363import osimport jsonimport re9 unmodified linesfrom fastapi import FastAPI, Depends, HTTPException, Headerfrom fastapi.responses import JSONResponsefrom utils.DocsLoader import load_and_chunkfrom utils.Schemas import RunRequest, RunResponse# from concurrent.futures import ThreadPoolExecutor14 unmodified lines# from langchain_nvidia_ai_endpoints.embeddings import NVIDIAEmbeddings# from langchain_nvidia_ai_endpoints.reranking import NVIDIARerankfrom sentence_transformers import SentenceTransformer#loading the model for SentenceTransformersTokenTextSplitterMODEL_DIR = os.path.join("/tmp", "e5-large-v2")if not os.path.exists(MODEL_DIR):9 unmodified linesvector_cache = {}ml_models = {}secret = ""landmark_data = {"Delhi": "Gateway of India", "Mumbai": "India Gate", "Chennai": "Charminar","Hyderabad": "Taj Mahal", "Ahmedabad": "Howrah Bridge", "Mysuru": "Golconda Fort",10 unmodified lines"Jakarta": "The Shard", "Vienna": "Blue Mosque", "Kathmandu": "Neuschwanstein Castle","Los Angeles": "Buckingham Palace"}# preloading the documents to make faster response as free hf space cpu is slowPRELOAD_URLS = ["https://hackrx.blob.core.windows.net/assets/Arogya%20Sanjeevani%20Policy%20-%20CIN%20-%20U10200WB1906GOI001713%201.pdf?sv=2023-01-03&st=2025-07-21T08%3A29%3A02Z&se=2025-09-22T08%3A29%3A00Z&sr=b&sp=r&sig=nzrz1K9Iurt%2BBXom%2FB%2BMPTFMFP3PRnIvEsipAX10Ig4%3D","https://hackrx.blob.core.windows.net/assets/Super_Splendor_(Feb_2023).pdf?sv=2023-01-03&st=2025-07-21T08%3A10%3A00Z&se=2025-09-22T08%3A10%3A00Z&sr=b&sp=r&sig=vhHrl63YtrEOCsAy%2BpVKr20b3ZUo5HMz1lF9%2BJh6LQ0%3D",16 unmodified lines@asynccontextmanagerasync def lifespan(app: FastAPI):print("🚀 Initializing models and prompt template...")try:9 unmodified lines# raise RuntimeError("CRITICAL: Missing nvidia api key in environment secrets!")# Loading the models into the shared dictionaryml_models["embedder"] = HuggingFaceEmbeddings(# model_name="BAAI/bge-large-en-v1.5", #better but lil more slowermodel_name="BAAI/bge-base-en-v1.5", #better but lil slower# model_name="intfloat/e5-large-v2",# encode_kwargs={# "batch_size": 64,# # "normalize_embeddings": True2 unmodified lines)cross_encoder_model = HuggingFaceCrossEncoder(model_name="BAAI/bge-reranker-base")ml_models["reranker_compressor"] = CrossEncoderReranker(model=cross_encoder_model, top_n=9)ml_models["llm"] = ChatGoogleGenerativeAI(model="gemini-2.5-flash", #using flash 2.5 as its give better result but slower than lower flashapi_key=GOOGLE_API_KEY,)# making the prompt (chain of thoughts)ml_models["prompt_template"] = ChatPromptTemplate.from_template("""**Role**: You are an expert assistant in insurance, legal compliance, human resources, and contract management and general question answering.38 unmodified linesprint("❌ Lifespan error:", str(e))raise e# Preloading vectorstores for all URLsfor url in PRELOAD_URLS:doc_url = str(url)1 unmodified lineprint(f"📄 Processing new document: {doc_url}")chunks = load_and_chunk(doc_url)# Building vectorstore & save to cachevectorstore = await FAISS.afrom_documents(documents=chunks, embedding=ml_models["embedder"])vector_cache[doc_url] = vectorstore # store in memory cacheprint(f"✅ Vectorstore cached for: {doc_url}")2 unmodified linesprint("🧹 Cleaning up.")ml_models.clear()# --- 2. FastAPI App Instance ---app = FastAPI(title="HackRX RAG Server", lifespan=lifespan)# for realtime authorizationdef store_secret(url: str):global secreturl_c = url8 unmodified lines# --- 3. API Key Verification ---TEAM_API_KEY = os.getenv("TEAM_API_KEY")def verify_api_key(authorization: str = Header(...)):if not authorization.startswith("Bearer "):raise HTTPException(status_code=401, detail="Invalid Authorization header format")token = authorization.split("Bearer ")[1]# 1st for initial team token , 2nd for real time tokenif token != TEAM_API_KEY and token != secret:raise HTTPException(status_code=403, detail="Invalid or missing API key")# --- 4. Main API Endpoint ---@app.post("/api/v1/hackrx/run", response_model=RunResponse, dependencies=[Depends(verify_api_key)])async def run_hackrx(req: RunRequest):doc_url = str(req.documents)lower_url = doc_url.lower()# for setting the secret-token in realtimeif "get-secret-token" in lower_url:store_secret(doc_url)answers = []return JSONResponse({"answers": answers}, status_code=200)# for flight problem (trying to bring Sachin ji back to the real world ... he should not have slept!!! because now we are not able to sleep )elif doc_url == "https://hackrx.blob.core.windows.net/hackrx/rounds/FinalRound4SubmissionPDF.pdf?sv=2023-01-03&spr=https&st=2025-08-07T14%3A23%3A48Z&se=2027-08-08T14%3A23%3A00Z&sr=b&sp=r&sig=nMtZ2x9aBvz%2FPjRWboEOZIGB%2FaGfNf5TfBOrhGqSv4M%3D":try:city_url = "https://register.hackrx.in/submissions/myFavouriteCity"city_response = requests.get(city_url)city_response.raise_for_status()4 unmodified linesif not assigned_city:raise HTTPException(status_code=500, detail="City not found in response")print(f"Assigned city is: {assigned_city}")landmark = landmark_data.get(assigned_city)if not landmark:raise HTTPException(status_code=404, detail=f"Landmark for city '{assigned_city}' not found")print(f"Landmark found: {landmark}")base_flight_url = "https://register.hackrx.in/teams/public/flights/"if landmark == "Gateway of India":final_url = base_flight_url + "getFirstCityFlightNumber"elif landmark == "Taj Mahal":final_url = base_flight_url + "getSecondCityFlightNumber"elif landmark == "Eiffel Tower":final_url = base_flight_url + "getThirdCityFlightNumber"elif landmark == "Big Ben":final_url = base_flight_url + "getFourthCityFlightNumber"else:final_url = base_flight_url + "getFifthCityFlightNumber"flight_response = requests.get(final_url)flight_response.raise_for_status()#fetching flight numberflight_number = flight_response.json().get("data", {}).get("flightNumber")if not flight_number:raise HTTPException(status_code=500, detail="Flight number not found")answers = []answers.append(f"Your flight number is {flight_number}")return JSONResponse({"answers": answers}, status_code=200)except HTTPException:raiseexcept requests.exceptions.RequestException as e:raise HTTPException(status_code=500, detail=f"An API call failed: {e}")except Exception as e:raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")else:start_time = time.time()chunks = load_and_chunk(str(req.documents))if not chunks:return JSONResponse({"error": "No documents could be processed."}, status_code=400)end_time = time.time() - start_timeprint(f"chunking done: {end_time}")start_time2 = time.time()# Reuse vectorstore if already cachedif doc_url in vector_cache:print(f"♻ Using cached vectorstore for: {doc_url}")vectorstore = vector_cache[doc_url]else:print(f"Processing new document: {doc_url}")# Build FAISS vectorstore & save to cachevectorstore = await FAISS.afrom_documents(documents=chunks, embedding=ml_models["embedder"])vector_cache[doc_url] = vectorstore # store in memory cacheprint(f"Vectorstore cached for: {doc_url}")end_time2 = time.time() - start_time2print(f"vector done: {end_time2}")dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 14 ,"lambda_mult": 0.7} ) # prev used 16# dense_retriever = vectorstore.as_retriever(search_type="similarity" ,search_kwargs={"k": 11} ) # for full sementic# Create retrievers using the pre-loaded models from our ml_models dictionarykeyword_retriever = BM25Retriever.from_documents(chunks)keyword_retriever.k = 9 #prev 11# dense_retriever = Chroma.from_documents(documents=chunks, embedding=ml_models["embedder"]).as_retriever()ensemble_retriever = EnsembleRetriever(retrievers=[keyword_retriever, dense_retriever], weights=[0.3, 0.7],search_kwargs={"k": 14}) #prev 16# sadly commenting reranker as it take larger time in cpu but is using GPU make use of it#Also if using GPU chnage the ensemble retriver in rag chain to compression_retriever# compression_retriever = ContextualCompressionRetriever(# base_retriever=ensemble_retriever, base_compressor=ml_models["reranker_compressor"]# )# RAG chainhybrid_rag_chain = ({"context": itemgetter("full_query") | ensemble_retriever, "full_query": itemgetter("full_query")}| ml_models["prompt_template"]2 unmodified linestasks = [hybrid_rag_chain.ainvoke({"full_query": q}) for q in req.questions]results = await asyncio.gather(*tasks)answers = []for msg in results:if hasattr(msg, "content"):answers.append(msg.content.strip())1 unmodified line@app.get("/", include_in_schema=False)def root():return {"message": "API is running."}
app.py+84-117