Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
-
import threading
|
| 4 |
import traceback
|
| 5 |
import spaces
|
| 6 |
import gradio as gr
|
|
@@ -13,38 +12,14 @@ 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 |
-
|
| 21 |
-
|
| 22 |
-
|
| 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,
|
| 43 |
-
n_ctx=8192,
|
| 44 |
-
verbose=False
|
| 45 |
-
)
|
| 46 |
-
log.info("Model loaded")
|
| 47 |
-
return _llm
|
| 48 |
|
| 49 |
@spaces.GPU
|
| 50 |
def cuda_test():
|
|
@@ -57,28 +32,40 @@ def cuda_test():
|
|
| 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,
|
| 66 |
-
top_p=top_p,
|
| 67 |
-
)
|
| 68 |
-
return output["choices"][0]["message"]["content"].strip()
|
| 69 |
-
|
| 70 |
-
def predict(message, history):
|
| 71 |
try:
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
def check_status():
|
| 84 |
exists = os.path.exists(MODEL_PATH)
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
|
|
|
| 3 |
import traceback
|
| 4 |
import spaces
|
| 5 |
import gradio as gr
|
|
|
|
| 12 |
MODEL_FILE = "gemma-4-12B-coder-fable5-composer2.5-Q4_K_M.gguf"
|
| 13 |
MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), MODEL_FILE)
|
| 14 |
|
| 15 |
+
if not os.path.exists(MODEL_PATH):
|
| 16 |
+
log.info("Downloading model (7.38 GB)...")
|
| 17 |
+
hf_hub_download(
|
| 18 |
+
repo_id=MODEL_REPO,
|
| 19 |
+
filename=MODEL_FILE,
|
| 20 |
+
local_dir=os.path.dirname(os.path.abspath(__file__)),
|
| 21 |
+
)
|
| 22 |
+
log.info("Download complete")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
@spaces.GPU
|
| 25 |
def cuda_test():
|
|
|
|
| 32 |
|
| 33 |
@spaces.GPU
|
| 34 |
def generate(messages, max_tokens=1024, temperature=0.7, top_p=0.95):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
+
log.info("Importing llama_cpp...")
|
| 37 |
+
from llama_cpp import Llama as _Llama
|
| 38 |
+
log.info("Import OK, loading model...")
|
| 39 |
+
llm = _Llama(
|
| 40 |
+
model_path=MODEL_PATH,
|
| 41 |
+
n_gpu_layers=-1,
|
| 42 |
+
n_ctx=8192,
|
| 43 |
+
verbose=False
|
| 44 |
+
)
|
| 45 |
+
log.info("Model loaded, generating...")
|
| 46 |
+
output = llm.create_chat_completion(
|
| 47 |
+
messages=messages,
|
| 48 |
+
max_tokens=max_tokens,
|
| 49 |
+
temperature=temperature,
|
| 50 |
+
top_p=top_p,
|
| 51 |
+
)
|
| 52 |
+
result = output["choices"][0]["message"]["content"].strip()
|
| 53 |
+
log.info("Generation complete")
|
| 54 |
+
return result
|
| 55 |
except Exception as e:
|
| 56 |
+
err = f"GPU Error: {type(e).__name__}: {e}\n{traceback.format_exc()}"
|
| 57 |
+
log.error(err)
|
| 58 |
+
return err
|
| 59 |
+
|
| 60 |
+
def predict(message, history):
|
| 61 |
+
if not os.path.exists(MODEL_PATH):
|
| 62 |
+
return "Model is still downloading... Please wait ~5 minutes and try again."
|
| 63 |
+
messages = []
|
| 64 |
+
for user_msg, assistant_msg in history:
|
| 65 |
+
messages.append({"role": "user", "content": user_msg})
|
| 66 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 67 |
+
messages.append({"role": "user", "content": message})
|
| 68 |
+
return generate(messages)
|
| 69 |
|
| 70 |
def check_status():
|
| 71 |
exists = os.path.exists(MODEL_PATH)
|