Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -13,52 +13,30 @@ MODEL_REPO = "sakamakismile/gemma-4-12B-coder-fable5-composer2.5-GGUF"
|
|
| 13 |
MODEL_FILE = "gemma-4-12B-coder-fable5-composer2.5-Q4_K_M.gguf"
|
| 14 |
MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), MODEL_FILE)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def download_model():
|
| 21 |
-
global _download_error
|
| 22 |
-
if os.path.exists(MODEL_PATH):
|
| 23 |
-
log.info("Model already downloaded")
|
| 24 |
-
_downloaded.set()
|
| 25 |
-
return
|
| 26 |
-
log.info("Downloading model (7.38 GB)...")
|
| 27 |
-
try:
|
| 28 |
hf_hub_download(
|
| 29 |
repo_id=MODEL_REPO,
|
| 30 |
filename=MODEL_FILE,
|
| 31 |
local_dir=os.path.dirname(os.path.abspath(__file__)),
|
| 32 |
)
|
| 33 |
log.info("Download complete")
|
| 34 |
-
except Exception as e:
|
| 35 |
-
_download_error = str(e)
|
| 36 |
-
log.error(f"Download failed: {e}")
|
| 37 |
-
finally:
|
| 38 |
-
_downloaded.set()
|
| 39 |
|
| 40 |
-
threading.Thread(target=
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
import torch
|
| 45 |
-
return {
|
| 46 |
-
"cuda_available": torch.cuda.is_available(),
|
| 47 |
-
"device_count": torch.cuda.device_count(),
|
| 48 |
-
"device_name": torch.cuda.get_device_name(0) if torch.cuda.is_available() else "N/A",
|
| 49 |
-
}
|
| 50 |
|
| 51 |
-
|
| 52 |
-
def generate(messages, max_tokens=1024, temperature=0.7, top_p=0.95):
|
| 53 |
global _llm
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
if _llm is None:
|
| 60 |
-
from llama_cpp import Llama as _Llama
|
| 61 |
log.info("Loading model into GPU...")
|
|
|
|
| 62 |
_llm = _Llama(
|
| 63 |
model_path=MODEL_PATH,
|
| 64 |
n_gpu_layers=-1,
|
|
@@ -66,9 +44,22 @@ def generate(messages, max_tokens=1024, temperature=0.7, top_p=0.95):
|
|
| 66 |
verbose=False
|
| 67 |
)
|
| 68 |
log.info("Model loaded")
|
|
|
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
log.info(f"Generating (max_tokens={max_tokens}, temp={temperature})")
|
| 71 |
-
output =
|
| 72 |
messages=messages,
|
| 73 |
max_tokens=max_tokens,
|
| 74 |
temperature=temperature,
|
|
@@ -78,6 +69,8 @@ def generate(messages, max_tokens=1024, temperature=0.7, top_p=0.95):
|
|
| 78 |
|
| 79 |
def predict(message, history):
|
| 80 |
try:
|
|
|
|
|
|
|
| 81 |
messages = []
|
| 82 |
for user_msg, assistant_msg in history:
|
| 83 |
messages.append({"role": "user", "content": user_msg})
|
|
@@ -85,12 +78,13 @@ def predict(message, history):
|
|
| 85 |
messages.append({"role": "user", "content": message})
|
| 86 |
return generate(messages)
|
| 87 |
except Exception as e:
|
| 88 |
-
return f"Error: {
|
| 89 |
|
| 90 |
def check_status():
|
| 91 |
-
|
|
|
|
| 92 |
cuda = cuda_test()
|
| 93 |
-
return f"Model: {
|
| 94 |
|
| 95 |
with gr.Blocks(title="Gemma Coder Zero", theme=gr.themes.Soft()) as demo:
|
| 96 |
gr.Markdown("# Gemma 4 12B Coder Zero")
|
|
|
|
| 13 |
MODEL_FILE = "gemma-4-12B-coder-fable5-composer2.5-Q4_K_M.gguf"
|
| 14 |
MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), MODEL_FILE)
|
| 15 |
|
| 16 |
+
def ensure_model():
|
| 17 |
+
if not os.path.exists(MODEL_PATH):
|
| 18 |
+
log.info("Downloading model (7.38 GB)...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
hf_hub_download(
|
| 20 |
repo_id=MODEL_REPO,
|
| 21 |
filename=MODEL_FILE,
|
| 22 |
local_dir=os.path.dirname(os.path.abspath(__file__)),
|
| 23 |
)
|
| 24 |
log.info("Download complete")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
threading.Thread(target=ensure_model, daemon=True).start()
|
| 27 |
|
| 28 |
+
_llm = None
|
| 29 |
+
_llm_lock = threading.Lock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
def load_model():
|
|
|
|
| 32 |
global _llm
|
| 33 |
+
if _llm is not None:
|
| 34 |
+
return _llm
|
| 35 |
+
with _llm_lock:
|
| 36 |
+
if _llm is not None:
|
| 37 |
+
return _llm
|
|
|
|
|
|
|
| 38 |
log.info("Loading model into GPU...")
|
| 39 |
+
from llama_cpp import Llama as _Llama
|
| 40 |
_llm = _Llama(
|
| 41 |
model_path=MODEL_PATH,
|
| 42 |
n_gpu_layers=-1,
|
|
|
|
| 44 |
verbose=False
|
| 45 |
)
|
| 46 |
log.info("Model loaded")
|
| 47 |
+
return _llm
|
| 48 |
|
| 49 |
+
@spaces.GPU
|
| 50 |
+
def cuda_test():
|
| 51 |
+
import torch
|
| 52 |
+
return {
|
| 53 |
+
"cuda_available": torch.cuda.is_available(),
|
| 54 |
+
"device_count": torch.cuda.device_count(),
|
| 55 |
+
"device_name": torch.cuda.get_device_name(0) if torch.cuda.is_available() else "N/A",
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
@spaces.GPU
|
| 59 |
+
def generate(messages, max_tokens=1024, temperature=0.7, top_p=0.95):
|
| 60 |
+
llm = load_model()
|
| 61 |
log.info(f"Generating (max_tokens={max_tokens}, temp={temperature})")
|
| 62 |
+
output = llm.create_chat_completion(
|
| 63 |
messages=messages,
|
| 64 |
max_tokens=max_tokens,
|
| 65 |
temperature=temperature,
|
|
|
|
| 69 |
|
| 70 |
def predict(message, history):
|
| 71 |
try:
|
| 72 |
+
if not os.path.exists(MODEL_PATH):
|
| 73 |
+
return "Model is still downloading... Please wait ~5 minutes and try again."
|
| 74 |
messages = []
|
| 75 |
for user_msg, assistant_msg in history:
|
| 76 |
messages.append({"role": "user", "content": user_msg})
|
|
|
|
| 78 |
messages.append({"role": "user", "content": message})
|
| 79 |
return generate(messages)
|
| 80 |
except Exception as e:
|
| 81 |
+
return f"Error: {traceback.format_exc()}"
|
| 82 |
|
| 83 |
def check_status():
|
| 84 |
+
exists = os.path.exists(MODEL_PATH)
|
| 85 |
+
size = os.path.getsize(MODEL_PATH) if exists else 0
|
| 86 |
cuda = cuda_test()
|
| 87 |
+
return f"Model file: {'exists' if exists else 'missing'} ({size/1e9:.1f} GB)\nGPU: {cuda}"
|
| 88 |
|
| 89 |
with gr.Blocks(title="Gemma Coder Zero", theme=gr.themes.Soft()) as demo:
|
| 90 |
gr.Markdown("# Gemma 4 12B Coder Zero")
|