Nueva publicación

Encontrar

Pregunta
· 23 jul, 2024

IRIS - basic navigation question

So, here is a novice question; but, I can't seem to figure out how to do it, or find any comments. I simply want to close out this DTL, under the Interoperability / Build / DTL screen:

There do not seem to be any buttons to 'close' the current DTL item. I tried logging off and back on, and, it brings it right back. Ideas?

4 comentarios
Comentarios (4)3
Inicie sesión o regístrese para continuar
Pregunta
· 23 jul, 2024

Issues running Docker container in Hugging Face Space

When I try to run my app as a Docker Container locally it works fine, but I attempted to create a Hugging Face Space to try to deploy my app to and when I run with the Docker SDK selected I run into the following IRIS error report.

Has anyone seen something like this before?

 

Starting Control Process 
Global buffer setting requires attention. Auto-selected 25% of total memory. 
Allocated 105698MB shared memory 95166MB global buffers, 1020MB routine buffers 
This copy of InterSystems IRIS has been licensed for use exclusively by: 
Too many Cores (24) for InterSystems IRIS Community License. 
Copyright (c) 1986-2024 by InterSystems Corporation Any other use is a violation of your license agreement 
Error: Invalid Community Edition license, may have exceeded core limit. - Shutting down the system : $zu(56,2)= 0Starting IRIS
6 comentarios
Comentarios (6)2
Inicie sesión o regístrese para continuar
Artículo
· 23 jul, 2024 Lectura de 8 min

Guia passo a passo para criar um chatbot personalizado utilizando spaCy (livraria Python NLP)

image

Olá Comunidade,

Nesse artigo, demonstrarei os seguintes passos para criar seu próprio chatbot utilizando spaCy (spaCy é uma biblioteca de software de código aberto para o processamento avançado de linguagem natural, escrita nas linguagens de programação Python e Cython):

  • Passo 1: Instalar as livrarias necessárias

  • Passo 2: Criar o arquivo de padrões e respostas

  • Passo 3: Treinar o modelo

  • Passo 4: Criar uma aplicação ChatBot baseada no modelo treinado

Comecemos

Passo 1: Instalar as livrarias necessárias 

Em primeiro lugar, precisamos instalar as livrarias python necessárias, o que conseguimos executando o seguinte comando:

pip3 install spacy nltk
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

Passo 2: Criar o arquivo de padrões e respostas

Necessitamos criar um arquivo intents.json que contenha padrões de perguntas e respostas. A seguir se mostra um exemplo de alguns dos padrões de perguntas e respostas

{
  "intents": [
    {
      "tag": "greeting",
      "patterns": [
        "Hi",
        "Hey",
        "How are you",
        "Is anyone there?",
        "Hello",
        "Good day"
      ],
      "responses": [
        "Hey :-)",
        "Hello, thanks for visiting",
        "Hi there, what can I do for you?",
        "Hi there, how can I help?"
      ]
    },
    {
      "tag": "goodbye",
      "patterns": ["Bye", "See you later", "Goodbye"],
      "responses": [
        "See you later, thanks for visiting",
        "Have a nice day",
        "Bye! Come back again soon."
      ]
    },
    {
      "tag": "thanks",
      "patterns": ["Thanks", "Thank you", "That's helpful", "Thank's a lot!"],
      "responses": ["Happy to help!", "Any time!", "My pleasure"]
    },
    {
      "tag": "items",
      "patterns": [
        "tell me about this app",
        "What kinds of technology used?",
        "What do you have?"
      ],
      "responses": [
        "Write something about the app."
      ]
    },        
    {
      "tag": "funny",
      "patterns": [
        "Tell me a joke!",
        "Tell me something funny!",
        "Do you know a joke?"
      ],
      "responses": [
        "Why did the hipster burn his mouth? He drank the coffee before it was cool.",
        "What did the buffalo say when his son left for college? Bison."
      ]
    }
  ]
}

Passo 3: Treinar o modelo

3.1- Criar um arquivo NeuralNet model.py que se utilizará para treinar o modelo

#model.py file
import torch.nn as nn

