ricitos2001 commited on
Commit
c8f49e3
·
verified ·
1 Parent(s): 0712107

Update embedding.py

Browse files
Files changed (1) hide show
  1. embedding.py +48 -38
embedding.py CHANGED
@@ -1,38 +1,48 @@
1
- import os
2
- import pandas
3
- import dotenv
4
- import requests
5
-
6
- dotenv.load_dotenv(dotenv.find_dotenv())
7
- HF_TOKEN = os.environ['YOUR_TOKEN']
8
-
9
- def query(api_url, headers, texts):
10
- response = requests.post(api_url, headers=headers, json={"inputs": texts, "options":{"wait_for_model":True}})
11
- return response.json()
12
-
13
- def main():
14
- model_id = "sentence-transformers/all-MiniLM-L6-v2"
15
- api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_id}"
16
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
17
- texts = ["How do I get a replacement Medicare card?",
18
- "What is the monthly premium for Medicare Part B?",
19
- "How do I terminate my Medicare Part B (medical insurance)?",
20
- "How do I sign up for Medicare?",
21
- "Can I sign up for Medicare Part B if I am working and have health insurance through an employer?",
22
- "How do I sign up for Medicare Part B if I already have Part A?",
23
- "What are Medicare late enrollment penalties?",
24
- "What is Medicare and who can get it?",
25
- "How can I get help with my Medicare Part A and Part B premiums?",
26
- "What are the different parts of Medicare?",
27
- "Will my Medicare premiums be higher because of my higher income?",
28
- "What is TRICARE ?",
29
- "Should I sign up for Medicare Part B if I have Veterans' Benefits?"]
30
-
31
- output = query(api_url, headers, texts)
32
-
33
- embeddings = pandas.DataFrame(output)
34
- embeddings.to_csv("embeddings.csv", index=False)
35
-
36
- if __name__ == "__main__":
37
- main()
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()