Spaces:
Sleeping
Sleeping
local app
Browse files- app.py +10 -3
- app_local.py +192 -0
app.py
CHANGED
@@ -32,9 +32,16 @@ hf_hub_download(
|
|
32 |
local_dir="./models",
|
33 |
token=huggingface_token
|
34 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
# TobDeBer/granite-8b-code-instruct-128k-Q4_K_M-GGUF
|
37 |
# granite-8b-code-instruct-128k-q4_k_m.gguf
|
|
|
38 |
|
39 |
llm = None
|
40 |
llm_model = None
|
@@ -109,8 +116,8 @@ def respond(
|
|
109 |
outputs += output
|
110 |
yield outputs
|
111 |
|
112 |
-
description = """<p align="center">Defaults to Qwen 500M
|
113 |
-
More models in Advanced Section <br>
|
114 |
"""
|
115 |
|
116 |
demo = gr.ChatInterface(
|
@@ -153,7 +160,7 @@ demo = gr.ChatInterface(
|
|
153 |
undo_btn="Undo",
|
154 |
clear_btn="Clear",
|
155 |
submit_btn="Send",
|
156 |
-
title="Chat with Qwen 2 using llama.cpp",
|
157 |
description=description,
|
158 |
chatbot=gr.Chatbot(
|
159 |
scale=1,
|
|
|
32 |
local_dir="./models",
|
33 |
token=huggingface_token
|
34 |
)
|
35 |
+
# 5GB
|
36 |
+
|
37 |
+
|
38 |
+
# RichardErkhov/ibm-granite_-_granite-7b-base-gguf
|
39 |
+
# granite-7b-base.Q4_K_M.gguf
|
40 |
+
# 4GB
|
41 |
|
42 |
# TobDeBer/granite-8b-code-instruct-128k-Q4_K_M-GGUF
|
43 |
# granite-8b-code-instruct-128k-q4_k_m.gguf
|
44 |
+
# 5GB
|
45 |
|
46 |
llm = None
|
47 |
llm_model = None
|
|
|
116 |
outputs += output
|
117 |
yield outputs
|
118 |
|
119 |
+
description = """<p align="center">Defaults to Qwen 500M<br>
|
120 |
+
More models in Advanced Section <br></p>
|
121 |
"""
|
122 |
|
123 |
demo = gr.ChatInterface(
|
|
|
160 |
undo_btn="Undo",
|
161 |
clear_btn="Clear",
|
162 |
submit_btn="Send",
|
163 |
+
title="Chat with Qwen 2 and friends using llama.cpp",
|
164 |
description=description,
|
165 |
chatbot=gr.Chatbot(
|
166 |
scale=1,
|
app_local.py
ADDED
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# conda activate audio
|
2 |
+
# pip install llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
|
3 |
+
import llama_cpp
|
4 |
+
import os
|
5 |
+
import json
|
6 |
+
import subprocess
|
7 |
+
from llama_cpp import Llama
|
8 |
+
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
9 |
+
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
10 |
+
from llama_cpp_agent.chat_history import BasicChatHistory
|
11 |
+
from llama_cpp_agent.chat_history.messages import Roles
|
12 |
+
import gradio as gr
|
13 |
+
from huggingface_hub import hf_hub_download
|
14 |
+
|
15 |
+
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
16 |
+
|
17 |
+
#hf_hub_download(
|
18 |
+
# repo_id="Qwen/Qwen2-0.5B-Instruct-GGUF",
|
19 |
+
# filename="qwen2-0_5b-instruct-q4_k_m.gguf",
|
20 |
+
# local_dir="./models"
|
21 |
+
#)
|
22 |
+
|
23 |
+
#hf_hub_download(
|
24 |
+
# repo_id="TobDeBer/gpt2-Q4_K_M-GGUF",
|
25 |
+
# filename="gpt2-q4_k_m.gguf",
|
26 |
+
# local_dir="./models"
|
27 |
+
#)
|
28 |
+
|
29 |
+
#hf_hub_download(
|
30 |
+
# repo_id="TobDeBer/Meta-Llama-3.1-8B-Instruct-Q4_K_M-GGUF",
|
31 |
+
# filename="meta-llama-3.1-8b-instruct-q4_k_m.gguf",
|
32 |
+
# local_dir="./models",
|
33 |
+
# token=huggingface_token
|
34 |
+
#)
|
35 |
+
# 5GB
|
36 |
+
|
37 |
+
|
38 |
+
# RichardErkhov/ibm-granite_-_granite-7b-base-gguf
|
39 |
+
# granite-7b-base.Q4_K_M.gguf
|
40 |
+
# 4GB
|
41 |
+
|
42 |
+
# TobDeBer/granite-8b-code-instruct-128k-Q4_K_M-GGUF
|
43 |
+
# granite-8b-code-instruct-128k-q4_k_m.gguf
|
44 |
+
# 5GB
|
45 |
+
|
46 |
+
llm = None
|
47 |
+
llm_model = None
|
48 |
+
|
49 |
+
def respond(
|
50 |
+
message,
|
51 |
+
history: list[tuple[str, str]],
|
52 |
+
model,
|
53 |
+
system_message,
|
54 |
+
max_tokens,
|
55 |
+
temperature,
|
56 |
+
top_p,
|
57 |
+
top_k,
|
58 |
+
repeat_penalty,
|
59 |
+
):
|
60 |
+
chat_template = MessagesFormatterType.GEMMA_2
|
61 |
+
|
62 |
+
global llm
|
63 |
+
global llm_model
|
64 |
+
|
65 |
+
if llm is None or llm_model != model:
|
66 |
+
llm = Llama(
|
67 |
+
model_path=f"models/{model}",
|
68 |
+
flash_attn=True,
|
69 |
+
n_gpu_layers=81,
|
70 |
+
n_batch=1024,
|
71 |
+
n_ctx=8192,
|
72 |
+
)
|
73 |
+
llm_model = model
|
74 |
+
|
75 |
+
provider = LlamaCppPythonProvider(llm)
|
76 |
+
|
77 |
+
agent = LlamaCppAgent(
|
78 |
+
provider,
|
79 |
+
system_prompt=f"{system_message}",
|
80 |
+
predefined_messages_formatter_type=chat_template,
|
81 |
+
debug_output=True
|
82 |
+
)
|
83 |
+
|
84 |
+
settings = provider.get_provider_default_settings()
|
85 |
+
settings.temperature = temperature
|
86 |
+
settings.top_k = top_k
|
87 |
+
settings.top_p = top_p
|
88 |
+
settings.max_tokens = max_tokens
|
89 |
+
settings.repeat_penalty = repeat_penalty
|
90 |
+
settings.stream = True
|
91 |
+
|
92 |
+
messages = BasicChatHistory()
|
93 |
+
|
94 |
+
for msn in history:
|
95 |
+
user = {
|
96 |
+
'role': Roles.user,
|
97 |
+
'content': msn[0]
|
98 |
+
}
|
99 |
+
assistant = {
|
100 |
+
'role': Roles.assistant,
|
101 |
+
'content': msn[1]
|
102 |
+
}
|
103 |
+
messages.add_message(user)
|
104 |
+
messages.add_message(assistant)
|
105 |
+
|
106 |
+
stream = agent.get_chat_response(
|
107 |
+
message,
|
108 |
+
llm_sampling_settings=settings,
|
109 |
+
chat_history=messages,
|
110 |
+
returns_streaming_generator=True,
|
111 |
+
print_output=False
|
112 |
+
)
|
113 |
+
|
114 |
+
outputs = ""
|
115 |
+
for output in stream:
|
116 |
+
outputs += output
|
117 |
+
yield outputs
|
118 |
+
|
119 |
+
description = """<p align="center">Defaults to Qwen 500M</p>
|
120 |
+
"""
|
121 |
+
|
122 |
+
# Create a shared state variable to store the selected model
|
123 |
+
selected_model = gr.State("qwen2-0_5b-instruct-q4_k_m.gguf")
|
124 |
+
|
125 |
+
# Create a separate interface for model selection
|
126 |
+
model_selection = gr.Interface(
|
127 |
+
fn=None, # No function needed for model selection
|
128 |
+
inputs=[
|
129 |
+
gr.Dropdown([
|
130 |
+
'qwen2-0_5b-instruct-q4_k_m.gguf',
|
131 |
+
'gpt2-q4_k_m.gguf',
|
132 |
+
'meta-llama-3.1-8b-instruct-q4_k_m.gguf',
|
133 |
+
],
|
134 |
+
value="qwen2-0_5b-instruct-q4_k_m.gguf",
|
135 |
+
label="Model"
|
136 |
+
),
|
137 |
+
],
|
138 |
+
outputs=None, # Add a dummy output
|
139 |
+
title="Select Model",
|
140 |
+
)
|
141 |
+
|
142 |
+
# Create the main chat interface
|
143 |
+
demo = gr.ChatInterface(
|
144 |
+
respond,
|
145 |
+
additional_inputs=[
|
146 |
+
selected_model, # Use the state variable to receive the model
|
147 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
148 |
+
gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
|
149 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
150 |
+
gr.Slider(
|
151 |
+
minimum=0.1,
|
152 |
+
maximum=1.0,
|
153 |
+
value=0.95,
|
154 |
+
step=0.05,
|
155 |
+
label="Top-p",
|
156 |
+
),
|
157 |
+
gr.Slider(
|
158 |
+
minimum=0,
|
159 |
+
maximum=100,
|
160 |
+
value=40,
|
161 |
+
step=1,
|
162 |
+
label="Top-k",
|
163 |
+
),
|
164 |
+
gr.Slider(
|
165 |
+
minimum=0.0,
|
166 |
+
maximum=2.0,
|
167 |
+
value=1.1,
|
168 |
+
step=0.1,
|
169 |
+
label="Repetition penalty",
|
170 |
+
),
|
171 |
+
],
|
172 |
+
retry_btn="Retry",
|
173 |
+
undo_btn="Undo",
|
174 |
+
clear_btn="Clear",
|
175 |
+
submit_btn="Send",
|
176 |
+
title="Chat with Qwen 2 and friends using llama.cpp",
|
177 |
+
description=description,
|
178 |
+
chatbot=gr.Chatbot(
|
179 |
+
scale=1,
|
180 |
+
likeable=False,
|
181 |
+
show_copy_button=True
|
182 |
+
)
|
183 |
+
)
|
184 |
+
|
185 |
+
gr.TabbedInterface(
|
186 |
+
[model_selection, demo],
|
187 |
+
tab_names=["Model Selection", "Demo"],
|
188 |
+
).launch()
|
189 |
+
|
190 |
+
# Launch the model selection interface
|
191 |
+
#if __name__ == "__main__":
|
192 |
+
# model_selection.launch()
|