class NeuralNet(nn.Module):
    def __init__(self, input_size, hidden_size, num_classes):
        super(NeuralNet, self).__init__()
        self.l1 = nn.Linear(input_size, hidden_size) 
        self.l2 = nn.Linear(hidden_size, hidden_size) 
        self.l3 = nn.Linear(hidden_size, num_classes)
        self.relu = nn.ReLU()
    
    def forward(self, x):
        out = self.l1(x)
        out = self.relu(out)
        out = self.l2(out)
        out = self.relu(out)
        out = self.l3(out)
        # no activation and no softmax at the end
        return out

3.2 - Precisamos do arquivo nltk_utils.py, que utiliza a livraria python «nltk» (uma plataforma utilizada para construir programas Python, que trabalham com dados de linguagem humana, para aplicá-los no processamento estatístico de linguagem natural (PLN)). 

#nltk_utils.py
#nltk library used for building Python programs that work with human language data for applying in 
#statistical natural language processing (NLP)
import numpy as np
import nltk
from nltk.stem.porter import PorterStemmer
stemmer = PorterStemmer()

def tokenize(sentence):
    """
    split sentence into array of words/tokens
    a token can be a word or punctuation character, or number
    """
    return nltk.word_tokenize(sentence)


def stem(word):
    """
    stemming = find the root form of the word
    examples:
    words = ["organize", "organizes", "organizing"]
    words = [stem(w) for w in words]
    -> ["organ", "organ", "organ"]
    """
    return stemmer.stem(word.lower())


def bag_of_words(tokenized_sentence, words):
    """
    return bag of words array:
    1 for each known word that exists in the sentence, 0 otherwise
    example:
    sentence = ["hello", "how", "are", "you"]
    words = ["hi", "hello", "I", "you", "bye", "thank", "cool"]
    bog   = [  0 ,    1 ,    0 ,   1 ,    0 ,    0 ,      0]
    """
    # stem each word
    sentence_words = [stem(word) for word in tokenized_sentence]
    # initialize bag with 0 for each word
    bag = np.zeros(len(words), dtype=np.float32)
    for idx, w in enumerate(words):
        if w in sentence_words: 
            bag[idx] = 1
    return bag

3.3 - Temos que treinar o modelo. Vamos utilizar o arquivo train.py para criá-lo.

#train.py
import numpy as np
import json,torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from nltk_utils import bag_of_words, tokenize, stem
from model import NeuralNet
 
with open('intents.json', 'r') as f:
    intents = json.load(f)

all_words = []
tags = []
xy = []
# loop through each sentence in our intents patterns
for intent in intents['intents']:
    tag = intent['tag']
    # add to tag list
    tags.append(tag)
    for pattern in intent['patterns']:
        # tokenize each word in the sentence
        w = tokenize(pattern)
        # add to our words list
        all_words.extend(w)
        # add to xy pair
        xy.append((w, tag))

# stem and lower each word
ignore_words = ['?', '.', '!']
all_words = [stem(w) for w in all_words if w not in ignore_words]
# remove duplicates and sort
all_words = sorted(set(all_words))
tags = sorted(set(tags))

print(len(xy), "patterns")
print(len(tags), "tags:", tags)
print(len(all_words), "unique stemmed words:", all_words)

# create training data
X_train = []
y_train = []
for (pattern_sentence, tag) in xy:
    # X: bag of words for each pattern_sentence
    bag = bag_of_words(pattern_sentence, all_words)
    X_train.append(bag)
    # y: PyTorch CrossEntropyLoss needs only class labels, not one-hot
    label = tags.index(tag)
    y_train.append(label)

X_train = np.array(X_train)
y_train = np.array(y_train)

# Hyper-parameters 
num_epochs = 1000
batch_size = 8
learning_rate = 0.001
input_size = len(X_train[0])
hidden_size = 8
output_size = len(tags)
print(input_size, output_size)

class ChatDataset(Dataset):
    def __init__(self):
        self.n_samples = len(X_train)
        self.x_data = X_train
        self.y_data = y_train

    # support indexing such that dataset[i] can be used to get i-th sample
    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]

    # we can call len(dataset) to return the size
    def __len__(self):
        return self.n_samples

