Skip to content

Instantly share code, notes, and snippets.

View jcabot's full-sized avatar

Jordi Cabot jcabot

View GitHub Profile
@jcabot
jcabot / dsl_ner_example.py
Created May 9, 2022 10:51
creating an intent with a custom ner
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)])
@jcabot
jcabot / dsl_custom_ner.py
Created May 9, 2022 10:44
Extension of chatbot dsl representation with custom ners
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"""
@jcabot
jcabot / fastapi.py
Created March 23, 2022 21:44
Wrapping of the intent classifier in a REST Api
@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"}
@jcabot
jcabot / intent_prediction.py
Created March 23, 2022 15:24
Chatbot intent prediction code
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')
@jcabot
jcabot / chatbot_data_processing.py
Created March 23, 2022 15:04
Chatbot Data Processing
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:
@jcabot
jcabot / chatbotdsl.py
Last active March 23, 2022 15:10
(Partial) Chatbot DSL
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] = []
@jcabot
jcabot / keras_xatkit_nlu.py
Last active March 23, 2022 15:06
Keras model for Xatkit's NLU network
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'])
@jcabot
jcabot / xatkit_install_render.html
Created January 3, 2022 11:03
Xatkit website install - widget rendering
<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 }
@jcabot
jcabot / xatkit_install_imports.html
Created January 3, 2022 11:02
Xatkit website install - Head
<link rel="stylesheet" href="https://dev.xatkit.com/static/xatkit.min.css">
<script src="https://dev.xatkit.com/static/xatkit.min.js"></script>
@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)