Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import re
|
|
|
|
| 5 |
from contextlib import asynccontextmanager
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from operator import itemgetter
|
|
@@ -45,7 +46,8 @@ async def lifespan(app: FastAPI):
|
|
| 45 |
|
| 46 |
# Load models into the shared dictionary
|
| 47 |
ml_models["embedder"] = HuggingFaceEmbeddings(model_name="BAAI/bge-base-en-v1.5")
|
| 48 |
-
cross_encoder_model = HuggingFaceCrossEncoder(model_name="BAAI/bge-reranker-
|
|
|
|
| 49 |
ml_models["reranker_compressor"] = CrossEncoderReranker(model=cross_encoder_model, top_n=5)
|
| 50 |
ml_models["llm"] = ChatGoogleGenerativeAI(model="gemini-1.5-flash", api_key=GOOGLE_API_KEY)
|
| 51 |
ml_models["prompt_template"] = PromptTemplate.from_template(
|
|
@@ -159,14 +161,19 @@ async def run_hackrx(req: RunRequest):
|
|
| 159 |
| ml_models["llm"]
|
| 160 |
)
|
| 161 |
|
| 162 |
-
answers = []
|
| 163 |
-
for q in req.questions:
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
return JSONResponse({"answers": answers}, status_code=200)
|
| 172 |
|
|
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import re
|
| 5 |
+
import asyncio
|
| 6 |
from contextlib import asynccontextmanager
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from operator import itemgetter
|
|
|
|
| 46 |
|
| 47 |
# Load models into the shared dictionary
|
| 48 |
ml_models["embedder"] = HuggingFaceEmbeddings(model_name="BAAI/bge-base-en-v1.5")
|
| 49 |
+
cross_encoder_model = HuggingFaceCrossEncoder(model_name="BAAI/bge-reranker-base")
|
| 50 |
+
# cross_encoder_model = HuggingFaceCrossEncoder(model_name="BAAI/bge-reranker-large")
|
| 51 |
ml_models["reranker_compressor"] = CrossEncoderReranker(model=cross_encoder_model, top_n=5)
|
| 52 |
ml_models["llm"] = ChatGoogleGenerativeAI(model="gemini-1.5-flash", api_key=GOOGLE_API_KEY)
|
| 53 |
ml_models["prompt_template"] = PromptTemplate.from_template(
|
|
|
|
| 161 |
| ml_models["llm"]
|
| 162 |
)
|
| 163 |
|
| 164 |
+
# answers = []
|
| 165 |
+
# for q in req.questions:
|
| 166 |
+
# try:
|
| 167 |
+
# result = await hybrid_rag_chain.ainvoke({"full_query": q})
|
| 168 |
+
# parsed = parse_llm_response(result.content)
|
| 169 |
+
# answers.append(parsed)
|
| 170 |
+
# except Exception as e:
|
| 171 |
+
# return JSONResponse({"error": str(e)}, status_code=500)
|
| 172 |
+
tasks = [hybrid_rag_chain.ainvoke({"full_query": q}) for q in req.questions]
|
| 173 |
+
results = await asyncio.gather(*tasks)
|
| 174 |
+
|
| 175 |
+
# Extract the content from each result and parse it
|
| 176 |
+
answers = [parse_llm_response(result.content) for result in results]
|
| 177 |
|
| 178 |
return JSONResponse({"answers": answers}, status_code=200)
|
| 179 |
|