|
| 1 | +# Copyright 2022 The Forte Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import sys |
| 15 | +from importlib_metadata import csv |
| 16 | +from termcolor import colored |
| 17 | + |
| 18 | +from forte import Pipeline |
| 19 | +from forte.data.readers import ClassificationDatasetReader |
| 20 | +from fortex.nltk import NLTKSentenceSegmenter |
| 21 | +from fortex.huggingface import ZeroShotClassifier |
| 22 | +from ft.onto.base_ontology import Sentence |
| 23 | + |
| 24 | + |
| 25 | +csv_path = "data_samples/banking77/sample.csv" |
| 26 | +pl = Pipeline() |
| 27 | +# initialize labels |
| 28 | +class_names = [ |
| 29 | + "activate_my_card", |
| 30 | + "age_limit", |
| 31 | + "apple_pay_or_google_pay", |
| 32 | + "atm_support", |
| 33 | + "automatic_top_up", |
| 34 | + "balance_not_updated_after_bank_transfer", |
| 35 | + "balance_not_updated_after_cheque_or_cash_deposit", |
| 36 | + "beneficiary_not_allowed", |
| 37 | + "cancel_transfer", |
| 38 | + "card_about_to_expire", |
| 39 | + "card_acceptance", |
| 40 | + "card_arrival", |
| 41 | + "card_delivery_estimate", |
| 42 | + "card_linking", |
| 43 | + "card_not_working", |
| 44 | + "card_payment_fee_charged", |
| 45 | + "card_payment_not_recognised", |
| 46 | + "card_payment_wrong_exchange_rate", |
| 47 | + "card_swallowed", |
| 48 | + "cash_withdrawal_charge", |
| 49 | + "cash_withdrawal_not_recognised", |
| 50 | + "change_pin", |
| 51 | + "compromised_card", |
| 52 | + "contactless_not_working", |
| 53 | + "country_support", |
| 54 | + "declined_card_payment", |
| 55 | + "declined_cash_withdrawal", |
| 56 | + "declined_transfer", |
| 57 | + "direct_debit_payment_not_recognised", |
| 58 | + "disposable_card_limits", |
| 59 | + "edit_personal_details", |
| 60 | + "exchange_charge", |
| 61 | + "exchange_rate", |
| 62 | + "exchange_via_app", |
| 63 | + "extra_charge_on_statement", |
| 64 | + "failed_transfer", |
| 65 | + "fiat_currency_support", |
| 66 | + "get_disposable_virtual_card", |
| 67 | + "get_physical_card", |
| 68 | + "getting_spare_card", |
| 69 | + "getting_virtual_card", |
| 70 | + "lost_or_stolen_card", |
| 71 | + "lost_or_stolen_phone", |
| 72 | + "order_physical_card", |
| 73 | + "passcode_forgotten", |
| 74 | + "pending_card_payment", |
| 75 | + "pending_cash_withdrawal", |
| 76 | + "pending_top_up", |
| 77 | + "pending_transfer", |
| 78 | + "pin_blocked", |
| 79 | + "receiving_money", |
| 80 | + "Refund_not_showing_up", |
| 81 | + "request_refund", |
| 82 | + "reverted_card_payment?", |
| 83 | + "supported_cards_and_currencies", |
| 84 | + "terminate_account", |
| 85 | + "top_up_by_bank_transfer_charge", |
| 86 | + "top_up_by_card_charge", |
| 87 | + "top_up_by_cash_or_cheque", |
| 88 | + "top_up_failed", |
| 89 | + "top_up_limits", |
| 90 | + "top_up_reverted", |
| 91 | + "topping_up_by_card", |
| 92 | + "transaction_charged_twice", |
| 93 | + "transfer_fee_charged", |
| 94 | + "transfer_into_account", |
| 95 | + "transfer_not_received_by_recipient", |
| 96 | + "transfer_timing", |
| 97 | + "unable_to_verify_identity", |
| 98 | + "verify_my_identity", |
| 99 | + "verify_source_of_funds", |
| 100 | + "verify_top_up", |
| 101 | + "virtual_card_not_working", |
| 102 | + "visa_or_mastercard", |
| 103 | + "why_verify_identity", |
| 104 | + "wrong_amount_of_cash_received", |
| 105 | + "wrong_exchange_rate_for_cash_withdrawal", |
| 106 | +] |
| 107 | +index2class = dict(enumerate(class_names)) |
| 108 | + |
| 109 | +# initialize reader config |
| 110 | +this_reader_config = { |
| 111 | + "forte_data_fields": [ |
| 112 | + "ft.onto.base_ontology.Body", |
| 113 | + "label", |
| 114 | + ], |
| 115 | + "index2class": index2class, |
| 116 | + "text_fields": [ |
| 117 | + "ft.onto.base_ontology.Body" |
| 118 | + ], |
| 119 | + "digit_label": False, |
| 120 | + "one_based_index_label": False, |
| 121 | +} |
| 122 | + |
| 123 | +pl.set_reader(ClassificationDatasetReader(), config=this_reader_config) |
| 124 | +pl.add(NLTKSentenceSegmenter()) |
| 125 | +pl.add(ZeroShotClassifier(), config={"candidate_labels": class_names}) |
| 126 | +pl.initialize() |
| 127 | + |
| 128 | +for pack in pl.process_dataset(csv_path): |
| 129 | + for sentence in pack.get(Sentence): |
| 130 | + if ( |
| 131 | + input("Type n for the next sentence and its prediction: ").lower() |
| 132 | + == "n" |
| 133 | + ): |
| 134 | + sent_text = sentence.text |
| 135 | + print(colored("Sentence:", "red"), sent_text, "\n") |
| 136 | + print(colored("Prediction:", "blue"), sentence.classification) |
| 137 | + else: |
| 138 | + print("Exit the program due to unrecognized input") |
| 139 | + sys.exit() |
0 commit comments