Skip to main content

Hezar: A seamless AI framework & library for Persian

Project description

A seamless multi-task AI library for Persian

Hezar (meaning thousand in Persian) is a multipurpose AI library built to make AI easy for the Persian community!

Hezar is a library that:

  • brings together all the best works in AI for Persian
  • makes using AI models as easy as a couple of lines of code
  • seamlessly integrates with Hugging Face Hub for all of its models
  • has a highly developer-friendly interface
  • has a task-based model interface which is more convenient for general users.
  • is packed with additional tools like word embeddings, tokenizers, feature extractors, etc.
  • comes with a lot of supplementary ML tools for deployment, benchmarking, optimization, etc.
  • and more!

Installation

Hezar is available on PyPI and can be installed with pip:

pip install hezar

You can also install the latest version from the source. Clone the repo and execute the following commands:

git clone https://github.com/hezarai/hezar.git
pip install ./hezar

Quick Tour

Ready-to-use models from Hub

There's a bunch of ready-to-use trained models for different tasks on the Hub. See them here!

Text classification (sentiment analysis)

from hezar import Model

example = ["هزار، کتابخانه‌ای کامل برای به کارگیری آسان هوش مصنوعی"]
model = Model.load("hezarai/bert-fa-sentiment-digikala-snappfood")
outputs = model.predict(example)
print(outputs)
{'labels': ['positive'], 'probs': [0.812910258769989]}

Sequence labeling (part-of-speech tagging)

from hezar import Model

hub_path = "hezarai/distilbert-fa-pos-lscp-500k"
model = Model.load(hub_path)
inputs = ["سلام بر فارسی زبانان شریف"]
outputs = model.predict(inputs)
print(outputs)
[[{'token': 'سلام', 'tag': 'N'}, {'token': 'بر', 'tag': 'P'}, {'token': 'فارسی', 'tag': 'Ne'}, {'token': 'زبانان', 'tag': 'Ne'}, {'token': 'شریف', 'tag': 'AJ'}]]

Sequence labeling (named entity recognition)

from hezar import Model

hub_path = "hezarai/bert-fa-ner-arman"
model = Model.load(hub_path)
inputs = ["شرکت هوش مصنوعی هزار برترین در نوع خود"]
outputs = model.predict(inputs)
print(outputs)
[[{'token': 'شرکت', 'tag': 'B-org'}, {'token': 'هوش', 'tag': 'I-org'}, {'token': 'مصنوعی', 'tag': 'I-org'}, {'token': 'هزار', 'tag': 'O'}, {'token': 'برترین', 'tag': 'O'}, {'token': 'در', 'tag': 'O'}, {'token': 'نوع', 'tag': 'O'}, {'token': 'خود', 'tag': 'O'}]]

Write your own model

It's fairly easy to extend this library or add your own model. Hezar has its own Model base class that is simply a normal PyTorch nn.Module but with some extra features!

Here's a simple example:

from dataclasses import dataclass

from torch import Tensor, nn

from hezar import Model, ModelConfig


@dataclass
class PerceptronConfig(ModelConfig):
    name: str = "perceptron"
    input_shape: int = 4
    output_shape: int = 2


class Perceptron(Model):
    """
    A simple single layer network
    """

    def __init__(self, config, **kwargs):
        super().__init__(config, **kwargs)
        self.nn = nn.Linear(
            in_features=self.config.input_shape,
            out_features=self.config.output_shape,
        )

    def forward(self, inputs: list, **kwargs):
        inputs = Tensor(inputs).reshape(1, -1)
        x = self.nn(inputs)
        return x

    def post_process(self, inputs, **kwargs):
        # post-process forward outputs (optional method)
        return inputs.numpy()  # convert torch tensor to numpy array


model = Perceptron(PerceptronConfig())
inputs = [1, 2, 3, 4]
outputs = model.predict(inputs)
print(outputs)
[[-0.13248837  0.7039478 ]]

As you can see, defining a new model is just like a typical PyTorch module, but comes with some amazing functionalities out-of-the-box like pushing to the Hub!

hub_repo = "<your_hf_username>/my-awesome-perceptron"
model.push_to_hub(hub_repo)
INFO: Uploaded:`PerceptronConfig(name=preceptron)` --> `your_hf_username/my-awesome-perceptron/model_config.yaml`
INFO: Uploaded: `Perceptron(name=preceptron)` --> `your_hf_username/my-awesome-perceptron/model.pt`

Documentation

Refer to the docs for a full documentation.

Contribution

This is a really heavy project to be maintained by a couple of developers. The idea isn't novel at all but actually doing it is really difficult hence being the only one in the whole history of the Persian open source! So any contribution is appreciated ❤️

MIT License

Copyright (c) 2022 Hezar AI

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

hezar-0.21.2.tar.gz (48.5 kB view details)

Uploaded Source

Built Distribution

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

hezar-0.21.2-py3-none-any.whl (84.5 kB view details)

Uploaded Python 3

File details

Details for the file hezar-0.21.2.tar.gz.

File metadata

  • Download URL: hezar-0.21.2.tar.gz
  • Upload date:
  • Size: 48.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.8.17 Linux/5.15.0-1042-azure

File hashes

Hashes for hezar-0.21.2.tar.gz
Algorithm Hash digest
SHA256 77205d604bbddae34b30c1781e7bb10601f7d6d595e5fc6fc5687cda8527aff2
MD5 45bf7da97a493eca37b6479b36a991c4
BLAKE2b-256 804753e6ae6c8e2b4af0512f269f6084672afac1ac7a69535e2cb8c4d52770de

See more details on using hashes here.

File details

Details for the file hezar-0.21.2-py3-none-any.whl.

File metadata

  • Download URL: hezar-0.21.2-py3-none-any.whl
  • Upload date:
  • Size: 84.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.8.17 Linux/5.15.0-1042-azure

File hashes

Hashes for hezar-0.21.2-py3-none-any.whl
Algorithm Hash digest
SHA256 168e65c08dab596fbf0434448992c1458be327820f75f679d7aeb5bd43a75806
MD5 e92e2d063892e1d106cb2beecea30ede
BLAKE2b-256 63e14eea72fd1b5456216c31396f3aaa23b498c80d691f7e0e4e90a88541a9bf

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