Skip to main content

A library for classifying texts into one or more of the 17 SDGs using a pretrained transformer-based model or keyword extraction method, with an option to combine both approaches.

Project description

SDGDetector

License

CyVer

SDGDetector is an open-source python library, that aims to classify texts with the Sustainable Development Goals (SDGs). This library either uses a pretrained fine-tuned model to classifiy the given texts to the SDG or implements the method of keywords extraction to associate them with the SDG or combine the two aforementioned methods.

  1. The first method takes as input a list of texts and a fine-tuned XLNet or RoBERTa model and returns the probabilities of the given texts to be associated with the SDG. The training/testing f1-score of the models is 0.90. XLNet is better than RoBERTa.
  2. The second method takes as input a list of texts and find the most relevant keywords and computes the cosine similarity with the keywords of the SDG. As keywords of the SDG, we use the keywords based on the methodology explained here and we add as keywords for the SDG17 the keywords which can be found here.
  3. The third option of this libary is a combination of the two above methods. The formula is
CyVer

The methodology used for this library is available in SustaiNLP Gitlab repository

Features

  • SDG_classifier_using_model: Classifies the given text with the SDGs by using the fine-tuned XLNet or RoBERTa models.
  • SDG_classifier_using_keywords_extraction: Classifies the given text with the SDGs by using keyword extraction and sentence embeddings generated by one of the models: all-mpnet-base-v2,distilbert-base-nli-mean-tokens, all-MiniLM-L6-v2. Representative keywords are identified through the MMR algorithm and compared to SDG keywords using cosine similarity.
  • SDG_classifier: Classifies the given text with the SDGs by combining the aforementioned methods.

⚙️ Installation

To install the latest stable version from PyPI, run:

pip install SDGDetector

Alternatively, if you prefer to install the latest development version, you can install it directly from GitLab:

pip install git+https://gitlab.com/netmode/sdg-detector.git

Or, you can manually clone the repository and run:

git clone https://gitlab.com/netmode/sdg-detector.git
cd sdg-detector
pip install .

📦 Prerequisites

Before installing, make sure you have the following Python packages installed, with Python version >= 3.11:

'numpy>=1.26.4',  
'nltk>=3.9.1',  
'sentence-transformers>=3.4.1', 
'sentencepiece>=0.2.0', 
'keras_preprocessing>=1.1.2'

📖Documentation

For documentation read Wiki.

💻 Example Usage

In the file Test.ipynb there are examples for the 3 different classes of this library.

The fine-tuned pretrained XLNet and RoBERTa models, which are used in the first class SDG_classifier_using_model can be found here under the folder 'Data/Classification Task-Transfer Learning'. The training/testing f1-score of the models is 0.90. XLNet is better than RoBERTa. In addition, this library can be used with the user's fine-tuned model. The requirements for a different fine-tuned model is:

  • It should be 'BERT' or 'XLNet' model
  • It should be saved using the python code torch.save(model.state_dict(), model_name) and implemented using Pytorch.

Importing the Library

import os
from SDGDetector import SDGDetector

text = ['Europe has always been the home of industry. For centuries, it has been a pioneer in industrial innovation and has helped \
    improve the way people around the world produce, consume and do business. Based on a strong internal market, the European industry \
    has long powered our economy, providing a stable living for millions and creating the social hubs around which our communities are built.']

SDG_classifier_using_model

model = SDGDetector.SDG_classifier_using_model(model_name='XLNet',model_path=<your path of downloaded model>)

# apply classifier on example input text
sdg,sdg_names,probs = model.predict(text, return_probs=True)

SDG_classifier_using_keywords_extraction

🔑: Some Sentence-Transformers models require authentication to access, especially models hosted on Hugging Face. To use these models, you need to create and set up a Hugging Face token.

os.environ['HF_TOKEN'] = <token>
hf_token = os.getenv('HF_TOKEN')
if hf_token:
    print(f"Hugging Face token is set: {hf_token}")
else:
    print("Hugging Face token is not set.")
mpnet = SDGDetector.SDG_classifier_using_keywords_extraction(top_keywords=5,diversity=0.3,n_gram_range=(1,2),model_name='all-mpnet-base-v2')

keywords_mpnet = mpnet.find_top_keywords(text)

sdg,sdg_name,cosine_similarity,cosine_matrix = mpnet.predict(text,return_cs_matrix_and_avg_cs=True)

SDG_classifier

combo = SDGDetector.SDG_classifier(pretrained_model_name='XLNet',pretrained_model_path=<your path of downloaded model>
                                sentence_model_name='all-mpnet-base-v2',top_keywords=10,diversity=0.3,n_gram_range=(1,2))

sdg,sdg_name,association = combo.predict(text,return_association=True)

🤝 Contributing

Contributions are welcome! Please submit a pull request or open an issue.

📚 Cite

To cite this work, please use:

Knowledge Graph Data Enrichment based on a Software Library for Text Mapping to the Sustainable Development Goals Ioanna Mandilara, Eleni Fotopoulou, Christina Maria Androna, Anastasios Zafeiropoulos, Symeon Papavassiliou

Zenodo repository: DOI

📬 Contact

For any request for detailed information or expression of interest for participating at this initiative, you may contact:

  • 📧 Ioanna Mandilara - ioannamand (at) netmode (dot) ntua (dot) gr
  • 📧 Christina Maria Androna - andronaxm (at) netmode (dot) ntua (dot) gr
  • 📧 Eleni Fotopoulou - efotopoulou (at) netmode (dot) ntua (dot) gr
  • 📧 Anastasios Zafeiropoulos - tzafeir (at) cn (dot) ntua (dot) gr

📑License

This project is licensed under the CC BY-NC 4.0 license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sdgdetector-1.3.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sdgdetector-1.3-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file sdgdetector-1.3.tar.gz.

File metadata

  • Download URL: sdgdetector-1.3.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for sdgdetector-1.3.tar.gz
Algorithm Hash digest
SHA256 8ec3aec2f1ab3eb0378272f6f543d1502a62a0a74ae5349991595929ca20d116
MD5 ca31e4453ded4f5bed87e018b815c5d8
BLAKE2b-256 2e30eaa583e4247b0dfa22c8f866cf97a665eae5379138c06e2bd86050d6823c

See more details on using hashes here.

File details

Details for the file sdgdetector-1.3-py3-none-any.whl.

File metadata

  • Download URL: sdgdetector-1.3-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for sdgdetector-1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2ec2214f0fa11dd40829cf36baf41f4e67d1e433e2a4bc66fd44ab94d1a5c589
MD5 91765da5b9d12789e4e09471fb10085d
BLAKE2b-256 5f2958f0cbec2af4382be7a9860e9baf53f20954d80002f994a5e7b44251001a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page