Spaces:
Runtime error
Runtime error
Fix: auto-download model from HF model repo at startup
Browse files
app.py
CHANGED
|
@@ -1,40 +1,69 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from llama_cpp import Llama
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
-
print("Loading Llama-2 model...")
|
| 9 |
llm = None
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
else:
|
| 14 |
-
print(
|
| 15 |
|
|
|
|
| 16 |
def chat(message, history):
|
| 17 |
if llm is None:
|
| 18 |
-
return
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
context = ""
|
| 22 |
-
for user_msg, bot_msg in history[-5:]:
|
| 23 |
context += f"[INST] {user_msg} [/INST] {bot_msg} </s>"
|
| 24 |
-
|
| 25 |
-
prompt =
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
demo = gr.ChatInterface(
|
| 33 |
fn=chat,
|
| 34 |
title="Llama-2-7B Chatbot",
|
| 35 |
description=(
|
| 36 |
-
"
|
| 37 |
-
"
|
|
|
|
| 38 |
),
|
| 39 |
theme=gr.themes.Soft(
|
| 40 |
primary_hue="blue",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from llama_cpp import Llama
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
MODEL_REPO = "d-e-e-k-11/llama-2-7b-chat-ggml"
|
| 7 |
+
MODEL_FILE = "llama-2-7b-chat.ggmlv3.q2_K.bin"
|
| 8 |
+
LOCAL_PATH = "/tmp/llama-model.bin"
|
| 9 |
|
| 10 |
+
# βββ Load Model ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 11 |
llm = None
|
| 12 |
+
print("Checking for model...")
|
| 13 |
+
|
| 14 |
+
if not os.path.exists(LOCAL_PATH):
|
| 15 |
+
print(f"Downloading model from {MODEL_REPO} ...")
|
| 16 |
+
try:
|
| 17 |
+
cached = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
| 18 |
+
os.symlink(cached, LOCAL_PATH)
|
| 19 |
+
print("Model downloaded via hf_hub_download.")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"Download failed: {e}")
|
| 22 |
+
|
| 23 |
+
if os.path.exists(LOCAL_PATH):
|
| 24 |
+
print("Loading Llama-2 model into memory...")
|
| 25 |
+
try:
|
| 26 |
+
llm = Llama(model_path=LOCAL_PATH, n_ctx=2048, n_threads=4, verbose=False)
|
| 27 |
+
print("Model ready!")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Failed to load model: {e}")
|
| 30 |
else:
|
| 31 |
+
print("Model file not found. Chatbot will return placeholder responses.")
|
| 32 |
|
| 33 |
+
# βββ Chat Function βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
def chat(message, history):
|
| 35 |
if llm is None:
|
| 36 |
+
return (
|
| 37 |
+
"Model is still loading or unavailable. "
|
| 38 |
+
"Please wait a moment and try again, or check the Space logs."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Build context from last 5 turns
|
| 42 |
context = ""
|
| 43 |
+
for user_msg, bot_msg in history[-5:]:
|
| 44 |
context += f"[INST] {user_msg} [/INST] {bot_msg} </s>"
|
| 45 |
+
|
| 46 |
+
prompt = (
|
| 47 |
+
f"[INST] <<SYS>>\nYou are a helpful, respectful AI assistant.\n<</SYS>>\n\n"
|
| 48 |
+
f"{context}[INST] {message} [/INST]"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
output = llm(
|
| 52 |
+
prompt,
|
| 53 |
+
max_tokens=512,
|
| 54 |
+
stop=["[/INST]", "</s>", "User:"],
|
| 55 |
+
echo=False,
|
| 56 |
+
)
|
| 57 |
+
return output["choices"][0]["text"].strip()
|
| 58 |
+
|
| 59 |
+
# βββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 60 |
demo = gr.ChatInterface(
|
| 61 |
fn=chat,
|
| 62 |
title="Llama-2-7B Chatbot",
|
| 63 |
description=(
|
| 64 |
+
"**Offline AI chatbot** powered by Llama-2-7B (GGMLv3 Q2_K quantized).\n\n"
|
| 65 |
+
"Model is downloaded automatically from Hugging Face on startup (~2.7 GB). "
|
| 66 |
+
"First load may take a few minutes."
|
| 67 |
),
|
| 68 |
theme=gr.themes.Soft(
|
| 69 |
primary_hue="blue",
|