Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -18,6 +18,7 @@ from huggingface_hub import hf_hub_download
|
|
18 |
from contextual import ContextualAI
|
19 |
from openai import AzureOpenAI
|
20 |
from datetime import datetime
|
|
|
21 |
|
22 |
"""
|
23 |
# to switch:
|
@@ -328,6 +329,7 @@ def run_retrieval(query):
|
|
328 |
"""
|
329 |
start_time = time.time()
|
330 |
query_embeddings = run_dense_retrieval(query)
|
|
|
331 |
print("--- Nvidia Embedding: %s seconds ---" % (time.time() - start_time))
|
332 |
D, I = faiss_index.search(query_embeddings, 45)
|
333 |
print("--- Faiss retrieval: %s seconds ---" % (time.time() - start_time))
|
@@ -362,7 +364,7 @@ def run_retrieval(query):
|
|
362 |
out_dict.append(tmp)
|
363 |
print (out_dict)
|
364 |
# and now, rerank
|
365 |
-
out_dict = rerank_with_contextual_AI(out_dict)
|
366 |
return out_dict
|
367 |
|
368 |
|
@@ -376,10 +378,20 @@ device = torch.device("cuda")
|
|
376 |
extractive_qa = pipeline("question-answering", model="ai-law-society-lab/extractive-qa-model", tokenizer="FacebookAI/roberta-large", device_map="auto", token=os.getenv('hf_token'))
|
377 |
ids, titles, chunks = load_NJ_caselaw()
|
378 |
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
383 |
|
384 |
with open("NJ_caselaw_metadata.json") as f:
|
385 |
metadata = json.load(f)
|
@@ -389,6 +401,7 @@ with open("NJ_caselaw_metadata.json") as f:
|
|
389 |
def load_embeddings_model(model_name = "intfloat/e5-large-v2"):
|
390 |
if "NV" in model_name:
|
391 |
model = AutoModel.from_pretrained('nvidia/NV-Embed-v2', trust_remote_code=True, torch_dtype=torch.bfloat16, device_map="auto")
|
|
|
392 |
model.eval()
|
393 |
return model
|
394 |
|
|
|
18 |
from contextual import ContextualAI
|
19 |
from openai import AzureOpenAI
|
20 |
from datetime import datetime
|
21 |
+
import sys
|
22 |
|
23 |
"""
|
24 |
# to switch:
|
|
|
329 |
"""
|
330 |
start_time = time.time()
|
331 |
query_embeddings = run_dense_retrieval(query)
|
332 |
+
np.save("test_query_embeddings", query_embeddings)
|
333 |
print("--- Nvidia Embedding: %s seconds ---" % (time.time() - start_time))
|
334 |
D, I = faiss_index.search(query_embeddings, 45)
|
335 |
print("--- Faiss retrieval: %s seconds ---" % (time.time() - start_time))
|
|
|
364 |
out_dict.append(tmp)
|
365 |
print (out_dict)
|
366 |
# and now, rerank
|
367 |
+
#out_dict = rerank_with_contextual_AI(out_dict)
|
368 |
return out_dict
|
369 |
|
370 |
|
|
|
378 |
extractive_qa = pipeline("question-answering", model="ai-law-society-lab/extractive-qa-model", tokenizer="FacebookAI/roberta-large", device_map="auto", token=os.getenv('hf_token'))
|
379 |
ids, titles, chunks = load_NJ_caselaw()
|
380 |
|
381 |
+
#@profile
|
382 |
+
def profiling_faiss_index():
|
383 |
+
ds = load_dataset("ai-law-society-lab/NJ_embeddings", token=os.getenv('hf_token'))["train"]
|
384 |
+
print (sys.getsizeof(ds))
|
385 |
+
ds = ds.with_format("np")
|
386 |
+
print (sys.getsizeof(ds))
|
387 |
+
print (ds)
|
388 |
+
#faiss_index = load_faiss_index(ds["embeddings"])
|
389 |
+
ds.add_faiss_index(column='embeddings')
|
390 |
+
#print (sys.getsizeof(faiss_index))
|
391 |
+
|
392 |
+
return ds
|
393 |
+
faiss_index = profiling_faiss_index()
|
394 |
+
|
395 |
|
396 |
with open("NJ_caselaw_metadata.json") as f:
|
397 |
metadata = json.load(f)
|
|
|
401 |
def load_embeddings_model(model_name = "intfloat/e5-large-v2"):
|
402 |
if "NV" in model_name:
|
403 |
model = AutoModel.from_pretrained('nvidia/NV-Embed-v2', trust_remote_code=True, torch_dtype=torch.bfloat16, device_map="auto")
|
404 |
+
#model = AutoModel.from_pretrained('nvidia/NV-Embed-v2', trust_remote_code=True, torch_dtype=torch.float16, device_map="auto")
|
405 |
model.eval()
|
406 |
return model
|
407 |
|