Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,67 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from llama_cpp import Llama
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
| 12 |
llm = Llama(
|
| 13 |
-
model_path=
|
| 14 |
-
n_ctx=
|
| 15 |
-
n_threads=2,
|
| 16 |
-
n_batch=
|
|
|
|
|
|
|
|
|
|
| 17 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def respond(message, history):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 24 |
|
| 25 |
response_text = ""
|
| 26 |
-
# Setting min_p and temperature for faster but logical output
|
| 27 |
stream = llm(
|
| 28 |
prompt,
|
| 29 |
-
max_tokens=
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
stream=True,
|
| 32 |
-
temperature=0.7
|
| 33 |
)
|
| 34 |
|
| 35 |
for chunk in stream:
|
|
@@ -37,6 +69,21 @@ def respond(message, history):
|
|
| 37 |
response_text += token
|
| 38 |
yield response_text
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if __name__ == "__main__":
|
| 42 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from llama_cpp import Llama
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 7 |
+
# MODEL LOADING (cached to /tmp to skip re-download on warm restarts)
|
| 8 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 9 |
|
| 10 |
+
MODEL_CACHE = "/tmp/qwen_coder.gguf"
|
| 11 |
+
|
| 12 |
+
print("Checking model cache...")
|
| 13 |
+
if not os.path.exists(MODEL_CACHE):
|
| 14 |
+
print("Downloading Q4_K_M model (first run only)...")
|
| 15 |
+
model_path = hf_hub_download(
|
| 16 |
+
repo_id="Qwen/Qwen2.5-Coder-1.5B-Instruct-GGUF",
|
| 17 |
+
filename="qwen2.5-coder-1.5b-instruct-q4_k_m.gguf", # ~2x faster than Q8
|
| 18 |
+
)
|
| 19 |
+
os.rename(model_path, MODEL_CACHE)
|
| 20 |
+
print("Model cached.")
|
| 21 |
+
else:
|
| 22 |
+
print("Model found in cache, skipping download.")
|
| 23 |
|
| 24 |
+
print("Loading model into memory...")
|
| 25 |
llm = Llama(
|
| 26 |
+
model_path=MODEL_CACHE,
|
| 27 |
+
n_ctx=512, # Reduced from 1024 β context scaling costs CPU cycles
|
| 28 |
+
n_threads=2, # Matches HuggingFace free tier vCPU count
|
| 29 |
+
n_batch=128, # Lower batch = less memory pressure on CPU
|
| 30 |
+
n_gpu_layers=0, # No GPU on free tier
|
| 31 |
+
use_mlock=False, # Don't lock memory on free tier
|
| 32 |
+
verbose=False, # Skip logging overhead
|
| 33 |
)
|
| 34 |
+
print("Model ready!")
|
| 35 |
+
|
| 36 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 37 |
+
# INFERENCE
|
| 38 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 39 |
+
|
| 40 |
+
SYSTEM_PROMPT = "You are a helpful Minecraft coding assistant. You help with Minecraft plugin development, mods, datapacks, and commands. Be concise and practical."
|
| 41 |
|
| 42 |
def respond(message, history):
|
| 43 |
+
# Cap history to last 3 exchanges to prevent prompt bloat
|
| 44 |
+
history = history[-3:] if history else []
|
| 45 |
+
|
| 46 |
+
# Build prompt in ChatML format
|
| 47 |
+
prompt = f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n"
|
| 48 |
+
for exchange in history:
|
| 49 |
+
user_msg = exchange[0] if isinstance(exchange, (list, tuple)) else exchange.get("role", "")
|
| 50 |
+
asst_msg = exchange[1] if isinstance(exchange, (list, tuple)) else ""
|
| 51 |
+
if user_msg:
|
| 52 |
+
prompt += f"<|im_start|>user\n{user_msg}<|im_end|>\n"
|
| 53 |
+
if asst_msg:
|
| 54 |
+
prompt += f"<|im_start|>assistant\n{asst_msg}<|im_end|>\n"
|
| 55 |
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 56 |
|
| 57 |
response_text = ""
|
|
|
|
| 58 |
stream = llm(
|
| 59 |
prompt,
|
| 60 |
+
max_tokens=128, # Halves worst-case wait vs 256
|
| 61 |
+
temperature=0.7,
|
| 62 |
+
repeat_penalty=1.1, # Prevents repetition loops that waste tokens
|
| 63 |
+
stop=["<|im_end|>", "<|im_start|>"],
|
| 64 |
stream=True,
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
for chunk in stream:
|
|
|
|
| 69 |
response_text += token
|
| 70 |
yield response_text
|
| 71 |
|
| 72 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 73 |
+
# UI
|
| 74 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 75 |
+
|
| 76 |
+
demo = gr.ChatInterface(
|
| 77 |
+
fn=respond,
|
| 78 |
+
title="βοΈ Minecraft Coding Assistant",
|
| 79 |
+
description="Powered by Qwen2.5-Coder 1.5B Β· Q4_K_M Β· CPU optimized",
|
| 80 |
+
examples=[
|
| 81 |
+
"How do I create a custom recipe in a datapack?",
|
| 82 |
+
"Write a Spigot plugin that teleports a player on command",
|
| 83 |
+
"What's the syntax for a /execute command with conditions?",
|
| 84 |
+
],
|
| 85 |
+
cache_examples=False,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
if __name__ == "__main__":
|
| 89 |
demo.launch()
|