Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,17 +1,11 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import logging
|
| 3 |
import traceback
|
| 4 |
import spaces
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
-
# Add PyTorch's bundled CUDA libs to library path BEFORE importing llama_cpp
|
| 9 |
-
import torch
|
| 10 |
-
_torch_lib = os.path.join(os.path.dirname(torch.__file__), "lib")
|
| 11 |
-
_env_ld = os.environ.get("LD_LIBRARY_PATH", "")
|
| 12 |
-
if _torch_lib not in _env_ld:
|
| 13 |
-
os.environ["LD_LIBRARY_PATH"] = _torch_lib + ":" + _env_ld
|
| 14 |
-
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
| 16 |
log = logging.getLogger(__name__)
|
| 17 |
|
|
@@ -28,8 +22,28 @@ if not os.path.exists(MODEL_PATH):
|
|
| 28 |
)
|
| 29 |
log.info("Download complete")
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
@spaces.GPU
|
| 32 |
def cuda_test():
|
|
|
|
| 33 |
return {
|
| 34 |
"cuda_available": torch.cuda.is_available(),
|
| 35 |
"device_count": torch.cuda.device_count(),
|
|
@@ -39,6 +53,8 @@ def cuda_test():
|
|
| 39 |
@spaces.GPU
|
| 40 |
def generate(messages, max_tokens=1024, temperature=0.7, top_p=0.95):
|
| 41 |
try:
|
|
|
|
|
|
|
| 42 |
log.info("Importing llama_cpp...")
|
| 43 |
from llama_cpp import Llama as _Llama
|
| 44 |
log.info("Import OK, loading model...")
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
| 3 |
import logging
|
| 4 |
import traceback
|
| 5 |
import spaces
|
| 6 |
import gradio as gr
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
| 10 |
log = logging.getLogger(__name__)
|
| 11 |
|
|
|
|
| 22 |
)
|
| 23 |
log.info("Download complete")
|
| 24 |
|
| 25 |
+
def _setup_cuda_paths():
|
| 26 |
+
paths = [
|
| 27 |
+
"/usr/local/lib/python3.12/site-packages/nvidia/cuda_runtime/lib",
|
| 28 |
+
]
|
| 29 |
+
# Try to find nvidia packages dynamically
|
| 30 |
+
for mod_name in ["nvidia.cuda_runtime"]:
|
| 31 |
+
try:
|
| 32 |
+
mod = __import__(mod_name, fromlist=["__file__"])
|
| 33 |
+
lib_dir = os.path.join(os.path.dirname(mod.__file__), "lib")
|
| 34 |
+
if os.path.exists(lib_dir):
|
| 35 |
+
paths.append(lib_dir)
|
| 36 |
+
except ImportError:
|
| 37 |
+
pass
|
| 38 |
+
current = os.environ.get("LD_LIBRARY_PATH", "")
|
| 39 |
+
for p in paths:
|
| 40 |
+
if os.path.exists(p) and p not in current:
|
| 41 |
+
os.environ["LD_LIBRARY_PATH"] = p + ":" + current
|
| 42 |
+
log.info(f"Added {p} to LD_LIBRARY_PATH")
|
| 43 |
+
|
| 44 |
@spaces.GPU
|
| 45 |
def cuda_test():
|
| 46 |
+
import torch
|
| 47 |
return {
|
| 48 |
"cuda_available": torch.cuda.is_available(),
|
| 49 |
"device_count": torch.cuda.device_count(),
|
|
|
|
| 53 |
@spaces.GPU
|
| 54 |
def generate(messages, max_tokens=1024, temperature=0.7, top_p=0.95):
|
| 55 |
try:
|
| 56 |
+
_setup_cuda_paths()
|
| 57 |
+
log.info(f"LD_LIBRARY_PATH={os.environ.get('LD_LIBRARY_PATH', '')[:200]}")
|
| 58 |
log.info("Importing llama_cpp...")
|
| 59 |
from llama_cpp import Llama as _Llama
|
| 60 |
log.info("Import OK, loading model...")
|