Upload 4 files
Browse files- README.md +7 -6
- app.py +159 -0
- gitattributes +35 -0
- requirements.txt +4 -0
README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.37.2
|
8 |
app_file: app.py
|
9 |
-
pinned:
|
|
|
10 |
---
|
11 |
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Gemma 2 llama.cpp 27B/9B
|
3 |
+
emoji: 😻
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: purple
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.37.2
|
8 |
app_file: app.py
|
9 |
+
pinned: true
|
10 |
+
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import json
|
3 |
+
import subprocess
|
4 |
+
from llama_cpp import Llama
|
5 |
+
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
6 |
+
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
7 |
+
from llama_cpp_agent.chat_history import BasicChatHistory
|
8 |
+
from llama_cpp_agent.chat_history.messages import Roles
|
9 |
+
import gradio as gr
|
10 |
+
from huggingface_hub import hf_hub_download
|
11 |
+
|
12 |
+
hf_hub_download(
|
13 |
+
repo_id="bartowski/gemma-2-9b-it-GGUF",
|
14 |
+
filename="gemma-2-9b-it-Q5_K_M.gguf",
|
15 |
+
local_dir="./models"
|
16 |
+
)
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
hf_hub_download(
|
21 |
+
repo_id="bartowski/gemma-2-27b-it-GGUF",
|
22 |
+
filename="gemma-2-27b-it-Q5_K_M.gguf",
|
23 |
+
local_dir="./models"
|
24 |
+
)
|
25 |
+
|
26 |
+
|
27 |
+
llm = None
|
28 |
+
llm_model = None
|
29 |
+
|
30 |
+
@spaces.GPU(duration=120)
|
31 |
+
def respond(
|
32 |
+
message,
|
33 |
+
history: list[tuple[str, str]],
|
34 |
+
model,
|
35 |
+
system_message,
|
36 |
+
max_tokens,
|
37 |
+
temperature,
|
38 |
+
top_p,
|
39 |
+
top_k,
|
40 |
+
repeat_penalty,
|
41 |
+
):
|
42 |
+
chat_template = MessagesFormatterType.GEMMA_2
|
43 |
+
|
44 |
+
global llm
|
45 |
+
global llm_model
|
46 |
+
|
47 |
+
if llm is None or llm_model != model:
|
48 |
+
llm = Llama(
|
49 |
+
model_path=f"models/{model}",
|
50 |
+
flash_attn=True,
|
51 |
+
n_gpu_layers=81,
|
52 |
+
n_batch=1024,
|
53 |
+
n_ctx=8192,
|
54 |
+
)
|
55 |
+
llm_model = model
|
56 |
+
|
57 |
+
provider = LlamaCppPythonProvider(llm)
|
58 |
+
|
59 |
+
agent = LlamaCppAgent(
|
60 |
+
provider,
|
61 |
+
system_prompt=f"{system_message}",
|
62 |
+
predefined_messages_formatter_type=chat_template,
|
63 |
+
debug_output=True
|
64 |
+
)
|
65 |
+
|
66 |
+
settings = provider.get_provider_default_settings()
|
67 |
+
settings.temperature = temperature
|
68 |
+
settings.top_k = top_k
|
69 |
+
settings.top_p = top_p
|
70 |
+
settings.max_tokens = max_tokens
|
71 |
+
settings.repeat_penalty = repeat_penalty
|
72 |
+
settings.stream = True
|
73 |
+
|
74 |
+
messages = BasicChatHistory()
|
75 |
+
|
76 |
+
for msn in history:
|
77 |
+
user = {
|
78 |
+
'role': Roles.user,
|
79 |
+
'content': msn[0]
|
80 |
+
}
|
81 |
+
assistant = {
|
82 |
+
'role': Roles.assistant,
|
83 |
+
'content': msn[1]
|
84 |
+
}
|
85 |
+
messages.add_message(user)
|
86 |
+
messages.add_message(assistant)
|
87 |
+
|
88 |
+
stream = agent.get_chat_response(
|
89 |
+
message,
|
90 |
+
llm_sampling_settings=settings,
|
91 |
+
chat_history=messages,
|
92 |
+
returns_streaming_generator=True,
|
93 |
+
print_output=False
|
94 |
+
)
|
95 |
+
|
96 |
+
outputs = ""
|
97 |
+
for output in stream:
|
98 |
+
outputs += output
|
99 |
+
yield outputs
|
100 |
+
|
101 |
+
description = """<p align="center">Defaults to 27B it (you can switch to 9b it from additional inputs)</p>
|
102 |
+
<p><center>
|
103 |
+
<a href="https://huggingface.co/google/gemma-2-27b-it" target="_blank">[27B it Model]</a>
|
104 |
+
<a href="https://huggingface.co/google/gemma-2-9b-it" target="_blank">[9B it Model]</a>
|
105 |
+
<a href="https://huggingface.co/bartowski/gemma-2-27b-it-GGUF" target="_blank">[27B it Model GGUF]</a>
|
106 |
+
<a href="https://huggingface.co/bartowski/gemma-2-9b-it-GGUF" target="_blank">[9B it Model GGUF]</a>
|
107 |
+
</center></p>
|
108 |
+
"""
|
109 |
+
|
110 |
+
demo = gr.ChatInterface(
|
111 |
+
respond,
|
112 |
+
additional_inputs=[
|
113 |
+
gr.Dropdown([
|
114 |
+
'gemma-2-9b-it-Q5_K_M.gguf',
|
115 |
+
'gemma-2-27b-it-Q5_K_M.gguf'
|
116 |
+
],
|
117 |
+
value="gemma-2-27b-it-Q5_K_M.gguf",
|
118 |
+
label="Model"
|
119 |
+
),
|
120 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
121 |
+
gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
|
122 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
123 |
+
gr.Slider(
|
124 |
+
minimum=0.1,
|
125 |
+
maximum=1.0,
|
126 |
+
value=0.95,
|
127 |
+
step=0.05,
|
128 |
+
label="Top-p",
|
129 |
+
),
|
130 |
+
gr.Slider(
|
131 |
+
minimum=0,
|
132 |
+
maximum=100,
|
133 |
+
value=40,
|
134 |
+
step=1,
|
135 |
+
label="Top-k",
|
136 |
+
),
|
137 |
+
gr.Slider(
|
138 |
+
minimum=0.0,
|
139 |
+
maximum=2.0,
|
140 |
+
value=1.1,
|
141 |
+
step=0.1,
|
142 |
+
label="Repetition penalty",
|
143 |
+
),
|
144 |
+
],
|
145 |
+
retry_btn="Retry",
|
146 |
+
undo_btn="Undo",
|
147 |
+
clear_btn="Clear",
|
148 |
+
submit_btn="Send",
|
149 |
+
title="Chat with Gemma 2 using llama.cpp",
|
150 |
+
description=description,
|
151 |
+
chatbot=gr.Chatbot(
|
152 |
+
scale=1,
|
153 |
+
likeable=False,
|
154 |
+
show_copy_button=True
|
155 |
+
)
|
156 |
+
)
|
157 |
+
|
158 |
+
if __name__ == "__main__":
|
159 |
+
demo.launch()
|
gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub==0.22.2
|
2 |
+
scikit-build-core
|
3 |
+
https://github.com/abetlen/llama-cpp-python/releases/download/v0.2.81-cu124/llama_cpp_python-0.2.81-cp310-cp310-linux_x86_64.whl
|
4 |
+
llama-cpp-agent>=0.2.25
|