dataset = ChatDataset()
train_loader = DataLoader(dataset=dataset,
                          batch_size=batch_size,
                          shuffle=True,
                          num_workers=0)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = NeuralNet(input_size, hidden_size, output_size).to(device)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train the model
for epoch in range(num_epochs):
    for (words, labels) in train_loader:
        words = words.to(device)
        labels = labels.to(dtype=torch.long).to(device)        
        # Forward pass
        outputs = model(words)
        # if y would be one-hot, we must apply
        # labels = torch.max(labels, 1)[1]
        loss = criterion(outputs, labels)        
        # Backward and optimize
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()        
    if (epoch+1) % 100 == 0:
        print (f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')

print(f'final loss: {loss.item():.4f}')
data = {
"model_state": model.state_dict(),
"input_size": input_size,
"hidden_size": hidden_size,
"output_size": output_size,
"all_words": all_words,
"tags": tags
}
FILE = "chatdata.pth"
torch.save(data, FILE)
print(f'training complete. file saved to {FILE}')

3.4 - Execute o arquivo train.py anterior para treinar o modelo

O arquivo train.py criará o arquivo chatdata.pth que se utilizará em nosso arquivo chat.py

Passo 4: Criar uma aplicação ChatBot baseada no modelo treinado

Como nosso modelo é criado. No passo final, vamos criar um arquivo chat.py que podemos utilizar no nosso chatbot

#chat.py
import os,random,json,torch
from model import NeuralNet
from nltk_utils import bag_of_words, tokenize

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

with open("intents.json") as json_data:
    intents = json.load(json_data)

data_dir = os.path.join(os.path.dirname(__file__))
FILE = os.path.join(data_dir, 'chatdata.pth')
data = torch.load(FILE)

input_size = data["input_size"]
hidden_size = data["hidden_size"]
output_size = data["output_size"]
all_words = data['all_words']
tags = data['tags']
model_state = data["model_state"]

model = NeuralNet(input_size, hidden_size, output_size).to(device)
model.load_state_dict(model_state)
model.eval()

bot_name = "iris-NLP"
def get_response(msg):
    sentence = tokenize(msg)
    X = bag_of_words(sentence, all_words)
    X = X.reshape(1, X.shape[0])
    X = torch.from_numpy(X).to(device)

    output = model(X)
    _, predicted = torch.max(output, dim=1)

    tag = tags[predicted.item()]

    probs = torch.softmax(output, dim=1)
    prob = probs[0][predicted.item()]
    if prob.item() > 0.75:
        for intent in intents['intents']:
            if tag == intent["tag"]:
                return random.choice(intent['responses'])
    return "I do not understand..."
if __name__ == "__main__":
    print("Let's chat! (type 'quit' to exit)")
    while True:
        sentence = input("You: ")
        if sentence == "quit":
            break
        resp = get_response(sentence)
        print(resp)

Ejecutad el archivo chat.py y realizad algunas preguntas que ya definimos anteriormente en nuestro archivo intents.json

 

Para mais informação, visite a página do open exchange IRIS-GenLab

Obrigado

Comentarios (0)1
Inicie sesión o regístrese para continuar
Artículo
· 23 jul, 2024 Lectura de 1 min

Implemented Community Idea about Generative AI for emails in Python Contest

I implemented a Python Flask application for the 2024 Python Contest with a page that provides common form fields for an outgoing email such as the To and CC fields. And it lets you input a message as well as uploading text based attachments.

Then using LlamaIndex in Python, the app analyzes the content you put in and returns to you in a result box if there is anything that should stop you from sending that email.

Take a look at the Github repo here.

3 comentarios
Comentarios (3)3
Inicie sesión o regístrese para continuar
Anuncio
· 23 jul, 2024

[Video] Organize Your Code with Namespaces

Hi Community,

What are the advantages of using multiple namespaces for your code? Learn some of the benefits in this discussion with @Derek Robinson, Senior Online Course Developer, and @Scott Clark, Implementation Specialist:

Using Multiple Namespaces

In this video, you will hear some potential use cases for creating multiple namespaces.

Ready to try it yourself? See how to set up namespaces and databases in the Management Portal in InterSystems IRIS® data platform (video, 2m).

3 comentarios
Comentarios (3)1
Inicie sesión o regístrese para continuar