Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Export tests function #3

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions bothub_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def create_example(example, *args):
"repository": repository,
"repository_version": repository_version,
"text": example.get('text'),
"language": example.get('language'),
"intent": example.get('intent'),
"entities": entities
}
Expand Down Expand Up @@ -101,22 +102,29 @@ def delete_all_examples(*args):
print(response)


def create_evaluate_examples(*args):
with open('examples/rasa-dataset-testing.json', encoding="utf-8") as json_file:
def create_evaluate_examples(filename, *args):
with open(f'{filename}.json', encoding="utf-8") as json_file:
examples = json.load(json_file)

count = len(examples)
index = 0

for example in examples:
entities = []
for entity in example['entities']:
entities.append({
"entity": entity['entity'],
"start": entity['start'],
"end": entity['end']
})
data = {
"is_corrected": False,
"repository": repository,
"repository_version": int(repository_version),
"text": example['text'],
"language": example['language'],
"intent": example['intent'],
"entities": []
"entities": entities
}

response = requests.post(
Expand Down
19 changes: 17 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from decouple import config

from bothub_api import get_all_examples, create_example, delete_example, get_examples_count
from bothub_api import get_all_examples, create_example, delete_example, get_examples_count, create_evaluate_examples

repository = config('REPOSITORY_UUID')
repository_version = config('REPOSITORY_VERSION_ID')
Expand Down Expand Up @@ -51,6 +51,21 @@ def export_to_json(filename, *args):
with open(f'{filename}.json', 'w') as outfile:
json.dump(examples, outfile)

def export_evaluate_to_json(filename, *args):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eae, @rsogit! Tava pensando aqui, e vi que essa função é igual a da linha 39 export_to_json, mudando apenas o final do endpoint ("examples/" ou "evaluate/").

Será que ficaria melhor permanecer com uma só função, e adicionar um parâmetro para receber o fim do endpoint examples ou evaluate?

Tava pensando em algo mais ou menos assim:

def export_to_json(filename, endpoint='examples', *args):
    ...
    results = get_all_examples(
        ...
        # alterando o final da string com a variável endpoint
        next_call=f'{base_url}/repository/{endpoint}/'

Para funcionar, chamaríamos no terminal

python main.py nome_arquivo evaluate

Edit 1

Ou algo assim, que considero com menos chances de erro:

def export_to_json(filename, evaluate=False, *args):
    # Getting all sentences from bothub repository
    endpoint = 'examples'
    if evaluate:
        endpoint = 'evaluate'

    results = get_all_examples(
        ...
        # alterando o final da string com a variável endpoint
        next_call=f'{base_url}/repository/{endpoint}/'

Para funcionar, chamaríamos no terminal

python main.py export_to_json nome_arquivo evaluate=True

O que acha? Se puder, aproveita que tá com o código por ai, e aplica essa melhoria, se achar necessário.

# Getting all evaluate sentences from a bothub repository
results = get_all_examples(
headers=headers,
next_call=f'{base_url}/repository/evaluate/',
params=params
)
examples = []
for result in results:
for sentence in result:
examples.append(sentence)
# Save results in a JSON file
with open(f'{filename}.json', 'w') as outfile:
json.dump(examples, outfile)


def train_json_sentences(filename, *args):
# Read the examples in JSON file and save in a List
Expand Down Expand Up @@ -97,7 +112,7 @@ def train_chatito(filename, *args):

result = {
"text": example['text'],
"language": "pt-br",
"language": "pt_br",
"intent": example['intent'],
"entities": entities
}
Expand Down