|
--- |
|
license: mit |
|
task_categories: |
|
- feature-extraction |
|
language: |
|
- fr |
|
- en |
|
--- |
|
|
|
This dataset comes from a request on the HAL API (the French national open archive) limited to the UNIV-COTEDAZUR portal instance. |
|
The request collects the bibliographic records of the SHS articles with abstract published between 2013 and 2023 |
|
|
|
The parameters passed in the url request are : |
|
|
|
https://api.archives-ouvertes.fr/search/UNIV-COTEDAZUR/?q=docType_s:ART&fq=abstract_s:[%22%22%20TO%20*]&fq=domain_s:*shs*&fq=submittedDateY_i:[2020%20TO%202023]&fl=doiId_s,uri_s,title_s,subTitle_s,authFullName_s,producedDate_s,journalTitle_s,journalPublisher_s,abstract_s,domain_s,openAccess_bool |
|
|
|
The embeddings column stores the embeddings of the "combined" column values converted in vectors with the sentence-transformers/all-MiniLM-L6-v2 embeddinsg model. |
|
|
|
## Metadata extraction |
|
|
|
``` |
|
url = ""https://api.archives-ouvertes.fr/search/UNIV-COTEDAZUR/?q=docType_s:ART&fq=abstract_s:[%22%22%20TO%20*]&fq=domain_s:*shs*&fq=publicationDateY_i:[2013%20TO%202023]&fl=doiId_s,uri_s,title_s,subTitle_s,authFullName_s,producedDate_s,journalTitle_s,journalPublisher_s,abstract_s,domain_s,openAccess_bool" |
|
|
|
# Get the total number of records |
|
url_for_total_count = f"{url}&wt=json&rows=0" |
|
response = requests.request("GET", url_for_total_count).text |
|
data = json.loads(response) |
|
total_count = data["response"]["numFound"] |
|
print(total_cout) |
|
# return 3601 |
|
|
|
# Loop over the records and get metadata |
|
step = 500 |
|
appended_data = [] |
|
for i in range(1, int(total_count), int(step)): |
|
url = f"{url}&rows={step}&start={i}&wt=csv" |
|
df = pd.read_csv(url, encoding="utf-8") |
|
appended_data.append(df) |
|
appended_data = pd.concat(appended_data) |
|
|
|
# dedup |
|
appended_data = appended_data.drop_duplicates(subset=['uri_s']) |
|
|
|
appended_data.shape |
|
# returns 2405 |
|
``` |
|
|
|
## Add embeddings (CPU) |
|
|
|
### Solution 1 : with HF Inference API |
|
|
|
``` |
|
import requests |
|
import json |
|
from typing import Optional, List, Dict, Any |
|
|
|
HF_TOKEN = "<hf_token>" |
|
model_id = "sentence-transformers/all-MiniLM-L6-v2" |
|
|
|
embeddings_api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_id}" |
|
headers = {"Authorization": f"Bearer {HF_TOKEN}"} |
|
|
|
def embeddings_query(text:str) -> List: |
|
response = requests.post(embeddings_api_url, headers=headers, json={"inputs": text, "options":{"wait_for_model":True}}) |
|
return response.json() |
|
|
|
df = appended_data.replace(np.nan, '') |
|
df['embeddings'] = df.combined.apply(lambda x:embeddings_query(x.strip())) |
|
``` |
|
|
|
### Solution 2 : with sentence-transformers library |
|
|
|
``` |
|
from sentence_transformers import SentenceTransformer |
|
|
|
model_id = "sentence-transformers/all-MiniLM-L6-v2" |
|
embedder = SentenceTransformer(model_id) |
|
|
|
def embeddings_query(text:str) -> List: |
|
return embedder.encode(text,convert_to_tensor=True) |
|
|
|
df['embeddings'] = df.combined.apply(lambda x:embeddings_query(x.strip().to_list())) |
|
|
|
``` |