Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import logging
|
| 3 |
+
import threading
|
| 4 |
+
import spaces
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
from llama_cpp import Llama
|
| 8 |
+
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
log = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
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 |
+
_llm = None
|
| 17 |
+
_downloaded = threading.Event()
|
| 18 |
+
|
| 19 |
+
def download_model():
|
| 20 |
+
if os.path.exists(MODEL_PATH):
|
| 21 |
+
log.info("Model already downloaded")
|
| 22 |
+
_downloaded.set()
|
| 23 |
+
return
|
| 24 |
+
log.info("Downloading model (7.38 GB)...")
|
| 25 |
+
hf_hub_download(
|
| 26 |
+
repo_id=MODEL_REPO,
|
| 27 |
+
filename=MODEL_FILE,
|
| 28 |
+
local_dir=os.path.dirname(os.path.abspath(__file__)),
|
| 29 |
+
resume=True
|
| 30 |
+
)
|
| 31 |
+
log.info("Download complete")
|
| 32 |
+
_downloaded.set()
|
| 33 |
+
|
| 34 |
+
threading.Thread(target=download_model, daemon=True).start()
|
| 35 |
+
|
| 36 |
+
@spaces.GPU
|
| 37 |
+
def generate(messages, max_tokens=1024, temperature=0.7, top_p=0.95):
|
| 38 |
+
global _llm
|
| 39 |
+
if _llm is None:
|
| 40 |
+
_downloaded.wait()
|
| 41 |
+
log.info("Loading model into GPU...")
|
| 42 |
+
_llm = Llama(
|
| 43 |
+
model_path=MODEL_PATH,
|
| 44 |
+
n_gpu_layers=-1,
|
| 45 |
+
n_ctx=8192,
|
| 46 |
+
verbose=False
|
| 47 |
+
)
|
| 48 |
+
log.info("Model loaded")
|
| 49 |
+
log.info(f"Generating (max_tokens={max_tokens}, temp={temperature})")
|
| 50 |
+
output = _llm.create_chat_completion(
|
| 51 |
+
messages=messages,
|
| 52 |
+
max_tokens=max_tokens,
|
| 53 |
+
temperature=temperature,
|
| 54 |
+
top_p=top_p,
|
| 55 |
+
)
|
| 56 |
+
return output["choices"][0]["message"]["content"].strip()
|
| 57 |
+
|
| 58 |
+
def predict(message, history):
|
| 59 |
+
messages = []
|
| 60 |
+
for user_msg, assistant_msg in history:
|
| 61 |
+
messages.append({"role": "user", "content": user_msg})
|
| 62 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 63 |
+
messages.append({"role": "user", "content": message})
|
| 64 |
+
return generate(messages)
|
| 65 |
+
|
| 66 |
+
with gr.Blocks(title="Gemma Coder Zero", theme=gr.themes.Soft()) as demo:
|
| 67 |
+
gr.Markdown("# Gemma 4 12B Coder Zero")
|
| 68 |
+
gr.Markdown("Powered by llama.cpp on ZeroGPU (RTX Pro 6000 Blackwell)")
|
| 69 |
+
|
| 70 |
+
gr.ChatInterface(
|
| 71 |
+
fn=predict,
|
| 72 |
+
title="Gemma Coder",
|
| 73 |
+
description="Ask any coding question!"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
gr.Markdown("---\nFirst request is slow (~5 min) while the 7.38 GB model downloads. Subsequent requests are fast.")
|
| 77 |
+
|
| 78 |
+
demo.queue(default_concurrency_limit=1)
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|