Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
71afe01
1
Parent(s):
5814ab0
Trying to load in cuda only within spaces environment to enable zero GPU space to run successfully
Browse files- app.py +2 -1
- funcs/embeddings.py +17 -14
- funcs/representation_model.py +15 -2
- funcs/topic_core_funcs.py +10 -8
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import socket
|
| 3 |
import gradio as gr
|
| 4 |
import pandas as pd
|
| 5 |
import numpy as np
|
| 6 |
from datetime import datetime
|
| 7 |
-
|
| 8 |
|
| 9 |
from funcs.topic_core_funcs import pre_clean, optimise_zero_shot, extract_topics, reduce_outliers, represent_topics, visualise_topics, save_as_pytorch_model, change_default_vis_col
|
| 10 |
from funcs.helper_functions import initial_file_load, custom_regex_load, ensure_output_folder_exists, output_folder, get_connection_params, get_or_create_env_var
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
import os
|
| 3 |
import socket
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
| 6 |
import numpy as np
|
| 7 |
from datetime import datetime
|
| 8 |
+
|
| 9 |
|
| 10 |
from funcs.topic_core_funcs import pre_clean, optimise_zero_shot, extract_topics, reduce_outliers, represent_topics, visualise_topics, save_as_pytorch_model, change_default_vis_col
|
| 11 |
from funcs.helper_functions import initial_file_load, custom_regex_load, ensure_output_folder_exists, output_folder, get_connection_params, get_or_create_env_var
|
funcs/embeddings.py
CHANGED
|
@@ -2,29 +2,18 @@ import time
|
|
| 2 |
import numpy as np
|
| 3 |
import os
|
| 4 |
import spaces
|
| 5 |
-
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
from sklearn.pipeline import make_pipeline
|
| 8 |
from sklearn.decomposition import TruncatedSVD
|
| 9 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 10 |
|
| 11 |
-
|
| 12 |
# If you want to disable cuda for testing purposes
|
| 13 |
#os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
| 14 |
|
| 15 |
-
print("Is CUDA enabled? ", cuda.is_available())
|
| 16 |
-
print("Is a CUDA device available on this computer?", backends.cudnn.enabled)
|
| 17 |
-
if cuda.is_available():
|
| 18 |
-
torch_device = "gpu"
|
| 19 |
-
print("Cuda version installed is: ", version.cuda)
|
| 20 |
-
high_quality_mode = "Yes"
|
| 21 |
-
os.system("nvidia-smi")
|
| 22 |
-
else:
|
| 23 |
-
torch_device = "cpu"
|
| 24 |
-
high_quality_mode = "No"
|
| 25 |
|
| 26 |
-
|
| 27 |
-
@spaces.GPU
|
| 28 |
def make_or_load_embeddings(docs: list, file_list: list, embeddings_out: np.ndarray, embeddings_super_compress: str, high_quality_mode_opt: str, embeddings_name:str="mixedbread-ai/mxbai-embed-xsmall-v1", random_seed:int=42) -> np.ndarray:
|
| 29 |
"""
|
| 30 |
Create or load embeddings for the given documents.
|
|
@@ -41,6 +30,20 @@ def make_or_load_embeddings(docs: list, file_list: list, embeddings_out: np.ndar
|
|
| 41 |
np.ndarray: The generated or loaded embeddings.
|
| 42 |
"""
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if high_quality_mode_opt == "Yes":
|
| 45 |
# Define a list of possible local locations to search for the model
|
| 46 |
local_embeddings_locations = [
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import os
|
| 4 |
import spaces
|
| 5 |
+
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
from sklearn.pipeline import make_pipeline
|
| 8 |
from sklearn.decomposition import TruncatedSVD
|
| 9 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 10 |
|
| 11 |
+
|
| 12 |
# If you want to disable cuda for testing purposes
|
| 13 |
#os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
@spaces.GPU(duration=120)
|
|
|
|
| 17 |
def make_or_load_embeddings(docs: list, file_list: list, embeddings_out: np.ndarray, embeddings_super_compress: str, high_quality_mode_opt: str, embeddings_name:str="mixedbread-ai/mxbai-embed-xsmall-v1", random_seed:int=42) -> np.ndarray:
|
| 18 |
"""
|
| 19 |
Create or load embeddings for the given documents.
|
|
|
|
| 30 |
np.ndarray: The generated or loaded embeddings.
|
| 31 |
"""
|
| 32 |
|
| 33 |
+
# Check for torch cuda
|
| 34 |
+
from torch import cuda, backends, version
|
| 35 |
+
|
| 36 |
+
print("Is CUDA enabled? ", cuda.is_available())
|
| 37 |
+
print("Is a CUDA device available on this computer?", backends.cudnn.enabled)
|
| 38 |
+
if cuda.is_available():
|
| 39 |
+
torch_device = "gpu"
|
| 40 |
+
print("Cuda version installed is: ", version.cuda)
|
| 41 |
+
high_quality_mode = "Yes"
|
| 42 |
+
os.system("nvidia-smi")
|
| 43 |
+
else:
|
| 44 |
+
torch_device = "cpu"
|
| 45 |
+
high_quality_mode = "No"
|
| 46 |
+
|
| 47 |
if high_quality_mode_opt == "Yes":
|
| 48 |
# Define a list of possible local locations to search for the model
|
| 49 |
local_embeddings_locations = [
|
funcs/representation_model.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
from bertopic.representation import LlamaCPP
|
| 3 |
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from gradio import Warning
|
| 8 |
|
|
@@ -19,6 +19,19 @@ random_seed = 42
|
|
| 19 |
RUNNING_ON_AWS = get_or_create_env_var('RUNNING_ON_AWS', '0')
|
| 20 |
print(f'The value of RUNNING_ON_AWS is {RUNNING_ON_AWS}')
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Currently set n_gpu_layers to 0 even with cuda due to persistent bugs in implementation with cuda
|
| 23 |
print("torch device for representation functions:", torch_device)
|
| 24 |
if torch_device == "gpu":
|
|
@@ -29,7 +42,7 @@ else: # torch_device = "cpu"
|
|
| 29 |
n_gpu_layers = 0
|
| 30 |
|
| 31 |
#print("Running on device:", torch_device)
|
| 32 |
-
n_threads =
|
| 33 |
print("CPU n_threads:", n_threads)
|
| 34 |
|
| 35 |
# Default Model parameters
|
|
|
|
| 2 |
from bertopic.representation import LlamaCPP
|
| 3 |
|
| 4 |
from pydantic import BaseModel
|
| 5 |
+
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from gradio import Warning
|
| 8 |
|
|
|
|
| 19 |
RUNNING_ON_AWS = get_or_create_env_var('RUNNING_ON_AWS', '0')
|
| 20 |
print(f'The value of RUNNING_ON_AWS is {RUNNING_ON_AWS}')
|
| 21 |
|
| 22 |
+
from torch import cuda, backends, version, get_num_threads
|
| 23 |
+
|
| 24 |
+
print("Is CUDA enabled? ", cuda.is_available())
|
| 25 |
+
print("Is a CUDA device available on this computer?", backends.cudnn.enabled)
|
| 26 |
+
if cuda.is_available():
|
| 27 |
+
torch_device = "gpu"
|
| 28 |
+
print("Cuda version installed is: ", version.cuda)
|
| 29 |
+
high_quality_mode = "Yes"
|
| 30 |
+
os.system("nvidia-smi")
|
| 31 |
+
else:
|
| 32 |
+
torch_device = "cpu"
|
| 33 |
+
high_quality_mode = "No"
|
| 34 |
+
|
| 35 |
# Currently set n_gpu_layers to 0 even with cuda due to persistent bugs in implementation with cuda
|
| 36 |
print("torch device for representation functions:", torch_device)
|
| 37 |
if torch_device == "gpu":
|
|
|
|
| 42 |
n_gpu_layers = 0
|
| 43 |
|
| 44 |
#print("Running on device:", torch_device)
|
| 45 |
+
n_threads = get_num_threads()
|
| 46 |
print("CPU n_threads:", n_threads)
|
| 47 |
|
| 48 |
# Default Model parameters
|
funcs/topic_core_funcs.py
CHANGED
|
@@ -14,9 +14,9 @@ PandasDataFrame = Type[pd.DataFrame]
|
|
| 14 |
from funcs.clean_funcs import initial_clean, regex_clean
|
| 15 |
from funcs.anonymiser import expand_sentences_spacy
|
| 16 |
from funcs.helper_functions import read_file, zip_folder, delete_files_in_folder, save_topic_outputs, output_folder, get_or_create_env_var, custom_regex_load
|
| 17 |
-
from funcs.embeddings import make_or_load_embeddings
|
| 18 |
from funcs.bertopic_vis_documents import visualize_documents_custom, visualize_hierarchical_documents_custom, hierarchical_topics_custom, visualize_hierarchy_custom
|
| 19 |
-
|
| 20 |
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
|
| 21 |
from sklearn.decomposition import TruncatedSVD
|
| 22 |
import funcs.anonymiser as anon
|
|
@@ -31,14 +31,14 @@ today = datetime.now().strftime("%d%m%Y")
|
|
| 31 |
today_rev = datetime.now().strftime("%Y%m%d")
|
| 32 |
|
| 33 |
# Load embeddings
|
| 34 |
-
if RUNNING_ON_AWS=="0":
|
| 35 |
-
|
| 36 |
-
else:
|
| 37 |
-
|
| 38 |
|
| 39 |
# LLM model used for representing topics
|
| 40 |
-
hf_model_name = "bartowski/Llama-3.2-3B-Instruct-GGUF" #"bartowski/Phi-3.1-mini-128k-instruct-GGUF"
|
| 41 |
-
hf_model_file = "Llama-3.2-3B-Instruct-Q5_K_M.gguf" #"Phi-3.1-mini-128k-instruct-Q4_K_M.gguf"
|
| 42 |
|
| 43 |
# When topic modelling column is chosen, change the default visualisation column to the same
|
| 44 |
def change_default_vis_col(in_colnames:List[str]):
|
|
@@ -573,6 +573,8 @@ def represent_topics(topic_model: BERTopic, docs: List[str], data_file_name_no_e
|
|
| 573 |
|
| 574 |
progress(0.1, desc= "Loading model and creating new topic representation")
|
| 575 |
|
|
|
|
|
|
|
| 576 |
representation_model = create_representation_model(representation_type, llm_config, hf_model_name, hf_model_file, chosen_start_tag, high_quality_mode)
|
| 577 |
|
| 578 |
progress(0.3, desc= "Updating existing topics")
|
|
|
|
| 14 |
from funcs.clean_funcs import initial_clean, regex_clean
|
| 15 |
from funcs.anonymiser import expand_sentences_spacy
|
| 16 |
from funcs.helper_functions import read_file, zip_folder, delete_files_in_folder, save_topic_outputs, output_folder, get_or_create_env_var, custom_regex_load
|
| 17 |
+
from funcs.embeddings import make_or_load_embeddings
|
| 18 |
from funcs.bertopic_vis_documents import visualize_documents_custom, visualize_hierarchical_documents_custom, hierarchical_topics_custom, visualize_hierarchy_custom
|
| 19 |
+
|
| 20 |
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
|
| 21 |
from sklearn.decomposition import TruncatedSVD
|
| 22 |
import funcs.anonymiser as anon
|
|
|
|
| 31 |
today_rev = datetime.now().strftime("%Y%m%d")
|
| 32 |
|
| 33 |
# Load embeddings
|
| 34 |
+
# if RUNNING_ON_AWS=="0":
|
| 35 |
+
# embeddings_name = "mixedbread-ai/mxbai-embed-large-v1" #"mixedbread-ai/mxbai-embed-xsmall-v1" #"mixedbread-ai/mxbai-embed-large-v1"
|
| 36 |
+
# else:
|
| 37 |
+
embeddings_name = "mixedbread-ai/mxbai-embed-large-v1" #"mixedbread-ai/mxbai-embed-xsmall-v1"
|
| 38 |
|
| 39 |
# LLM model used for representing topics
|
| 40 |
+
hf_model_name = "unsloth/gemma-2-it-GGUF"#"bartowski/Llama-3.2-3B-Instruct-GGUF" #"bartowski/Phi-3.1-mini-128k-instruct-GGUF"
|
| 41 |
+
hf_model_file = "gemma-2-2b-it.q8_0.gguf" #"Llama-3.2-3B-Instruct-Q5_K_M.gguf" #"Phi-3.1-mini-128k-instruct-Q4_K_M.gguf"
|
| 42 |
|
| 43 |
# When topic modelling column is chosen, change the default visualisation column to the same
|
| 44 |
def change_default_vis_col(in_colnames:List[str]):
|
|
|
|
| 573 |
|
| 574 |
progress(0.1, desc= "Loading model and creating new topic representation")
|
| 575 |
|
| 576 |
+
from funcs.representation_model import create_representation_model, llm_config, chosen_start_tag
|
| 577 |
+
|
| 578 |
representation_model = create_representation_model(representation_type, llm_config, hf_model_name, hf_model_file, chosen_start_tag, high_quality_mode)
|
| 579 |
|
| 580 |
progress(0.3, desc= "Updating existing topics")
|