Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,12 +17,40 @@ from bm25 import BM25
|
|
| 17 |
from vector_store import Vectorstore
|
| 18 |
from retriever import Retriever
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
embedder = Embedder()
|
| 23 |
sessions: dict = {}
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
MODELS = [
|
| 28 |
("Qwen/Qwen2.5-72B-Instruct"),
|
|
@@ -86,12 +114,13 @@ async def upload_file(file: UploadFile = File(...)):
|
|
| 86 |
|
| 87 |
extracted_text = await asyncio.to_thread(Loader(tmp_path).load)
|
| 88 |
chunks = await asyncio.to_thread(Chunker(extracted_text).chunk)
|
|
|
|
| 89 |
embedded_chunks = await asyncio.to_thread(embedder.embed, chunks)
|
| 90 |
|
| 91 |
-
vector_store = Vectorstore(embedder)
|
| 92 |
await asyncio.to_thread(vector_store.add_vectors, embedded_chunks)
|
| 93 |
|
| 94 |
-
bm25 = BM25()
|
| 95 |
await asyncio.to_thread(bm25.add, chunks)
|
| 96 |
|
| 97 |
session_id = str(uuid.uuid4())
|
|
@@ -99,6 +128,7 @@ async def upload_file(file: UploadFile = File(...)):
|
|
| 99 |
"store": vector_store,
|
| 100 |
"bm25": bm25,
|
| 101 |
"expires_at": datetime.now() + timedelta(hours=24),
|
|
|
|
| 102 |
}
|
| 103 |
|
| 104 |
return {"message": "PDF indexed successfully!", "session_id": session_id}
|
|
@@ -124,7 +154,7 @@ async def chat(chat_req: ChatRequest):
|
|
| 124 |
|
| 125 |
bm25 = session["bm25"]
|
| 126 |
vector_store = session["store"]
|
| 127 |
-
retriever = Retriever(vector_store=vector_store, bm25=bm25)
|
| 128 |
|
| 129 |
context_chunks = await asyncio.to_thread(retriever.retrieve, chat_req.message)
|
| 130 |
|
|
|
|
| 17 |
from vector_store import Vectorstore
|
| 18 |
from retriever import Retriever
|
| 19 |
|
| 20 |
+
class K_selecter:
|
| 21 |
+
def __init__(self, documents, min_final=2, max_final=20):
|
| 22 |
+
self.documents = documents
|
| 23 |
+
self.min_final = min_final
|
| 24 |
+
self.max_final = max_final
|
| 25 |
+
self.total_chunks = len(documents)
|
| 26 |
+
|
| 27 |
+
def initial_k(self) -> int: # for the sematic and lexical search
|
| 28 |
+
if self.total_chunks <= 20:
|
| 29 |
+
base = max(5, int(self.total_chunks * 0.5))
|
| 30 |
+
elif self.total_chunks <= 50:
|
| 31 |
+
base = max(8, int(self.total_chunks * 0.35))
|
| 32 |
+
elif self.total_chunks <= 100:
|
| 33 |
+
base = max(12, int(self.total_chunks * 0.25))
|
| 34 |
+
else:
|
| 35 |
+
base = max(15, int(self.total_chunks * 0.15))
|
| 36 |
+
return min(base, self.total_chunks)
|
| 37 |
+
|
| 38 |
+
def final_k(self) -> int: # for the final rrf
|
| 39 |
+
if self.total_chunks <= 20:
|
| 40 |
+
base = max(self.min_final, int(self.total_chunks * 0.3))
|
| 41 |
+
elif self.total_chunks <= 50:
|
| 42 |
+
base = max(self.min_final, int(self.total_chunks * 0.15))
|
| 43 |
+
elif self.total_chunks <= 100:
|
| 44 |
+
base = max(self.min_final, int(self.total_chunks * 0.1))
|
| 45 |
+
else:
|
| 46 |
+
base = max(self.min_final, int(self.total_chunks * 0.06))
|
| 47 |
+
final = max(self.min_final, min(self.max_final, base))
|
| 48 |
+
return min(final, self.total_chunks)
|
| 49 |
|
| 50 |
embedder = Embedder()
|
| 51 |
sessions: dict = {}
|
| 52 |
|
| 53 |
+
|
| 54 |
|
| 55 |
MODELS = [
|
| 56 |
("Qwen/Qwen2.5-72B-Instruct"),
|
|
|
|
| 114 |
|
| 115 |
extracted_text = await asyncio.to_thread(Loader(tmp_path).load)
|
| 116 |
chunks = await asyncio.to_thread(Chunker(extracted_text).chunk)
|
| 117 |
+
kselect = K_selecter(chunks)
|
| 118 |
embedded_chunks = await asyncio.to_thread(embedder.embed, chunks)
|
| 119 |
|
| 120 |
+
vector_store = Vectorstore(embedder,top_k=kselect.initial_k())
|
| 121 |
await asyncio.to_thread(vector_store.add_vectors, embedded_chunks)
|
| 122 |
|
| 123 |
+
bm25 = BM25(top_k=kselect.initial_k())
|
| 124 |
await asyncio.to_thread(bm25.add, chunks)
|
| 125 |
|
| 126 |
session_id = str(uuid.uuid4())
|
|
|
|
| 128 |
"store": vector_store,
|
| 129 |
"bm25": bm25,
|
| 130 |
"expires_at": datetime.now() + timedelta(hours=24),
|
| 131 |
+
"final_k": kselect.final_k()
|
| 132 |
}
|
| 133 |
|
| 134 |
return {"message": "PDF indexed successfully!", "session_id": session_id}
|
|
|
|
| 154 |
|
| 155 |
bm25 = session["bm25"]
|
| 156 |
vector_store = session["store"]
|
| 157 |
+
retriever = Retriever(vector_store=vector_store, bm25=bm25, top_k=session["final_k"])
|
| 158 |
|
| 159 |
context_chunks = await asyncio.to_thread(retriever.retrieve, chat_req.message)
|
| 160 |
|