Update app.py
Browse files
app.py
CHANGED
|
@@ -9,11 +9,32 @@ from pathlib import Path
|
|
| 9 |
|
| 10 |
from transformers import pipeline, set_seed
|
| 11 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 12 |
from datasets import load_dataset
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# 1. Initialize LLM
|
| 15 |
print("Loading BioMistral LLM...")
|
| 16 |
-
llm =
|
| 17 |
|
| 18 |
# 2. Load Datasets
|
| 19 |
print("Loading datasets...")
|
|
@@ -78,15 +99,6 @@ Question:
|
|
| 78 |
@spaces.GPU(duration=60)
|
| 79 |
def generate_answer(prompt: str, max_new_tokens: int = 80) -> str:
|
| 80 |
set_seed(42)
|
| 81 |
-
global llm
|
| 82 |
-
if llm is None:
|
| 83 |
-
print("Initializing BioMistral on ZeroGPU...")
|
| 84 |
-
llm = pipeline(
|
| 85 |
-
'text-generation',
|
| 86 |
-
model='BioMistral/BioMistral-7B',
|
| 87 |
-
torch_dtype=torch.bfloat16,
|
| 88 |
-
device_map="auto"
|
| 89 |
-
)
|
| 90 |
if llm.model.config.pad_token_id is None:
|
| 91 |
llm.model.config.pad_token_id = llm.model.config.eos_token_id
|
| 92 |
|
|
|
|
| 9 |
|
| 10 |
from transformers import pipeline, set_seed
|
| 11 |
from sentence_transformers import SentenceTransformer
|
| 12 |
+
import transformers.utils.import_utils as transformers_import_utils
|
| 13 |
from datasets import load_dataset
|
| 14 |
|
| 15 |
+
# Transformers 5 may query the CUDA stream while preparing CPU attention
|
| 16 |
+
# masks (used internally when we call embedding_model.encode(...) on CPU).
|
| 17 |
+
# In the GPU-less ZeroGPU parent process, that stray CUDA runtime call
|
| 18 |
+
# leaves CUDA in a state that then breaks the next forked @spaces.GPU
|
| 19 |
+
# worker's attempt to initialize its dynamically assigned GPU, causing
|
| 20 |
+
# "RuntimeError: No CUDA GPUs are available" even though the code never
|
| 21 |
+
# explicitly touches CUDA outside the decorated function.
|
| 22 |
+
# Fix: skip only that query in the ZeroGPU parent; real GPU workers keep
|
| 23 |
+
# the original Transformers behavior once CUDA_VISIBLE_DEVICES is set.
|
| 24 |
+
# See: https://huggingface.co/spaces/zero-gpu-explorers/README/discussions/181
|
| 25 |
+
if hasattr(transformers_import_utils, "is_cuda_stream_capturing"):
|
| 26 |
+
_original_is_cuda_stream_capturing = transformers_import_utils.is_cuda_stream_capturing
|
| 27 |
+
|
| 28 |
+
def _zerogpu_safe_is_cuda_stream_capturing(*args, **kwargs):
|
| 29 |
+
if os.getenv("SPACES_ZERO_GPU") == "1" and not os.getenv("CUDA_VISIBLE_DEVICES"):
|
| 30 |
+
return False
|
| 31 |
+
return _original_is_cuda_stream_capturing(*args, **kwargs)
|
| 32 |
+
|
| 33 |
+
transformers_import_utils.is_cuda_stream_capturing = _zerogpu_safe_is_cuda_stream_capturing
|
| 34 |
+
|
| 35 |
# 1. Initialize LLM
|
| 36 |
print("Loading BioMistral LLM...")
|
| 37 |
+
llm = pipeline('text-generation', model='BioMistral/BioMistral-7B', torch_dtype=torch.bfloat16, device_map="auto")
|
| 38 |
|
| 39 |
# 2. Load Datasets
|
| 40 |
print("Loading datasets...")
|
|
|
|
| 99 |
@spaces.GPU(duration=60)
|
| 100 |
def generate_answer(prompt: str, max_new_tokens: int = 80) -> str:
|
| 101 |
set_seed(42)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
if llm.model.config.pad_token_id is None:
|
| 103 |
llm.model.config.pad_token_id = llm.model.config.eos_token_id
|
| 104 |
|