What is Zshot?
Zshot is a highly customizable framework designed for performing Zero and Few-Shot Named Entity Recognition (NER) and Relationship Extraction (RE). With Zshot, you can address a variety of tasks, including:
- Mentions Extraction: Identify global or domain-specific mentions.
- Wikification: Link textual mentions to Wikipedia entities.
- Zero and Few-Shot NER: Generalize NER to unseen domains using language descriptions.
- Zero and Few-Shot Relationship Recognition: Extract relationships between entities.
- Visualization: Visualize NER and RE extractions.
Requirements
- Python 3.6+
- SpaCy for pipelining and visualization.
- PyTorch to run models.
- Transformers for pre-trained language models.
- Evaluate for evaluation functionality.
- Datasets for dataset evaluations.
Installation
To install Zshot, open your command line and type:
$ pip install zshot
How to Use Zshot
Let’s break down the usage of Zshot with an analogy. Imagine you’re a librarian and you want to classify books into genres based on their content. Just like how you could identify whether a book is a thriller or a romance without having read every book, Zshot can recognize unnamed entities and relationships based solely on descriptions or keywords.
Step-by-Step Procedure
- Install the requirements by running
pip install -r requirements.txt. - Install a SpaCy pipeline for mentions extraction by executing
python -m spacy download en_core_web_sm. - Create a
main.pyfile and configure your pipeline, specifying the entities and their descriptions. - Run the script using
python main.py.
Sample Code
import spacy
from zshot import PipelineConfig, displacy
from zshot.linker import LinkerRegen
from zshot.mentions_extractor import MentionsExtractorSpacy
from zshot.utils.data_models import Entity
nlp = spacy.load('en_core_web_sm')
nlp_config = PipelineConfig(
mentions_extractor=MentionsExtractorSpacy(),
linker=LinkerRegen(),
entities=[
Entity(name="Paris", description="Paris is the capital of France."),
Entity(name="IBM", description="IBM is an American multinational technology corporation.")
]
)
nlp.add_pipe(zshot, config=nlp_config, last=True)
text = "IBM is based in Armonk, New York."
doc = nlp(text)
displacy.serve(doc, style='ent')
This code will analyze the text to classify entities and visualize the annotations. Just like sorting the right books to the correct shelves, Zshot helps classify your text.
Troubleshooting
- If you encounter issues with installations, double-check your Python version and ensure all necessary dependencies are installed.
- Ensure the SpaCy model is correctly downloaded and matches the one specified in your configuration.
- If the visualization does not appear, ensure that the server is running, and you can access the URL http://127.0.0.1:5000.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
How to Create a Custom Component
To implement your own mentions extractor or linker, extend the base classes provided in Zshot. You’ll need to implement the predict method to extract desired mentions from the provided text.
Evaluation
To evaluate the performance of your models, Zshot provides the functionalities to assess using predefined datasets such as OntoNotes. This is akin to reviewing your library’s performance after sorting books to ensure they are correctly placed. Implement evaluation using:
from zshot.evaluation import evaluate
evaluation = evaluate(nlp, dataset)
print(evaluation)
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

