Spaces:
Paused
Paused
Download src/tools.py from Demosthene-OR/rakuten: direct link, hf CLI and curl.
- Browser
- Download file 2.84 kB
-
https://huggingface.co/spaces/Demosthene-OR/rakuten/resolve/main/src/tools.py
- Command line
-
hf download hf://spaces/Demosthene-OR/rakuten/src/tools.py
-
curl -L -o tools.py https://huggingface.co/spaces/Demosthene-OR/rakuten/resolve/main/src/tools.py
2.84 kB
| import tensorflow as tf | |
| from tensorflow import keras | |
| from keras import backend as K | |
| from huggingface_hub import hf_hub_download | |
| import os | |
| # Definition de la metrique weighted-F1 score | |
| precision = tf.keras.metrics.Precision() | |
| recall = tf.keras.metrics.Recall() | |
| file_path = "models" | |
| REPO_ID = "Demosthene-OR/rakuten-models" | |
| def f1_m(y_true, y_pred): | |
| precision.update_state(y_true, y_pred) | |
| recall.update_state(y_true, y_pred) | |
| precision_result = precision.result() | |
| recall_result = recall.result() | |
| # Calculer le F1-score | |
| f1 = 2 * ((precision_result * recall_result) / (precision_result + recall_result + K.epsilon())) | |
| # Calculer le weighted F1-score | |
| weights = tf.reduce_sum(y_true, axis=0) / tf.reduce_sum(y_true) | |
| weighted_f1 = tf.reduce_sum(weights * f1) | |
| return weighted_f1 | |
| def check_and_download(file_path, file_name): | |
| """ | |
| Checks if a file exists locally, otherwise downloads it from Hugging Face Hub. | |
| """ | |
| full_path = os.path.join(file_path, file_name) | |
| # If file doesn't exist locally, download from Hub | |
| if not os.path.exists(full_path): | |
| print(f"File {file_name} not found locally in {file_path}. Downloading from {REPO_ID}...") | |
| try: | |
| # existing code expected it in 'models/' folder (or whatever path is passed). | |
| hf_hub_download( | |
| repo_id=REPO_ID, | |
| filename=file_name, | |
| local_dir=file_path, | |
| local_dir_use_symlinks=False | |
| ) | |
| print("Download complete.") | |
| except Exception as e: | |
| print(f"Error downloading file: {e}") | |
| raise e | |
| return full_path | |
| def load_model(file_path, file_name): | |
| """ | |
| Loads a model, downloading it from Hugging Face Hub if not present locally. | |
| """ | |
| model_path = check_and_download(file_path, file_name) | |
| with keras.utils.custom_object_scope({"f1_m": f1_m}): | |
| return keras.models.load_model(model_path) | |
| def save_model(file_path, file_name): | |
| """ | |
| Saves the model locally. Uploading to Hub is handled separately. | |
| """ | |
| if not os.path.exists(file_path): | |
| os.makedirs(file_path) | |
| # No more splitting | |
| # model.save() is called by the caller, this function seemed to only handle the splitting part | |
| # but looking at train_model.py:114: save_model(self.file_path, "best_rnn_model.h5") | |
| # Wait, train_model.py calls model.save() THEN save_model()? | |
| # train_model.py: | |
| # 114: save_model(self.file_path, "best_rnn_model.h5") | |
| # Actually train_model.py saves the model in the callback: | |
| # 95: filepath=self.file_path+"/best_rnn_model.h5" | |
| # So save_model in tools.py was purely for splitting! | |
| # So we can just make this a pass or remove it, but to keep compatibility we'll just pass. | |
| pass | |