This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_intent_with_ner_initialization(): | |
entity: CustomEntity = CustomEntity('city_entity', [CustomEntityEntry('Barcelona', ['BCN']), CustomEntityEntry('Madrid')]) | |
intent: Intent = Intent('intent_name', ['what is the weather like in mycity', 'forecast for mycity', 'is it sunny?'], [EntityReference('city', 'mycity', entity)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Entity: | |
"""An entity to be recognized as part of the matching process""" | |
def __init__(self, name: str): | |
self.name: str = name | |
class CustomEntityEntry: | |
"""Each one of the entries (and its synonyms) a CustomEntity consists of""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.post("/bot/{name}/train/") | |
def bot_train(name: str, configurationdto: ConfigurationDTO): | |
if name not in bots.keys(): | |
raise HTTPException(status_code=422, detail="Bot does not exist") | |
bot: Bot = bots[name] | |
if len(bot.contexts) == 0: | |
raise HTTPException(status_code=422, detail="Bot is empty, nothing to train") | |
bot.configuration = configurationdto_to_configuration(configurationdto) | |
train(bot) | |
return {"status:" : "successful training for " + str(len(bot.contexts)) + " contexts"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def predict(context: NLUContext, sentence: str, configuration: NlpConfiguration) -> numpy.ndarray: | |
prediction: numpy.ndarray | |
sentences = [preprocess_prediction_sentence(sentence, configuration)] | |
sequences = context.tokenizer.texts_to_sequences(sentences) | |
if configuration.discard_oov_sentences and all(i==1 for i in sequences[0]): | |
# the sentence to predict consists of only out of focabulary tokens so we can automatically assign a zero probability to all classes | |
prediction = numpy.zeros(len(context.intents)) | |
else: | |
padded = tf.keras.preprocessing.sequence.pad_sequences(sequences, padding='post', maxlen=configuration.input_max_num_tokens, truncating='post') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def train(bot: Bot): | |
for context in bot.contexts: | |
__train_context(context, bot.configuration) | |
def __train_context(context: NLUContext, configuration: NlpConfiguration): | |
tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=configuration.num_words, lower=configuration.lower, oov_token=configuration.oov_token) | |
total_training_sentences: list[str] = [] | |
total_labels_training_sentences: list[int] = [] | |
for intent in context.intents: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Intent: | |
def __init__(self, name: str, training_sentences: list[str]): | |
self.name: str = name | |
self.training_sentences: list[str] = training_sentences | |
class NLUContext: | |
def __init__(self, name: str): | |
self.name: str = name | |
self.intents: list[Intent] = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model: tf.keras.models = tf.keras.Sequential([ | |
tf.keras.layers.Embedding(input_dim=configuration.num_words, output_dim=configuration.embedding_dim, input_length=configuration.input_max_num_tokens), | |
tf.keras.layers.GlobalAveragePooling1D(), | |
tf.keras.layers.Dense(24, activation='relu'), | |
tf.keras.layers.Dense(24, activation='relu'), | |
tf.keras.layers.Dense(len(context.intents), activation='sigmoid') | |
]) | |
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(), optimizer='adam', metrics=['accuracy']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="xatkit-chat"></div> | |
<script> | |
xatkit.renderXatkitWidget({ | |
server: "https://yourboturl.com/chat-handler", | |
widget: { | |
title: "Xatkit Demonstration", | |
subtitle: "Add a bot to your Carrd site", | |
startMinimized: true, | |
}, | |
storage: { autoClear: true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link rel="stylesheet" href="https://dev.xatkit.com/static/xatkit.min.css"> | |
<script src="https://dev.xatkit.com/static/xatkit.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/ask_QA') | |
def ask_QA(): | |
query = request.args.get('question') | |
print("Question received: ", query) | |
# Connect to Elasticsearch (locally installed) | |
document_store = ElasticsearchDocumentStore(host="localhost", username="", password="", index="blogposts") | |
retriever = ElasticsearchRetriever(document_store=document_store) | |
reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2", use_gpu=True) |
NewerOlder