From 3ab655a1728ca26699be36ce65e39ac2df176ccb Mon Sep 17 00:00:00 2001 From: Ivan Pavlovich <ivan.pavlovic@hes-so.ch> Date: Sat, 29 Mar 2025 23:51:19 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20d'entaite=20dans=20les=20scripts=20util?= =?UTF-8?q?is=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/FineTuning/fine_tuning_wrapper.py | 10 +++++ models/FineTuning/finetunning-multi-label.py | 12 +++++- models/FineTuning/job_launch.sbatch | 2 +- .../HuggingFace/zero_shot_classification.py | 39 ++++++------------- models/LLM/Cohere/cohere_wrapper.py | 10 +++++ models/LLM/Gemini/gemini.py | 10 +++++ models/LLM/Ollama/ollama_wrapper.py | 20 +++++----- 7 files changed, 61 insertions(+), 42 deletions(-) diff --git a/models/FineTuning/fine_tuning_wrapper.py b/models/FineTuning/fine_tuning_wrapper.py index e6156d71e..f6afc779f 100644 --- a/models/FineTuning/fine_tuning_wrapper.py +++ b/models/FineTuning/fine_tuning_wrapper.py @@ -1,3 +1,13 @@ +# ----------------------------- +# Le script ne fait rien. Il met a disposition deux fonction une qui créer un classifier à +# partir du model fine-tuner (./model). Le deuxième classifie une sequence sur des label en utilisant le classifier +# créer dans la première fonction +# +# Ce script ne prend pas de paramètres. +# +# Auteur : Pavlovich Ivan +# ----------------------------- + from transformers import pipeline import os import sys diff --git a/models/FineTuning/finetunning-multi-label.py b/models/FineTuning/finetunning-multi-label.py index dda2f01d5..a397a1cde 100644 --- a/models/FineTuning/finetunning-multi-label.py +++ b/models/FineTuning/finetunning-multi-label.py @@ -1,4 +1,12 @@ -# https://medium.com/@lidores98/finetuning-huggingface-facebook-bart-model-2c758472e340 +# ----------------------------- +# Le script affine le modèle BART-Large-MNLI de Facebook AI sur les articles stoquer localement (dataSources/PubMed/save/save_3_years.json) +# +# Ce script ne prend pas de paramètres. +# +# Basée sur : https://medium.com/@lidores98/finetuning-huggingface-facebook-bart-model-2c758472e340 +# +# Auteur : Pavlovich Ivan +# ----------------------------- import pandas as pd import torch @@ -18,7 +26,7 @@ from parsers.jsonParser import parseJsonFile from variables.pubmed import NCDS, NCDS_MESH_TERM DATASET_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../testModel/dataset")) -TMP_DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../dataSources/PubMed/tmp")) +TMP_DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../dataSources/PubMed/save")) SAVE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "./save")) MODEL_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "./model")) LOGS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "./logs")) diff --git a/models/FineTuning/job_launch.sbatch b/models/FineTuning/job_launch.sbatch index 71a6bb93d..105a8c918 100644 --- a/models/FineTuning/job_launch.sbatch +++ b/models/FineTuning/job_launch.sbatch @@ -13,4 +13,4 @@ module load CUDA/12.3.0 GCC/12.2.0 Python/3.10.8 Qt5/5.15.7 source ../../.venv/bin/activate -python3 facebook-bart-large-mnli.py \ No newline at end of file +python3 finetunning-multi-label.py \ No newline at end of file diff --git a/models/HuggingFace/zero_shot_classification.py b/models/HuggingFace/zero_shot_classification.py index 0e8533d24..9aebb04a1 100644 --- a/models/HuggingFace/zero_shot_classification.py +++ b/models/HuggingFace/zero_shot_classification.py @@ -1,35 +1,18 @@ +# ----------------------------- +# Le script ne fait rien. Il met a disposition deux fonction une qui créer une pipline zero-shot classification à +# partir d un model hugging face. Le deuxième classifie une sequence sur des label en utilisant le classifier +# créer dans la première fonction +# +# Ce script ne prend pas de paramètres. +# +# Auteur : Pavlovich Ivan +# ----------------------------- + from transformers import pipeline import torch import time -LABELS = [ - "Diabetes", - "Cancer", - "Chronic respiratory disease", - "Cardiovascular diseases", - "Mental Health", - "Diabetes type 1", - "Diabetes type 2" -] - -# LABELS = [ -# "Neoplasms", -# "Diabetes Mellitus", -# "Male", -# "Blood Cells", -# "Arthritis, Infectious" -# ] - -MODELS = [ - "facebook/bart-large-mnli", # https://huggingface.co/facebook/bart-large-mnli - "MoritzLaurer/bge-m3-zeroshot-v2.0", # https://huggingface.co/MoritzLaurer/bge-m3-zeroshot-v2.0 - "MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli", - "MoritzLaurer/deberta-v3-base-zeroshot-v1.1-all-33", - "MoritzLaurer/multilingual-MiniLMv2-L6-mnli-xnli", - "microsoft/BiomedNLP-BiomedBERT-base-uncased-abstract" # https://huggingface.co/microsoft/BiomedNLP-BiomedBERT-base-uncased-abstract -] - -def create_classifier(model = MODELS[0]): +def create_classifier(model): print(f" CUDA available: {torch.cuda.is_available()}") print(f"CUDA version: {torch.version.cuda}") print(f"GPUs number: {torch.cuda.device_count()}") diff --git a/models/LLM/Cohere/cohere_wrapper.py b/models/LLM/Cohere/cohere_wrapper.py index 4a3d7de66..e1546cf9b 100644 --- a/models/LLM/Cohere/cohere_wrapper.py +++ b/models/LLM/Cohere/cohere_wrapper.py @@ -1,3 +1,13 @@ +# ----------------------------- +# Le script ne fait rien. Il met a disposition deux fonction une qui créer un client cohere à +# partir d une clé. Le deuxième classifie une sequence sur des label en utilisant le client +# créer dans la première fonction +# +# Ce script ne prend pas de paramètres. +# +# Auteur : Pavlovich Ivan +# ----------------------------- + import cohere import json import time diff --git a/models/LLM/Gemini/gemini.py b/models/LLM/Gemini/gemini.py index 5bb6fd8d2..7cb8aeb6b 100644 --- a/models/LLM/Gemini/gemini.py +++ b/models/LLM/Gemini/gemini.py @@ -1,3 +1,13 @@ +# ----------------------------- +# Le script ne fait rien. Il met a disposition deux fonction une qui créer un chat gemini. +# La deuxième classifie une sequence sur des label en utilisant le chat créer dans la première fonction +# +# Ce script ne prend pas de paramètres. +# +# Auteur : Pavlovich Ivan +# ----------------------------- + + import os import google.generativeai as genai import json diff --git a/models/LLM/Ollama/ollama_wrapper.py b/models/LLM/Ollama/ollama_wrapper.py index c96c276e1..30a6e0c45 100644 --- a/models/LLM/Ollama/ollama_wrapper.py +++ b/models/LLM/Ollama/ollama_wrapper.py @@ -1,19 +1,17 @@ +# ----------------------------- +# Le script ne fait rien. Il met a disposition une fonction qui classifie une sequence sur des label en utilisant un model ollama +# +# Ce script ne prend pas de paramètres. +# +# Auteur : Pavlovich Ivan +# ----------------------------- + + from ollama import chat from ollama import ChatResponse import json import time -NCDS = [ - "Non-Communicable Diseases", - "Diabetes", - "Cancer", - "Chronic respiratory disease", - "Cardiovascular diseases", - "Mental Health", - "Diabetes type 1", - "Diabetes type 2" -] - def classify(model, sequence, labels): prompt = 'I need you to give me the labels that could be given to the text (keep in mind that u can put multiple labels and select only the labels that i give you):\n' -- GitLab