-
Notifications
You must be signed in to change notification settings - Fork 1
[embeddings-computer]: Create #409
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
Open
parmentf
wants to merge
4
commits into
main
Choose a base branch
from
services/embeddings-computer/create
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Ignore all files by default | ||
| * | ||
|
|
||
| # White list only the required files | ||
| !config.json | ||
| !v1 | ||
| !swagger.json | ||
| !download-model.sh | ||
| !embed-local.mjs |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # syntax=docker/dockerfile:1.2 | ||
| FROM cnrsinist/ezs-python-server:py3.9-no24-1.0.13 | ||
|
|
||
| WORKDIR /app | ||
| # Install all python dependencies | ||
| # RUN pip install --no-cache-dir \ | ||
| # unidecode==1.3.7 | ||
|
|
||
| # Install all node dependencies | ||
| RUN npm install --omit=dev \ | ||
| @xenova/transformers@2.17.2 && \ | ||
| npm cache clean --force | ||
|
|
||
| WORKDIR /app/public | ||
| # Declare files to copy in .dockerignore | ||
| COPY --chown=daemon:daemon . /app/public/ | ||
| COPY --chown=daemon:daemon ./config.json /app/config.json | ||
|
|
||
| RUN ./download-model.sh && \ | ||
| chmod +x ./embed-local.mjs |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # ws-embeddings-computer@0.1.0 | ||
|
|
||
| Vectorisation de texte | ||
|
|
||
| Des services de génération de vecteurs numériques représentant du texte (plongements lexicaux, ou embeddings en anglais). |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "environnement": { | ||
| "EZS_TITLE": "Vectorisation de texte", | ||
| "EZS_DESCRIPTION": "Des services de génération de vecteurs numériques représentant du texte (plongements lexicaux, ou embeddings en anglais).", | ||
| "EZS_METRICS": true, | ||
| "EZS_CONCURRENCY": 2, | ||
| "EZS_CONTINUE_DELAY": 60, | ||
| "EZS_NSHARDS": 32, | ||
| "EZS_CACHE": true, | ||
| "EZS_VERBOSE": false, | ||
| "NODE_OPTIONS": "--max_old_space_size=1024", | ||
| "NODE_ENV": "production" | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| MODEL_ID="Xenova/all-MiniLM-L6-v2" | ||
| REVISION="main" # ou un hash de commit si tu veux figer la version | ||
| BASE_URL="https://huggingface.co/${MODEL_ID}/resolve/${REVISION}" | ||
| OUT_DIR="${1:-models/${MODEL_ID}}" | ||
|
|
||
| echo "Downloading ${MODEL_ID} into ${OUT_DIR}" | ||
|
|
||
| mkdir -p "${OUT_DIR}" | ||
|
|
||
| # Liste des fichiers nécessaires pour transformers.js | ||
| files=( | ||
| "config.json" | ||
| "tokenizer_config.json" | ||
| "tokenizer.json" | ||
| "special_tokens_map.json" | ||
| "vocab.txt" | ||
| "onnx/model_quantized.onnx" | ||
| ) | ||
|
|
||
| for file in "${files[@]}"; do | ||
| url="${BASE_URL}/${file}?download=true" | ||
| dest="${OUT_DIR}/${file}" | ||
| dest_dir="$(dirname "${dest}")" | ||
|
|
||
| mkdir -p "${dest_dir}" | ||
|
|
||
| echo "-> ${url}" | ||
| curl -fL "${url}" -o "${dest}" || { | ||
| echo "Échec du téléchargement de ${url}" >&2 | ||
| exit 1 | ||
| } | ||
| done | ||
|
|
||
| echo "Done." | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| // embeddings.ts | ||
| // Pour Node | ||
| // @ts-ignore | ||
| globalThis.self = globalThis; | ||
|
|
||
| import { env, pipeline } from "@xenova/transformers"; | ||
| import * as readline from "node:readline/promises"; | ||
| import { stdin as input, stdout as output } from "node:process"; | ||
|
|
||
| // 1) Forcer les modèles locaux uniquement | ||
| env.allowRemoteModels = false; | ||
|
|
||
| // 2) Répertoire racine des modèles pré-téléchargés | ||
| // => par exemple monté dans le container à /app/public/models/ | ||
| env.localModelPath = "models"; | ||
|
|
||
| // 3) Préchargement (optionnel) au démarrage | ||
| /** @type {Promise<any> | null} */ | ||
| let embedderPromise = null; | ||
|
|
||
| export function initEmbedder() { | ||
| if (!embedderPromise) { | ||
| embedderPromise = pipeline( | ||
| "feature-extraction", | ||
| // IMPORTANT : chemin *logique* du modèle à l’intérieur de models/ | ||
| // Si tu as models/Xenova/all-MiniLM-L6-v2, tu mets exactement : | ||
| "Xenova/all-MiniLM-L6-v2" | ||
| ); | ||
| } | ||
| return embedderPromise; | ||
| } | ||
|
|
||
| /** | ||
| * @param texts {string[]} | ||
| * @return {Promise<number[][]>} | ||
| */ | ||
| export async function embedTexts(texts) { | ||
| const extractor = await initEmbedder(); | ||
| const output = await extractor(texts, { | ||
| pooling: "mean", | ||
| normalize: true, | ||
| }); | ||
|
|
||
| /** @type {Float32Array} */ | ||
| const data = output.data; | ||
| const [batchSize, dim] = output.dims; | ||
| /** @type {number[][]} */ | ||
| const vectors = []; | ||
| for (let i = 0; i < batchSize; i++) { | ||
| const start = i * dim; | ||
| const end = start + dim; | ||
| vectors.push(Array.from(data.subarray(start, end))); | ||
| } | ||
| return vectors; | ||
| } | ||
|
|
||
|
|
||
| /////////////////// | ||
| const rl = readline.createInterface({ input, output }); | ||
| for await (const line of rl) { | ||
| const json = JSON.parse(line); | ||
| const { value = [] } = json; | ||
| const texts = Array.isArray(value) ? value : [value]; | ||
| const vectors = await embedTexts(texts); | ||
| json.value = vectors; | ||
| console.log(JSON.stringify(json)); | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # These examples can be used directly in VSCode, using HTTPYac extension (anweber.vscode-httpyac) | ||
| # They are important, because used to generate the tests.hurl file. | ||
|
|
||
| # Décommenter/commenter les lignes voulues pour tester localement | ||
| @host=http://localhost:31976 | ||
| # @host=https://embeddings-computer.services.istex.fr | ||
|
|
||
| ### | ||
| # @name v1embedSmallText | ||
| # Calcule les embeddings de petits textes (sans les découper) | ||
| POST {{host}}/v1/embed-small-text?indent=true HTTP/1.1 | ||
| Content-Type: application/json | ||
|
|
||
| [ | ||
| { "value": "This is a test text for the embeddings model. It should be able to handle multiple texts at once." }, | ||
| { "value": "Et maintenant, je mets une phrase en français." } | ||
| ] |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| { | ||
| "private": true, | ||
| "name": "ws-embeddings-computer", | ||
| "version": "0.1.0", | ||
| "description": "Vectorisation de texte", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/Inist-CNRS/web-services.git" | ||
| }, | ||
| "keywords": [ | ||
| "ezmaster" | ||
| ], | ||
| "author": "François Parmentier <francois.parmentier@gmail.com>", | ||
| "license": "MIT", | ||
| "bugs": { | ||
| "url": "https://github.com/Inist-CNRS/web-services/issues" | ||
| }, | ||
| "homepage": "https://github.com/Inist-CNRS/web-services/#readme", | ||
| "scripts": { | ||
| "version:insert:readme": "sed -i \"s#\\(${npm_package_name}.\\)\\([\\.a-z0-9]\\+\\)#\\1${npm_package_version}#g\" README.md && git add README.md", | ||
| "version:insert:swagger": "sed -i \"s/\\\"version\\\": \\\"[0-9]\\+.[0-9]\\+.[0-9]\\+\\\"/\\\"version\\\": \\\"${npm_package_version}\\\"/g\" swagger.json && git add swagger.json", | ||
| "version:insert": "npm run version:insert:readme && npm run version:insert:swagger", | ||
| "version:commit": "git commit -a -m \"release ${npm_package_name}@${npm_package_version}\"", | ||
| "version:tag": "git tag \"${npm_package_name}@${npm_package_version}\" -m \"${npm_package_name}@${npm_package_version}\"", | ||
| "version:push": "git push && git push --tags", | ||
| "version": "npm run version:insert && npm run version:commit && npm run version:tag", | ||
| "postversion": "npm run version:push", | ||
| "build:check": "DOCKER_BUILDKIT=1 docker build --check -t cnrsinist/${npm_package_name}:latest . && docker run --rm -i hadolint/hadolint hadolint - < Dockerfile ", | ||
| "build:dev": "docker build -t cnrsinist/${npm_package_name}:latest .", | ||
| "start:dev": "npm run build:dev && docker run --name dev --rm --detach -p 31976:31976 cnrsinist/${npm_package_name}:latest", | ||
| "stop:dev": "docker stop dev", | ||
| "build": "docker build -t cnrsinist/${npm_package_name}:${npm_package_version} .", | ||
| "start": "docker run --rm -p 31976:31976 cnrsinist/${npm_package_name}:${npm_package_version}", | ||
| "publish": "docker push cnrsinist/${npm_package_name}:${npm_package_version}" | ||
| }, | ||
| "avoid-testing": false | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "openapi": "3.0.0", | ||
| "info": { | ||
| "title": "embeddings-computer - Vectorisation de texte", | ||
| "description": "Des services de génération de vecteurs numériques représentant du texte (plongements lexicaux, ou embeddings en anglais).", | ||
| "version": "0.1.0", | ||
| "termsOfService": "https://services.istex.fr/", | ||
| "contact": { | ||
| "name": "Inist-CNRS", | ||
| "url": "https://www.inist.fr/nous-contacter/" | ||
| } | ||
| }, | ||
| "servers": [ | ||
| { | ||
| "x-comment": "Will be automatically completed by the ezs server." | ||
| }, | ||
| { | ||
| "url": "http://vptdmservices.intra.inist.fr:49225/", | ||
| "description": "Latest version for production", | ||
| "#DISABLED#x-profil": "Standard" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. polish: (bien penser à supprimer le "disabled" sur un nouveau service ! ) |
||
| } | ||
| ], | ||
| "tags": [ | ||
| { | ||
| "name": "embeddings-computer", | ||
| "description": "Vectorisation de texte", | ||
| "externalDocs": { | ||
| "description": "Plus de documentation", | ||
| "url": "https://github.com/inist-cnrs/web-services/tree/main/services/embeddings-computer" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: Bonne idée de télécharger directement le modèle avec un message d'erreur (ça évite le stockage inutile dans le dav)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Et surtout d'avoir à le télécharger à chaque fois qu'on lance le programme.