Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 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
|
|
@@ -14,30 +15,50 @@ MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), MODEL_FILE
|
|
| 14 |
|
| 15 |
_llm = None
|
| 16 |
_downloaded = threading.Event()
|
|
|
|
| 17 |
|
| 18 |
def download_model():
|
|
|
|
| 19 |
if os.path.exists(MODEL_PATH):
|
| 20 |
log.info("Model already downloaded")
|
| 21 |
_downloaded.set()
|
| 22 |
return
|
| 23 |
log.info("Downloading model (7.38 GB)...")
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
threading.Thread(target=download_model, daemon=True).start()
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
@spaces.GPU
|
| 36 |
def generate(messages, max_tokens=1024, temperature=0.7, top_p=0.95):
|
| 37 |
global _llm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
if _llm is None:
|
| 39 |
from llama_cpp import Llama as _Llama
|
| 40 |
-
_downloaded.wait()
|
| 41 |
log.info("Loading model into GPU...")
|
| 42 |
_llm = _Llama(
|
| 43 |
model_path=MODEL_PATH,
|
|
@@ -46,6 +67,7 @@ def generate(messages, max_tokens=1024, temperature=0.7, top_p=0.95):
|
|
| 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,
|
|
@@ -56,22 +78,36 @@ def generate(messages, max_tokens=1024, temperature=0.7, top_p=0.95):
|
|
| 56 |
return output["choices"][0]["message"]["content"].strip()
|
| 57 |
|
| 58 |
def predict(message, history):
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
gr.Markdown("---\nFirst request is slow (~5 min) while the 7.38 GB model downloads. Subsequent requests are fast.")
|
| 77 |
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
import threading
|
| 4 |
+
import traceback
|
| 5 |
import spaces
|
| 6 |
import gradio as gr
|
| 7 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 15 |
|
| 16 |
_llm = None
|
| 17 |
_downloaded = threading.Event()
|
| 18 |
+
_download_error = None
|
| 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 |
+
resume=True
|
| 33 |
+
)
|
| 34 |
+
log.info("Download complete")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
_download_error = str(e)
|
| 37 |
+
log.error(f"Download failed: {e}")
|
| 38 |
+
finally:
|
| 39 |
+
_downloaded.set()
|
| 40 |
|
| 41 |
threading.Thread(target=download_model, daemon=True).start()
|
| 42 |
|
| 43 |
+
@spaces.GPU
|
| 44 |
+
def cuda_test():
|
| 45 |
+
import torch
|
| 46 |
+
return {
|
| 47 |
+
"cuda_available": torch.cuda.is_available(),
|
| 48 |
+
"device_count": torch.cuda.device_count(),
|
| 49 |
+
"device_name": torch.cuda.get_device_name(0) if torch.cuda.is_available() else "N/A",
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
@spaces.GPU
|
| 53 |
def generate(messages, max_tokens=1024, temperature=0.7, top_p=0.95):
|
| 54 |
global _llm
|
| 55 |
+
_downloaded.wait()
|
| 56 |
+
|
| 57 |
+
if _download_error:
|
| 58 |
+
raise RuntimeError(f"Model download failed: {_download_error}")
|
| 59 |
+
|
| 60 |
if _llm is None:
|
| 61 |
from llama_cpp import Llama as _Llama
|
|
|
|
| 62 |
log.info("Loading model into GPU...")
|
| 63 |
_llm = _Llama(
|
| 64 |
model_path=MODEL_PATH,
|
|
|
|
| 67 |
verbose=False
|
| 68 |
)
|
| 69 |
log.info("Model loaded")
|
| 70 |
+
|
| 71 |
log.info(f"Generating (max_tokens={max_tokens}, temp={temperature})")
|
| 72 |
output = _llm.create_chat_completion(
|
| 73 |
messages=messages,
|
|
|
|
| 78 |
return output["choices"][0]["message"]["content"].strip()
|
| 79 |
|
| 80 |
def predict(message, history):
|
| 81 |
+
try:
|
| 82 |
+
messages = []
|
| 83 |
+
for user_msg, assistant_msg in history:
|
| 84 |
+
messages.append({"role": "user", "content": user_msg})
|
| 85 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 86 |
+
messages.append({"role": "user", "content": message})
|
| 87 |
+
return generate(messages)
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return f"Error: {e}\n\n{traceback.format_exc()}"
|
| 90 |
+
|
| 91 |
+
def check_status():
|
| 92 |
+
dl_status = "downloading" if not _downloaded.is_set() else ("done" if not _download_error else f"failed: {_download_error}")
|
| 93 |
+
cuda = cuda_test()
|
| 94 |
+
return f"Model: {dl_status}\nGPU: {cuda}"
|
| 95 |
|
| 96 |
with gr.Blocks(title="Gemma Coder Zero", theme=gr.themes.Soft()) as demo:
|
| 97 |
gr.Markdown("# Gemma 4 12B Coder Zero")
|
| 98 |
gr.Markdown("Powered by llama.cpp on ZeroGPU (RTX Pro 6000 Blackwell)")
|
| 99 |
|
| 100 |
+
with gr.Tabs():
|
| 101 |
+
with gr.TabItem("Chat"):
|
| 102 |
+
gr.ChatInterface(
|
| 103 |
+
fn=predict,
|
| 104 |
+
title="Gemma Coder",
|
| 105 |
+
description="Ask any coding question!"
|
| 106 |
+
)
|
| 107 |
+
with gr.TabItem("Status"):
|
| 108 |
+
status_btn = gr.Button("Check GPU & Model Status")
|
| 109 |
+
status_out = gr.Textbox(label="Status")
|
| 110 |
+
status_btn.click(fn=check_status, outputs=status_out)
|
| 111 |
|
| 112 |
gr.Markdown("---\nFirst request is slow (~5 min) while the 7.38 GB model downloads. Subsequent requests are fast.")
|
| 113 |
|