Update embedding.py
Browse files- embedding.py +48 -38
embedding.py
CHANGED
|
@@ -1,38 +1,48 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
api_url =
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
"What
|
| 24 |
-
"
|
| 25 |
-
"How
|
| 26 |
-
"
|
| 27 |
-
"
|
| 28 |
-
"What
|
| 29 |
-
"
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import numpy
|
| 4 |
+
import pandas
|
| 5 |
+
import dotenv
|
| 6 |
+
import requests
|
| 7 |
+
from datasets import load_dataset
|
| 8 |
+
from sentence_transformers.util import semantic_search
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
dotenv.load_dotenv(dotenv.find_dotenv())
|
| 12 |
+
HF_TOKEN = os.environ['YOUR_TOKEN']
|
| 13 |
+
|
| 14 |
+
def query(api_url, headers, texts):
|
| 15 |
+
response = requests.post(api_url, headers=headers, json={"inputs": texts, "options":{"wait_for_model":True}})
|
| 16 |
+
return response.json()
|
| 17 |
+
|
| 18 |
+
def main():
|
| 19 |
+
model_id = "sentence-transformers/all-MiniLM-L6-v2"
|
| 20 |
+
api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_id}"
|
| 21 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 22 |
+
texts = ["How do I get a replacement Medicare card?",
|
| 23 |
+
"What is the monthly premium for Medicare Part B?",
|
| 24 |
+
"How do I terminate my Medicare Part B (medical insurance)?",
|
| 25 |
+
"How do I sign up for Medicare?",
|
| 26 |
+
"Can I sign up for Medicare Part B if I am working and have health insurance through an employer?",
|
| 27 |
+
"How do I sign up for Medicare Part B if I already have Part A?",
|
| 28 |
+
"What are Medicare late enrollment penalties?",
|
| 29 |
+
"What is Medicare and who can get it?",
|
| 30 |
+
"How can I get help with my Medicare Part A and Part B premiums?",
|
| 31 |
+
"What are the different parts of Medicare?",
|
| 32 |
+
"Will my Medicare premiums be higher because of my higher income?",
|
| 33 |
+
"What is TRICARE ?",
|
| 34 |
+
"Should I sign up for Medicare Part B if I have Veterans' Benefits?"]
|
| 35 |
+
output = query(api_url, headers, texts)
|
| 36 |
+
embeddings = pandas.DataFrame(output)
|
| 37 |
+
embeddings.to_csv("embeddings.csv", index=False)
|
| 38 |
+
|
| 39 |
+
faqs_embeddings = load_dataset('ricitos2001/OMEGAI')
|
| 40 |
+
dataset_embeddings = torch.from_numpy(faqs_embeddings["train"].to_pandas().to_numpy()).to(torch.float)
|
| 41 |
+
question = ["How can Medicare help me?"]
|
| 42 |
+
output = query(api_url, headers, question)
|
| 43 |
+
query_embeddings = torch.FloatTensor(output)
|
| 44 |
+
hits = semantic_search(query_embeddings, dataset_embeddings, top_k=5)
|
| 45 |
+
print([texts[hits[0][i]['corpus_id']] for i in range(len(hits[0]))])
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
main()
|