Spaces:
Sleeping
Sleeping
Fix LLäMmlein 1B chat Space runtime
Browse files- Dockerfile +0 -18
- README.md +3 -2
- app.py +66 -45
- requirements.txt +3 -2
Dockerfile
DELETED
|
@@ -1,18 +0,0 @@
|
|
| 1 |
-
# Use an alias for the base image for easier updates
|
| 2 |
-
FROM python:3.10 as base
|
| 3 |
-
|
| 4 |
-
# Set the working directory
|
| 5 |
-
WORKDIR /app
|
| 6 |
-
|
| 7 |
-
# Install Python requirements
|
| 8 |
-
COPY ./requirements.txt /app/
|
| 9 |
-
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 10 |
-
|
| 11 |
-
# Download model
|
| 12 |
-
RUN wget -nv https://huggingface.co/LSX-UniWue/LLaMmlein_1B_alternative_formats/resolve/LLaMmlein_1B_chat_selected/LLaMmlein_1B_chat_selected.gguf -O model.gguf
|
| 13 |
-
|
| 14 |
-
# Copy the rest of your application
|
| 15 |
-
COPY . .
|
| 16 |
-
|
| 17 |
-
# Command to run the application
|
| 18 |
-
CMD ["python", "app.py"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
|
@@ -7,15 +7,16 @@ models:
|
|
| 7 |
colorFrom: purple
|
| 8 |
colorTo: yellow
|
| 9 |
sdk: gradio
|
| 10 |
-
sdk_version: 5.
|
| 11 |
python_version: '3.10'
|
| 12 |
preload_from_hub:
|
| 13 |
- >-
|
| 14 |
LSX-UniWue/LLaMmlein_1B_alternative_formats LLaMmlein_1B_chat_selected.gguf
|
| 15 |
7d97b69ae6910b5f317be2dbd5b4820d848c66b4
|
| 16 |
pinned: true
|
|
|
|
| 17 |
thumbnail: >-
|
| 18 |
https://cdn-uploads.huggingface.co/production/uploads/6070431e1a4c4d313032558b/6_LoaV5O5bsLImOQ1oTh_.png
|
| 19 |
---
|
| 20 |
|
| 21 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 7 |
colorFrom: purple
|
| 8 |
colorTo: yellow
|
| 9 |
sdk: gradio
|
| 10 |
+
sdk_version: 5.50.0
|
| 11 |
python_version: '3.10'
|
| 12 |
preload_from_hub:
|
| 13 |
- >-
|
| 14 |
LSX-UniWue/LLaMmlein_1B_alternative_formats LLaMmlein_1B_chat_selected.gguf
|
| 15 |
7d97b69ae6910b5f317be2dbd5b4820d848c66b4
|
| 16 |
pinned: true
|
| 17 |
+
suggested_hardware: cpu-basic
|
| 18 |
thumbnail: >-
|
| 19 |
https://cdn-uploads.huggingface.co/production/uploads/6070431e1a4c4d313032558b/6_LoaV5O5bsLImOQ1oTh_.png
|
| 20 |
---
|
| 21 |
|
| 22 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -1,71 +1,92 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from llama_cpp import Llama
|
| 5 |
-
import spaces
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
model_name = model_id.split('/')[-1]
|
| 14 |
title = f"🇩🇪 {model_name}"
|
| 15 |
-
description =
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
from pathlib import Path
|
| 20 |
-
|
| 21 |
-
# Get the Hugging Face cache directory
|
| 22 |
-
hf_cache_dir = os.getenv("HF_HOME", str(Path.home() / ".cache" / "huggingface"))
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# Function for streaming chat completions
|
| 38 |
-
@spaces.GPU
|
| 39 |
def chat_stream_completion(message, history):
|
| 40 |
-
|
| 41 |
-
messages_prompts = []
|
| 42 |
-
for human, assistant in history:
|
| 43 |
-
messages_prompts.append({"role": "user", "content": human})
|
| 44 |
-
messages_prompts.append({"role": "assistant", "content": assistant})
|
| 45 |
messages_prompts.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
response = llm.
|
| 48 |
-
|
|
|
|
| 49 |
repeat_penalty=1.1,
|
| 50 |
-
#temperature=0,
|
| 51 |
stream=True,
|
| 52 |
-
stop=["
|
| 53 |
)
|
| 54 |
message_repl = ""
|
| 55 |
for chunk in response:
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
yield message_repl
|
| 59 |
|
| 60 |
-
print("starting gradio")
|
| 61 |
-
# Gradio chat interface
|
| 62 |
gr.ChatInterface(
|
| 63 |
fn=chat_stream_completion,
|
|
|
|
| 64 |
title=title,
|
| 65 |
description=description,
|
| 66 |
-
#additional_inputs=[gr.Textbox("Du bist ein hilfreicher Assistent.")],
|
| 67 |
-
#additional_inputs_accordion="📝 System prompt",
|
| 68 |
examples=[
|
| 69 |
["Was weißt du über Würzburg?"],
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
from llama_cpp import Llama
|
|
|
|
| 7 |
|
| 8 |
+
MODEL_ID = os.getenv("MODEL", "LSX-UniWue/LLaMmlein_1B_chat_selected")
|
| 9 |
+
MODEL_REPO_ID = os.getenv("MODEL_REPO_ID", "LSX-UniWue/LLaMmlein_1B_alternative_formats")
|
| 10 |
+
MODEL_REVISION = os.getenv("MODEL_REVISION", "7d97b69ae6910b5f317be2dbd5b4820d848c66b4")
|
| 11 |
+
MODEL_FILENAME = os.getenv("MODEL_FILENAME", "LLaMmlein_1B_chat_selected.gguf")
|
| 12 |
+
QUANT = os.getenv("QUANT", "Q8_0/BF16 GGUF")
|
| 13 |
+
N_CTX = int(os.getenv("N_CTX", "2048"))
|
| 14 |
+
N_THREADS = int(os.getenv("N_THREADS", str(min(4, os.cpu_count() or 2))))
|
| 15 |
+
MAX_TOKENS = int(os.getenv("MAX_TOKENS", "64"))
|
| 16 |
|
| 17 |
+
model_name = MODEL_ID.split("/")[-1]
|
|
|
|
| 18 |
title = f"🇩🇪 {model_name}"
|
| 19 |
+
description = (
|
| 20 |
+
f"Chat with <a href=\"https://huggingface.co/{MODEL_ID}\">{model_name}</a> "
|
| 21 |
+
f"in GGUF format ({QUANT})."
|
| 22 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
print("resolving model file", flush=True)
|
| 25 |
+
model_path = hf_hub_download(
|
| 26 |
+
repo_id=MODEL_REPO_ID,
|
| 27 |
+
filename=MODEL_FILENAME,
|
| 28 |
+
revision=MODEL_REVISION,
|
| 29 |
+
)
|
| 30 |
+
print(f"loading model from {Path(model_path).name}", flush=True)
|
| 31 |
|
| 32 |
+
llm = Llama(
|
| 33 |
+
model_path=model_path,
|
| 34 |
+
n_ctx=N_CTX,
|
| 35 |
+
n_threads=N_THREADS,
|
| 36 |
+
n_batch=64,
|
| 37 |
+
verbose=False,
|
| 38 |
+
)
|
| 39 |
|
| 40 |
+
def iter_history_messages(history):
|
| 41 |
+
for entry in history or []:
|
| 42 |
+
if isinstance(entry, dict):
|
| 43 |
+
role = entry.get("role")
|
| 44 |
+
content = entry.get("content")
|
| 45 |
+
if role in {"user", "assistant"} and content:
|
| 46 |
+
yield {"role": role, "content": content}
|
| 47 |
+
else:
|
| 48 |
+
human, assistant = entry
|
| 49 |
+
if human:
|
| 50 |
+
yield {"role": "user", "content": human}
|
| 51 |
+
if assistant:
|
| 52 |
+
yield {"role": "assistant", "content": assistant}
|
| 53 |
|
|
|
|
|
|
|
| 54 |
def chat_stream_completion(message, history):
|
| 55 |
+
messages_prompts = list(iter_history_messages(history))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
messages_prompts.append({"role": "user", "content": message})
|
| 57 |
+
prompt_parts = [
|
| 58 |
+
"Du bist ein hilfreicher deutschsprachiger Assistent.",
|
| 59 |
+
"",
|
| 60 |
+
]
|
| 61 |
+
for item in messages_prompts:
|
| 62 |
+
label = "Benutzer" if item["role"] == "user" else "Assistent"
|
| 63 |
+
prompt_parts.append(f"{label}: {item['content']}")
|
| 64 |
+
prompt_parts.append("Assistent:")
|
| 65 |
+
prompt = "\n".join(prompt_parts)
|
| 66 |
|
| 67 |
+
response = llm.create_completion(
|
| 68 |
+
prompt=prompt,
|
| 69 |
+
max_tokens=MAX_TOKENS,
|
| 70 |
repeat_penalty=1.1,
|
|
|
|
| 71 |
stream=True,
|
| 72 |
+
stop=["\nBenutzer:", "\nAssistent:"],
|
| 73 |
)
|
| 74 |
message_repl = ""
|
| 75 |
for chunk in response:
|
| 76 |
+
text = chunk["choices"][0].get("text", "")
|
| 77 |
+
if text:
|
| 78 |
+
message_repl = message_repl + text
|
| 79 |
yield message_repl
|
| 80 |
|
| 81 |
+
print("starting gradio", flush=True)
|
|
|
|
| 82 |
gr.ChatInterface(
|
| 83 |
fn=chat_stream_completion,
|
| 84 |
+
type="messages",
|
| 85 |
title=title,
|
| 86 |
description=description,
|
|
|
|
|
|
|
| 87 |
examples=[
|
| 88 |
["Was weißt du über Würzburg?"],
|
| 89 |
+
["Erkläre Quantencomputing in einfachen Worten."],
|
| 90 |
+
],
|
| 91 |
+
cache_examples=False,
|
| 92 |
+
).queue().launch()
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
-
llama-cpp-python
|
| 2 |
-
|
|
|
|
|
|
| 1 |
+
--extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
|
| 2 |
+
llama-cpp-python==0.3.30
|
| 3 |
+
huggingface_hub>=0.33,<1
|