<jupyter_start><jupyter_text>Aspect term extraction and sentiment classification via [PyABSA](https://github.com/yangheng95/PyABSA)More usages see [here](https://github.com/yangheng95/PyABSA/tree/release/demos/aspect_term_extraction)<jupyter_code>!pip install pyabsa<jupyter_output>Collecting pyabsa
  Downloading pyabsa-1.10.1-py3-none-any.whl (207 kB)
[K     |████████████████████████████████| 207 kB 4.3 MB/s 
[?25hCollecting pytorch-warmup
  Downloading pytorch-warmup-0.1.0.tar.gz (314 kB)
[K     |████████████████████████████████| 314 kB 50.7 MB/s 
[?25hCollecting findfile>=1.7.3
  Downloading findfile-1.7.7-py3-none-any.whl (4.7 kB)
Requirement already satisfied: termcolor in /usr/local/lib/python3.7/dist-packages (from pyabsa) (1.1.0)
Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from pyabsa) (4.64.0)
Collecting sentencepiece
  Downloading sentencepiece-0.1.96-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB)
[K     |████████████████████████████████| 1.2 MB 56.7 MB/s 
[?25hCollecting seqeval
  Downloading seqeval-1.2.2.tar.gz (43 kB)
[K     |████████████████████████████████| 43 kB 2.0 MB/s 
[?25hCollecting autocuda>=0.9
  Downloading autocuda-0.11-py3-none-any.whl (4.8 kB)
Collecting gitpython
  Downloa[...]<jupyter_text>Find Available Checkpoints For **Current Version**<jupyter_code>from pyabsa import available_checkpoints
checkpoint_map = available_checkpoints(from_local=False)<jupyter_output>This script could only be used to manage NVIDIA GPUs,but no GPU found in your device
Remote ABSADataset version: 2022.03.22 Local ABSADatasets version: N.A.
Unknown local version for ABSADatasets, please check the latest version of ABSADatasets at https://github.com/yangheng95/ABSADatasets<jupyter_text>Init a aspect extractor from online checkpoint or local checkpoint<jupyter_code>from pyabsa import ABSADatasetList, available_checkpoints
from pyabsa import ATEPCCheckpointManager

aspect_extractor = ATEPCCheckpointManager.get_aspect_extractor(checkpoint='english2',
                                   auto_device=True  # False means load model on CPU
                                   )<jupyter_output>This script could only be used to manage NVIDIA GPUs,but no GPU found in your device
Remote ABSADataset version: 2022.03.22 Local ABSADatasets version: N.A.
Unknown local version for ABSADatasets, please check the latest version of ABSADatasets at https://github.com/yangheng95/ABSADatasets<jupyter_text>Aspect Term Extract & Sentiment Inference<jupyter_code># You can inference from a list of setences or a DatasetItem from PyABSA 
examples = ['But the staff was so perfect to us, but the service was bad .',
            ]
inference_source = examples
atepc_result = aspect_extractor.extract_aspect(inference_source=inference_source,  #
                          save_result=True,
                          print_result=True,  # print the result
                          pred_sentiment=True,  # Predict the sentiment of extracted aspect terms
                          )<jupyter_output>/usr/local/lib/python3.7/dist-packages/catalogue.py:138: DeprecationWarning: SelectableGroups dict interface is deprecated. Use select.
  for entry_point in AVAILABLE_ENTRY_POINTS.get(self.entry_point_namespace, []):
/usr/local/lib/python3.7/dist-packages/transformers/models/deberta_v2/modeling_deberta_v2.py:538: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  bucket_pos = np.where(abs_pos <= mid, relative_pos, log_pos * sign).astype(np.int)
/usr/local/lib/python3.7/dist-packages/pyabsa/core/atepc/prediction/aspect_extractor.py:351: UserWarning: Cr[...]<jupyter_text>Batch Inference<jupyter_code>inference_source = ABSADatasetList.SemEval
atepc_result = aspect_extractor.extract_aspect(inference_source=inference_source,  #
                          save_result=True,
                          print_result=True,  # print the result
                          pred_sentiment=True,  # Predict the sentiment of extracted aspect terms
                          )<jupyter_output>Loading Laptop14 dataset from:  https://github.com/yangheng95/ABSADatasets
Clone ABSADatasets from https://github.com/yangheng95/ABSADatasets.git
FindFile Warning: multiple targets ['integrated_datasets/apc_datasets/110.SemEval/113.laptop14', 'integrated_datasets/apc_datasets/110.SemEval/111.arts_laptop14'] found but return the shortest path
loading: integrated_datasets/apc_datasets/110.SemEval/113.laptop14/Laptops_Test_Gold.xml.seg.inference
Loading Restaurant14 dataset from:  https://github.com/yangheng95/ABSADatasets
FindFile Warning: multiple targets ['integrated_datasets/apc_datasets/110.SemEval/112.arts_restaurant14', 'integrated_datasets/apc_datasets/110.SemEval/114.restaurant14'] found but return the shortest path
loading: integrated_datasets/apc_datasets/110.SemEval/114.restaurant14/Restaurants_Test_Gold.xml.seg.inference
Loading Restaurant16 dataset from:  https://github.com/yangheng95/ABSADatasets
loading: integrated_datasets/apc_datasets/110.SemEval/116.restaurant16/restaur[...]<jupyter_text>Train a model (FAST_LCF_ATEPC) for Aspect-based sentiment classification  We dont run a training on Colab as it is too slow<jupyter_code>import os

import findfile

from pyabsa.functional import ATEPCModelList
from pyabsa.functional import Trainer, ATEPCTrainer
from pyabsa.functional import ABSADatasetList
from pyabsa.functional import ATEPCConfigManager
from pyabsa.functional.dataset import DatasetItem

atepc_config = ATEPCConfigManager.get_atepc_config_english()

atepc_config.pretrained_bert = 'microsoft/deberta-v3-base'
atepc_config.model = ATEPCModelList.FAST_LCF_ATEPC
dataset_path = ABSADatasetList.Restaurant14
# or your local dataset: dataset_path = 'your local dataset path'

aspect_extractor = ATEPCTrainer(config=atepc_config,
                  dataset=dataset_path,
                  from_checkpoint='',  # set checkpoint to train on the checkpoint.
                  checkpoint_save_mode=1,
                  auto_device=True
                  ).load_trained_model()<jupyter_output><empty_output><jupyter_text>Train on your own dataset from a checkpoint<jupyter_code>config = ATEPCConfigManager.get_atepc_config_english()

checkpoint_path = ATEPCCheckpointManager.get_checkpoint(checkpoint='english2')  # or
# checkpoint_path = 'lcfs_atepc_cdw_apcacc_86.17_apcf1_58.3_atef1_70.86'

config.model = ATEPCModelList.LCFS_ATEPC
config.evaluate_begin = 4
config.num_epoch = 5

TShirt = ABSADatasetList.TShirt
aspect_extractor = Trainer(config=config,
              dataset=TShirt,
              from_checkpoint=checkpoint_path,
              checkpoint_save_mode=1,
              auto_device=True
              ).load_trained_model()<jupyter_output><empty_output><jupyter_text>Deployment Example<jupyter_code># -*- coding: utf-8 -*-
# file: deploy_demo.py
# time: 2021/10/10
# author: yangheng <<EMAIL>>
# github: https://github.com/yangheng95
# Copyright (C) 2021. All Rights Reserved.

import gradio as gr
import pandas as pd

from pyabsa import ATEPCCheckpointManager

aspect_extractor = ATEPCCheckpointManager.get_aspect_extractor(checkpoint='multilingual2',
                                                               auto_device=True  # False means load model on CPU
                                                               )


def inference(text):
    result = aspect_extractor.extract_aspect(inference_source=[text],
                                             pred_sentiment=True)

    result = pd.DataFrame({
        'aspect': result[0]['aspect'],
        'sentiment': result[0]['sentiment']
    })

    return result


if __name__ == '__main__':
    iface = gr.Interface(
        fn=inference,
        inputs=["text"],
        examples=[['The wine list is incredible and extensive and diverse , the food is all incredible and the staff was all very nice ,'
                   ' good at their jobs and cultured .'],
                  ['Though the menu includes some unorthodox offerings (a peanut butter roll, for instance), the classics are pure and '
                   'great--we have never had better sushi anywhere, including Japan.'],
                  ['Everything, from the soft bread, soggy salad, and 50 minute wait time, with an incredibly rude service to deliver'
                   ' below average food .'],
                  ['Even though it is running Snow Leopard, 2.4 GHz C2D is a bit of an antiquated CPU and thus the occasional spinning '
                   'wheel would appear when running Office Mac applications such as Word or Excel .'],
                  ['This demo is trained on the laptop and restaurant and other review datasets from ABSADatasets (https://github.com/yangheng95/ABSADatasets)'],
                  ['To fit on your data, please train the model on your own data, see the PyABSA (https://github.com/yangheng95/PyABSA)'],
                  ],
        outputs="dataframe",
        title='Aspect Term Extraction for Short Texts (powered by PyABSA)'
    )

    iface.launch()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09701885599915727, 'fraction_numerical': 0.028547350679448014, 'mean_word_length': 3.5998062015503876, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 32, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '175435', 'license': 'MIT', 'n_tokens_mistral': 3237, 'n_tokens_neox': 2923, 'n_words': 723, 'path': 'demos/aspect_term_extraction/ATEPC-USAGES.ipynb', 'repo_name': 'yangheng95/LCF-ABSA', 'size': '9508'}