Upload 2 files
Browse files- embedding.py +52 -0
- embeddings.csv +1 -0
embedding.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import pandas
|
| 4 |
+
import dotenv
|
| 5 |
+
import requests
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
+
from sentence_transformers.util import semantic_search
|
| 8 |
+
|
| 9 |
+
dotenv.load_dotenv(dotenv.find_dotenv())
|
| 10 |
+
HF_TOKEN = os.environ['YOUR_TOKEN']
|
| 11 |
+
YOUR_API_KEY = os.environ['YOUR_API_KEY']
|
| 12 |
+
REQUEST_SUCESSFULL = 200
|
| 13 |
+
|
| 14 |
+
def obtener_temas():
|
| 15 |
+
headers = {"subdomain-X": "demotest", 'X-Subdomain': 'demotest', "Authorization": f"Bearer {YOUR_API_KEY}"}
|
| 16 |
+
respuesta = requests.request(method="GET", url="https://api.applearnify.es/api/subjects-with-topics", headers=headers)
|
| 17 |
+
temas = []
|
| 18 |
+
if respuesta.status_code == REQUEST_SUCESSFULL:
|
| 19 |
+
for subject in respuesta.json():
|
| 20 |
+
for topic in subject["topics"]:
|
| 21 |
+
for file in topic.get("files", []):
|
| 22 |
+
if file["type"] == "temario":
|
| 23 |
+
temas.append(file.get("friendly_name"))
|
| 24 |
+
return temas
|
| 25 |
+
|
| 26 |
+
def query(api_url, headers, texts):
|
| 27 |
+
response = requests.post(api_url, headers=headers, json={"inputs": texts, "options":{"wait_for_model":True}})
|
| 28 |
+
if response.status_code == REQUEST_SUCESSFULL:
|
| 29 |
+
return response.json()
|
| 30 |
+
else:
|
| 31 |
+
raise Exception(response.json())
|
| 32 |
+
|
| 33 |
+
def main():
|
| 34 |
+
model_id = ""
|
| 35 |
+
api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_id}"
|
| 36 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 37 |
+
texts = obtener_temas()
|
| 38 |
+
output = query(api_url, headers, texts)
|
| 39 |
+
embeddings = pandas.DataFrame(output)
|
| 40 |
+
embeddings.to_csv("embeddings.csv", index=False)
|
| 41 |
+
faqs_embeddings = load_dataset('ricitos2001/ecope-dataset')
|
| 42 |
+
dataset_embeddings = torch.from_numpy(faqs_embeddings["train"].to_pandas().to_numpy()).to(torch.float)
|
| 43 |
+
question = []
|
| 44 |
+
text = input("pon algo: ")
|
| 45 |
+
question.append(text)
|
| 46 |
+
output = query(api_url, headers, question)
|
| 47 |
+
query_embeddings = torch.FloatTensor(output)
|
| 48 |
+
hits = semantic_search(query_embeddings, dataset_embeddings, top_k=5)
|
| 49 |
+
print([texts[hits[0][i]['corpus_id']] for i in range(len(hits[0]))])
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
main()
|
embeddings.csv
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
|