mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
23 KiB
23 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[pipeline] sacremoses
# Remove tensorflow as it's not used and prints noisy log messages
!pip uninstall -y tensorflow
# Install python3.7-dev and nasm
!apt-get install python3.7-dev nasmIn [ ]:
%%writefile config.yml
summary:
path: sshleifer/distilbart-cnn-12-6
textractor:
join: true
lines: false
minlength: 100
paragraphs: true
sentences: false
tika: false
translation:
workflow:
summary:
tasks:
- action: textractor
task: url
- action: summary
translate:
tasks:
- action: translation
args:
- frWriting config.yml
In [ ]:
%%writefile workflow.c
#include <Python.h>
// Global instances
PyObject *module = NULL, *app = NULL;
/**
* Create txtai module.
*/
PyObject* txtai() {
PyObject* module = NULL;
module = PyImport_ImportModule("txtai.app");
return module;
}
/**
* Create txtai application instance.
*/
PyObject* application() {
PyObject* app = NULL;
app = PyObject_CallMethod(module, "Application", "z", "config.yml");
return app;
}
/**
* Run txtai workflow.
*/
PyObject* run(char** args) {
PyObject* result = NULL;
result = PyObject_CallMethod(app, "workflow", "z[z]", args[0], args[1]);
return result;
}
/**
* Cleanup Python objects.
*/
void cleanup() {
// Ensure Python instance exists
if (Py_IsInitialized()) {
PyErr_Print();
Py_CLEAR(app);
Py_CLEAR(module);
Py_FinalizeEx();
}
}
/**
* Initialize a txtai application and run a workflow.
*/
const char* workflow(char** args) {
PyObject* result = NULL;
// Create application instance if it doesn't already exist
if (!Py_IsInitialized()) {
// Start Python Interpreter
Py_Initialize();
// Create txtai module
module = txtai();
// Handle errors
if (!module) {
cleanup();
return NULL;
}
// Create txtai application
app = application();
// Handle errors
if (!app) {
cleanup();
return NULL;
}
}
// Run workflow
result = run(args);
// Handle errors
if (!result) {
cleanup();
return NULL;
}
// Get first result
const char *text = PyUnicode_AsUTF8(PyIter_Next(result));
// Cleanup result
Py_CLEAR(result);
return text;
}Writing workflow.c
In [ ]:
%%writefile main.c
#include <stdio.h>
extern char* workflow(char** argv);
extern void cleanup();
/**
* Run a txtai workflow and print results.
*/
int main(int argc, char** argv) {
if (argc < 3) {
printf("Usage: workflow <name> <element>\n");
return 1;
}
// Run workflow using command line arguments
char* text = workflow(argv + 1);
if (text) {
printf("%s\n", text);
}
// Cleanup
cleanup();
return 0;
}Writing main.c
In [ ]:
!cc -c main.c -I/usr/include/python3.7m
!cc -c workflow.c -I/usr/include/python3.7m
!cc -o workflow workflow.o main.o -lpython3.7mIn [ ]:
!./workflow translate "I'm running machine translation using a transformers model in C!"J'exécute la traduction automatique à l'aide d'un modèle de transformateurs en C!
In [ ]:
!ldd workflow | grep pythonlibpython3.7m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.7m.so.1.0 (0x00007efcba85e000)
In [ ]:
%%writefile main.asm
global main
; External C library functions
extern puts
; External txtai functions
extern workflow, cleanup
; Default to REL mode
default REL
section .data
message: db "Usage: workflow <name> <element>", 0
section .text
; Print a usage message
usage:
mov rdi, message
call puts
jmp done
; Main function
main:
; Enter
sub rsp, 8
; Read argc - require workflow name and element (plus program name)
cmp rdi, 3
jl usage
; Run txtai workflow with argv params (skip program name) and print result
lea rdi, [rsi + 8]
call workflow
mov rdi, rax
call puts
done:
; Close txtai application instance
call cleanup
; Exit
add rsp, 8
retWriting main.asm
In [ ]:
# Build workflow executable
!nasm -felf64 main.asm
!cc -c workflow.c -I/usr/include/python3.7m
!cc -o workflow -no-pie workflow.o main.o -lpython3.7mIn [ ]:
!./workflow translate "I'm running machine translation using a transformers model with assembler!"J'exécute la traduction automatique à l'aide d'un modèle de transformateurs avec assembleur!
In [ ]:
%%writefile main.asm
global main
; External C library functions
extern printf
; External txtai functions
extern workflow, cleanup
; Default to REL mode
default REL
section .data
format: db "action: %s", 10, "input: %s", 10, "output: %s", 10, 10, 0
summary: db "summary", 0
translate: db "translate", 0
text1: db "txtai executes machine-learning workflows to transform data and build AI-powered semantic search applications.", 0
text2: db "Traditional search systems use keywords to find data", 0
url1: db "https://github.com/neuml/txtai", 0
url2: db "https://github.com/neuml/paperai", 0
section .text
; Run txtai workflow and print results
%macro txtai 2
; Workflow name and element
push %2
push %1
; Run workflow
lea rdi, [rsp]
call workflow
; Print action-input-output
mov rdi, format
mov rsi, [rsp]
mov rdx, [rsp + 8]
mov rcx, rax
call printf
; Restore stack
add rsp, 16
%endmacro
; Main function
main:
; Enter
sub rsp, 8
; Run workflows
txtai translate, text1
txtai translate, text2
txtai summary, url1
txtai summary, url2
done:
; Close txtai application instance
call cleanup
; Exit
add rsp, 8
retOverwriting main.asm
In [ ]:
!time ./workflow translate "I'm running machine translation using a transformers model with assembler!"J'exécute la traduction automatique à l'aide d'un modèle de transformateurs avec assembleur! real 0m19.208s user 0m11.256s sys 0m3.224s
In [ ]:
# Build workflow executable
!nasm -felf64 main.asm
!cc -c workflow.c -I/usr/include/python3.7m
!cc -no-pie -o workflow workflow.o main.o -lpython3.7mIn [ ]:
!time ./workflowaction: translate input: txtai executes machine-learning workflows to transform data and build AI-powered semantic search applications. output: txtai exécute des workflows d'apprentissage automatique pour transformer les données et construire des applications de recherche sémantique alimentées par l'IA. action: translate input: Traditional search systems use keywords to find data output: Les systèmes de recherche traditionnels utilisent des mots-clés pour trouver des données action: summary input: https://github.com/neuml/txtai output: txtai executes machine-learning workflows to transform data and build AI-powered semantic search applications. Semantic search applications have an understanding of natural language and identify results that have the same meaning, not necessarily the same keywords. API bindings for JavaScript, Java, Rust and Go. Cloud-native architecture scales out with container orchestration systems (e. g. Kubernetes) action: summary input: https://github.com/neuml/paperai output: paperai is an AI-powered literature discovery and review engine for medical/scientific papers. Paperai was used to analyze the COVID-19 Open Research Dataset (CORD-19) paperai and NeuML have been recognized in the following articles: Cord-19 Kaggle Challenge Awards Machine-Learning Experts Delve Into 47,000 Papers on Coronavirus Family. real 0m22.478s user 0m13.776s sys 0m3.218s