Spaces:
Sleeping
Sleeping
Donald Winkelman
commited on
Commit
·
f362c64
1
Parent(s):
6dd3836
Updating Side-By-Side Space
Browse files- app.py +109 -6
- model_paths.py +12 -0
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -4,6 +4,26 @@ import time
|
|
| 4 |
import sys
|
| 5 |
from datetime import datetime
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Try to import llama_cpp
|
| 8 |
try:
|
| 9 |
from llama_cpp import Llama
|
|
@@ -14,27 +34,110 @@ except ImportError:
|
|
| 14 |
LLAMA_CPP_AVAILABLE = False
|
| 15 |
print("llama_cpp is not available. Running in fallback mode.")
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
BASE_MODEL_PATH =
|
| 19 |
-
NOVEL_MODEL_PATH =
|
| 20 |
|
| 21 |
# Initialize models
|
| 22 |
base_model = None
|
| 23 |
novel_model = None
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def load_models(progress=None):
|
| 27 |
"""Load the llama.cpp models"""
|
| 28 |
-
global base_model, novel_model
|
| 29 |
|
| 30 |
if not LLAMA_CPP_AVAILABLE:
|
| 31 |
print("llama_cpp not available, cannot load models")
|
| 32 |
return False
|
| 33 |
|
| 34 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Load base model
|
| 36 |
if progress is not None:
|
| 37 |
-
progress(0.
|
| 38 |
|
| 39 |
print(f"Loading base model from {BASE_MODEL_PATH}")
|
| 40 |
base_model = Llama(
|
|
@@ -45,7 +148,7 @@ def load_models(progress=None):
|
|
| 45 |
|
| 46 |
# Load novel model
|
| 47 |
if progress is not None:
|
| 48 |
-
progress(0.
|
| 49 |
|
| 50 |
print(f"Loading novel model from {NOVEL_MODEL_PATH}")
|
| 51 |
novel_model = Llama(
|
|
|
|
| 4 |
import sys
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
+
# Import model configuration
|
| 8 |
+
try:
|
| 9 |
+
from model_paths import (
|
| 10 |
+
BASE_MODEL_REPO,
|
| 11 |
+
NOVEL_MODEL_REPO,
|
| 12 |
+
BASE_MODEL_FILENAME,
|
| 13 |
+
NOVEL_MODEL_FILENAME,
|
| 14 |
+
MODELS_DIR
|
| 15 |
+
)
|
| 16 |
+
except ImportError:
|
| 17 |
+
# Default values if import fails
|
| 18 |
+
BASE_MODEL_REPO = "unsloth/Qwen3-4B-GGUF"
|
| 19 |
+
NOVEL_MODEL_REPO = "mradermacher/Clinician-Note-2.0a-i1-GGUF"
|
| 20 |
+
BASE_MODEL_FILENAME = "Qwen3-4B-Q4_K_M.gguf"
|
| 21 |
+
NOVEL_MODEL_FILENAME = "Clinician-Note-2.0a.i1-Q4_K_M.gguf"
|
| 22 |
+
MODELS_DIR = "models"
|
| 23 |
+
|
| 24 |
+
# Create models directory
|
| 25 |
+
os.makedirs(MODELS_DIR, exist_ok=True)
|
| 26 |
+
|
| 27 |
# Try to import llama_cpp
|
| 28 |
try:
|
| 29 |
from llama_cpp import Llama
|
|
|
|
| 34 |
LLAMA_CPP_AVAILABLE = False
|
| 35 |
print("llama_cpp is not available. Running in fallback mode.")
|
| 36 |
|
| 37 |
+
# Set up initial model paths
|
| 38 |
+
BASE_MODEL_PATH = os.path.join(MODELS_DIR, BASE_MODEL_FILENAME)
|
| 39 |
+
NOVEL_MODEL_PATH = os.path.join(MODELS_DIR, NOVEL_MODEL_FILENAME)
|
| 40 |
|
| 41 |
# Initialize models
|
| 42 |
base_model = None
|
| 43 |
novel_model = None
|
| 44 |
|
| 45 |
|
| 46 |
+
def download_model(model_repo_id, model_filename, progress=None, progress_start=0.0, progress_end=0.1):
|
| 47 |
+
"""Download a model from Hugging Face Hub if it doesn't exist locally"""
|
| 48 |
+
try:
|
| 49 |
+
from huggingface_hub import hf_hub_download
|
| 50 |
+
import os
|
| 51 |
+
|
| 52 |
+
# Create models directory if it doesn't exist
|
| 53 |
+
os.makedirs("models", exist_ok=True)
|
| 54 |
+
|
| 55 |
+
# Define the local path for the model
|
| 56 |
+
local_path = os.path.join("models", model_filename)
|
| 57 |
+
|
| 58 |
+
# Check if model already exists locally
|
| 59 |
+
if os.path.exists(local_path):
|
| 60 |
+
print(f"Model {model_filename} already exists at {local_path}")
|
| 61 |
+
return local_path
|
| 62 |
+
|
| 63 |
+
# If progress is provided, update it
|
| 64 |
+
if progress is not None:
|
| 65 |
+
progress(progress_start, desc=f"Downloading {model_filename}... This may take a while")
|
| 66 |
+
|
| 67 |
+
print(f"Downloading {model_filename} from Hugging Face Hub...")
|
| 68 |
+
|
| 69 |
+
# Download the model from Hugging Face
|
| 70 |
+
downloaded_path = hf_hub_download(
|
| 71 |
+
repo_id=model_repo_id,
|
| 72 |
+
filename=model_filename,
|
| 73 |
+
local_dir="models",
|
| 74 |
+
local_dir_use_symlinks=False
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# If progress is provided, update it
|
| 78 |
+
if progress is not None:
|
| 79 |
+
progress(progress_end, desc=f"Downloaded {model_filename}")
|
| 80 |
+
|
| 81 |
+
print(f"Model downloaded to {downloaded_path}")
|
| 82 |
+
return downloaded_path
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Error downloading model {model_filename}: {str(e)}")
|
| 86 |
+
if progress is not None:
|
| 87 |
+
progress(progress_end, desc=f"Error downloading model: {str(e)}")
|
| 88 |
+
return None
|
| 89 |
+
|
| 90 |
+
|
| 91 |
def load_models(progress=None):
|
| 92 |
"""Load the llama.cpp models"""
|
| 93 |
+
global base_model, novel_model, BASE_MODEL_PATH, NOVEL_MODEL_PATH
|
| 94 |
|
| 95 |
if not LLAMA_CPP_AVAILABLE:
|
| 96 |
print("llama_cpp not available, cannot load models")
|
| 97 |
return False
|
| 98 |
|
| 99 |
try:
|
| 100 |
+
# Download base model if needed
|
| 101 |
+
base_model_repo = "unsloth/Qwen3-4B-GGUF"
|
| 102 |
+
base_model_filename = "Qwen3-4B-Q4_K_M.gguf"
|
| 103 |
+
|
| 104 |
+
if progress is not None:
|
| 105 |
+
progress(0.1, desc="Checking for base model...")
|
| 106 |
+
|
| 107 |
+
base_model_path = download_model(
|
| 108 |
+
base_model_repo,
|
| 109 |
+
base_model_filename,
|
| 110 |
+
progress,
|
| 111 |
+
0.1, 0.3
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
if not base_model_path:
|
| 115 |
+
raise Exception(f"Failed to download base model {base_model_filename}")
|
| 116 |
+
|
| 117 |
+
BASE_MODEL_PATH = base_model_path
|
| 118 |
+
|
| 119 |
+
# Download novel model if needed
|
| 120 |
+
novel_model_repo = "mradermacher/Clinician-Note-2.0a-i1-GGUF"
|
| 121 |
+
novel_model_filename = "Clinician-Note-2.0a.i1-Q4_K_M.gguf"
|
| 122 |
+
|
| 123 |
+
if progress is not None:
|
| 124 |
+
progress(0.4, desc="Checking for novel model...")
|
| 125 |
+
|
| 126 |
+
novel_model_path = download_model(
|
| 127 |
+
novel_model_repo,
|
| 128 |
+
novel_model_filename,
|
| 129 |
+
progress,
|
| 130 |
+
0.4, 0.6
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
if not novel_model_path:
|
| 134 |
+
raise Exception(f"Failed to download novel model {novel_model_filename}")
|
| 135 |
+
|
| 136 |
+
NOVEL_MODEL_PATH = novel_model_path
|
| 137 |
+
|
| 138 |
# Load base model
|
| 139 |
if progress is not None:
|
| 140 |
+
progress(0.7, desc="Loading base model... This may take a few minutes")
|
| 141 |
|
| 142 |
print(f"Loading base model from {BASE_MODEL_PATH}")
|
| 143 |
base_model = Llama(
|
|
|
|
| 148 |
|
| 149 |
# Load novel model
|
| 150 |
if progress is not None:
|
| 151 |
+
progress(0.9, desc="Loading novel model... This may take a few minutes")
|
| 152 |
|
| 153 |
print(f"Loading novel model from {NOVEL_MODEL_PATH}")
|
| 154 |
novel_model = Llama(
|
model_paths.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Model configuration file
|
| 2 |
+
|
| 3 |
+
# Hugging Face Hub repository IDs
|
| 4 |
+
BASE_MODEL_REPO = "unsloth/Qwen3-4B-GGUF"
|
| 5 |
+
NOVEL_MODEL_REPO = "mradermacher/Clinician-Note-2.0a-i1-GGUF"
|
| 6 |
+
|
| 7 |
+
# Model filenames
|
| 8 |
+
BASE_MODEL_FILENAME = "Qwen3-4B-Q4_K_M.gguf"
|
| 9 |
+
NOVEL_MODEL_FILENAME = "Clinician-Note-2.0a.i1-Q4_K_M.gguf"
|
| 10 |
+
|
| 11 |
+
# Local directory for models
|
| 12 |
+
MODELS_DIR = "models"
|
requirements.txt
CHANGED
|
@@ -5,4 +5,6 @@ torch==2.7.0
|
|
| 5 |
accelerate==1.6.0
|
| 6 |
sentencepiece==0.2.0
|
| 7 |
protobuf==6.30.2
|
| 8 |
-
llama-cpp-python==0.3.9
|
|
|
|
|
|
|
|
|
| 5 |
accelerate==1.6.0
|
| 6 |
sentencepiece==0.2.0
|
| 7 |
protobuf==6.30.2
|
| 8 |
+
llama-cpp-python==0.3.9
|
| 9 |
+
requests>=2.31.0
|
| 10 |
+
tqdm>=4.65.0
|