Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,211 +1,211 @@
|
|
1 |
-
# Importing required libraries
|
2 |
-
import warnings
|
3 |
-
warnings.filterwarnings("ignore")
|
4 |
-
|
5 |
-
import json
|
6 |
-
import subprocess
|
7 |
-
import sys
|
8 |
-
from llama_cpp import Llama
|
9 |
-
from llama_cpp_agent import LlamaCppAgent
|
10 |
-
from llama_cpp_agent import MessagesFormatterType
|
11 |
-
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
12 |
-
from llama_cpp_agent.chat_history import BasicChatHistory
|
13 |
-
from llama_cpp_agent.chat_history.messages import Roles
|
14 |
-
import gradio as gr
|
15 |
-
from huggingface_hub import hf_hub_download
|
16 |
-
from typing import List, Tuple
|
17 |
-
from logger import logging
|
18 |
-
from exception import CustomExceptionHandling
|
19 |
-
|
20 |
-
|
21 |
-
# Download gguf model files
|
22 |
-
llm = None
|
23 |
-
llm_model = None
|
24 |
-
|
25 |
-
hf_hub_download(
|
26 |
-
repo_id="bartowski/Qwen2.5-Coder-1.5B-Instruct-GGUF",
|
27 |
-
filename="Qwen2.5-Coder-1.5B-Instruct-Q6_K.gguf",
|
28 |
-
local_dir="./models",
|
29 |
-
)
|
30 |
-
hf_hub_download(
|
31 |
-
repo_id="bartowski/Qwen2.5-Coder-
|
32 |
-
filename="Qwen2.5-Coder-
|
33 |
-
local_dir="./models",
|
34 |
-
)
|
35 |
-
|
36 |
-
# Set the title and description
|
37 |
-
title = "Qwen-Coder Llama.cpp"
|
38 |
-
description = """Qwen2.5-Coder, a six-model family of LLMs, boasts enhanced code generation, reasoning, and debugging. Trained on 5.5 trillion tokens, its 32B parameter model rivals GPT-4o, offering versatile capabilities for coding and broader applications."""
|
39 |
-
|
40 |
-
|
41 |
-
def respond(
|
42 |
-
message: str,
|
43 |
-
history: List[Tuple[str, str]],
|
44 |
-
model: str,
|
45 |
-
system_message: str,
|
46 |
-
max_tokens: int,
|
47 |
-
temperature: float,
|
48 |
-
top_p: float,
|
49 |
-
top_k: int,
|
50 |
-
repeat_penalty: float,
|
51 |
-
):
|
52 |
-
"""
|
53 |
-
Respond to a message using the Qwen2.5-Coder model via Llama.cpp.
|
54 |
-
|
55 |
-
Args:
|
56 |
-
- message (str): The message to respond to.
|
57 |
-
- history (List[Tuple[str, str]]): The chat history.
|
58 |
-
- model (str): The model to use.
|
59 |
-
- system_message (str): The system message to use.
|
60 |
-
- max_tokens (int): The maximum number of tokens to generate.
|
61 |
-
- temperature (float): The temperature of the model.
|
62 |
-
- top_p (float): The top-p of the model.
|
63 |
-
- top_k (int): The top-k of the model.
|
64 |
-
- repeat_penalty (float): The repetition penalty of the model.
|
65 |
-
|
66 |
-
Returns:
|
67 |
-
str: The response to the message.
|
68 |
-
"""
|
69 |
-
try:
|
70 |
-
# Load the global variables
|
71 |
-
global llm
|
72 |
-
global llm_model
|
73 |
-
|
74 |
-
# Load the model
|
75 |
-
if llm is None or llm_model != model:
|
76 |
-
llm = Llama(
|
77 |
-
model_path=f"models/{model}",
|
78 |
-
flash_attn=False,
|
79 |
-
n_gpu_layers=0,
|
80 |
-
n_batch=32,
|
81 |
-
n_ctx=8192,
|
82 |
-
)
|
83 |
-
llm_model = model
|
84 |
-
provider = LlamaCppPythonProvider(llm)
|
85 |
-
|
86 |
-
# Create the agent
|
87 |
-
agent = LlamaCppAgent(
|
88 |
-
provider,
|
89 |
-
system_prompt=f"{system_message}",
|
90 |
-
predefined_messages_formatter_type=MessagesFormatterType.CHATML,
|
91 |
-
debug_output=True,
|
92 |
-
)
|
93 |
-
|
94 |
-
# Set the settings like temperature, top-k, top-p, max tokens, etc.
|
95 |
-
settings = provider.get_provider_default_settings()
|
96 |
-
settings.temperature = temperature
|
97 |
-
settings.top_k = top_k
|
98 |
-
settings.top_p = top_p
|
99 |
-
settings.max_tokens = max_tokens
|
100 |
-
settings.repeat_penalty = repeat_penalty
|
101 |
-
settings.stream = True
|
102 |
-
|
103 |
-
messages = BasicChatHistory()
|
104 |
-
|
105 |
-
# Add the chat history
|
106 |
-
for msn in history:
|
107 |
-
user = {"role": Roles.user, "content": msn[0]}
|
108 |
-
assistant = {"role": Roles.assistant, "content": msn[1]}
|
109 |
-
messages.add_message(user)
|
110 |
-
messages.add_message(assistant)
|
111 |
-
|
112 |
-
# Get the response stream
|
113 |
-
stream = agent.get_chat_response(
|
114 |
-
message,
|
115 |
-
llm_sampling_settings=settings,
|
116 |
-
chat_history=messages,
|
117 |
-
returns_streaming_generator=True,
|
118 |
-
print_output=False,
|
119 |
-
)
|
120 |
-
|
121 |
-
# Log the success
|
122 |
-
logging.info("Response stream generated successfully")
|
123 |
-
|
124 |
-
# Generate the response
|
125 |
-
outputs = ""
|
126 |
-
for output in stream:
|
127 |
-
outputs += output
|
128 |
-
yield outputs
|
129 |
-
|
130 |
-
# Handle exceptions that may occur during the process
|
131 |
-
except Exception as e:
|
132 |
-
# Custom exception handling
|
133 |
-
raise CustomExceptionHandling(e, sys) from e
|
134 |
-
|
135 |
-
|
136 |
-
# Create a chat interface
|
137 |
-
demo = gr.ChatInterface(
|
138 |
-
respond,
|
139 |
-
additional_inputs_accordion=gr.Accordion(
|
140 |
-
label="⚙️ Parameters", open=False, render=False
|
141 |
-
),
|
142 |
-
additional_inputs=[
|
143 |
-
gr.Dropdown(
|
144 |
-
choices=[
|
145 |
-
"Qwen2.5-Coder-1.5B-Instruct-Q6_K.gguf",
|
146 |
-
"Qwen2.5-Coder-
|
147 |
-
],
|
148 |
-
value="Qwen2.5-Coder-
|
149 |
-
label="Model",
|
150 |
-
info="Select the AI model to use for chat",
|
151 |
-
),
|
152 |
-
gr.Textbox(
|
153 |
-
value="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.",
|
154 |
-
label="System Prompt",
|
155 |
-
info="Define the AI assistant's personality and behavior",
|
156 |
-
lines=2,
|
157 |
-
),
|
158 |
-
gr.Slider(
|
159 |
-
minimum=512,
|
160 |
-
maximum=4096,
|
161 |
-
value=2048,
|
162 |
-
step=512,
|
163 |
-
label="Max Tokens",
|
164 |
-
info="Maximum length of response (higher = longer replies)",
|
165 |
-
),
|
166 |
-
gr.Slider(
|
167 |
-
minimum=0.1,
|
168 |
-
maximum=2.0,
|
169 |
-
value=0.7,
|
170 |
-
step=0.1,
|
171 |
-
label="Temperature",
|
172 |
-
info="Creativity level (higher = more creative, lower = more focused)",
|
173 |
-
),
|
174 |
-
gr.Slider(
|
175 |
-
minimum=0.1,
|
176 |
-
maximum=1.0,
|
177 |
-
value=0.95,
|
178 |
-
step=0.05,
|
179 |
-
label="Top-p",
|
180 |
-
info="Nucleus sampling threshold",
|
181 |
-
),
|
182 |
-
gr.Slider(
|
183 |
-
minimum=1,
|
184 |
-
maximum=100,
|
185 |
-
value=40,
|
186 |
-
step=1,
|
187 |
-
label="Top-k",
|
188 |
-
info="Limit vocabulary choices to top K tokens",
|
189 |
-
),
|
190 |
-
gr.Slider(
|
191 |
-
minimum=1.0,
|
192 |
-
maximum=2.0,
|
193 |
-
value=1.1,
|
194 |
-
step=0.1,
|
195 |
-
label="Repetition Penalty",
|
196 |
-
info="Penalize repeated words (higher = less repetition)",
|
197 |
-
),
|
198 |
-
],
|
199 |
-
theme="Ocean",
|
200 |
-
submit_btn="Send",
|
201 |
-
stop_btn="Stop",
|
202 |
-
title=title,
|
203 |
-
description=description,
|
204 |
-
chatbot=gr.Chatbot(scale=1, show_copy_button=True),
|
205 |
-
flagging_mode="never",
|
206 |
-
)
|
207 |
-
|
208 |
-
|
209 |
-
# Launch the chat interface
|
210 |
-
if __name__ == "__main__":
|
211 |
-
demo.launch(debug=False)
|
|
|
1 |
+
# Importing required libraries
|
2 |
+
import warnings
|
3 |
+
warnings.filterwarnings("ignore")
|
4 |
+
|
5 |
+
import json
|
6 |
+
import subprocess
|
7 |
+
import sys
|
8 |
+
from llama_cpp import Llama
|
9 |
+
from llama_cpp_agent import LlamaCppAgent
|
10 |
+
from llama_cpp_agent import MessagesFormatterType
|
11 |
+
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
12 |
+
from llama_cpp_agent.chat_history import BasicChatHistory
|
13 |
+
from llama_cpp_agent.chat_history.messages import Roles
|
14 |
+
import gradio as gr
|
15 |
+
from huggingface_hub import hf_hub_download
|
16 |
+
from typing import List, Tuple
|
17 |
+
from logger import logging
|
18 |
+
from exception import CustomExceptionHandling
|
19 |
+
|
20 |
+
|
21 |
+
# Download gguf model files
|
22 |
+
llm = None
|
23 |
+
llm_model = None
|
24 |
+
|
25 |
+
hf_hub_download(
|
26 |
+
repo_id="bartowski/Qwen2.5-Coder-1.5B-Instruct-GGUF",
|
27 |
+
filename="Qwen2.5-Coder-1.5B-Instruct-Q6_K.gguf",
|
28 |
+
local_dir="./models",
|
29 |
+
)
|
30 |
+
hf_hub_download(
|
31 |
+
repo_id="bartowski/Qwen2.5-Coder-0.5B-Instruct-GGUF",
|
32 |
+
filename="Qwen2.5-Coder-0.5B-Instruct-Q6_K.gguf",
|
33 |
+
local_dir="./models",
|
34 |
+
)
|
35 |
+
|
36 |
+
# Set the title and description
|
37 |
+
title = "Qwen-Coder Llama.cpp"
|
38 |
+
description = """Qwen2.5-Coder, a six-model family of LLMs, boasts enhanced code generation, reasoning, and debugging. Trained on 5.5 trillion tokens, its 32B parameter model rivals GPT-4o, offering versatile capabilities for coding and broader applications."""
|
39 |
+
|
40 |
+
|
41 |
+
def respond(
|
42 |
+
message: str,
|
43 |
+
history: List[Tuple[str, str]],
|
44 |
+
model: str,
|
45 |
+
system_message: str,
|
46 |
+
max_tokens: int,
|
47 |
+
temperature: float,
|
48 |
+
top_p: float,
|
49 |
+
top_k: int,
|
50 |
+
repeat_penalty: float,
|
51 |
+
):
|
52 |
+
"""
|
53 |
+
Respond to a message using the Qwen2.5-Coder model via Llama.cpp.
|
54 |
+
|
55 |
+
Args:
|
56 |
+
- message (str): The message to respond to.
|
57 |
+
- history (List[Tuple[str, str]]): The chat history.
|
58 |
+
- model (str): The model to use.
|
59 |
+
- system_message (str): The system message to use.
|
60 |
+
- max_tokens (int): The maximum number of tokens to generate.
|
61 |
+
- temperature (float): The temperature of the model.
|
62 |
+
- top_p (float): The top-p of the model.
|
63 |
+
- top_k (int): The top-k of the model.
|
64 |
+
- repeat_penalty (float): The repetition penalty of the model.
|
65 |
+
|
66 |
+
Returns:
|
67 |
+
str: The response to the message.
|
68 |
+
"""
|
69 |
+
try:
|
70 |
+
# Load the global variables
|
71 |
+
global llm
|
72 |
+
global llm_model
|
73 |
+
|
74 |
+
# Load the model
|
75 |
+
if llm is None or llm_model != model:
|
76 |
+
llm = Llama(
|
77 |
+
model_path=f"models/{model}",
|
78 |
+
flash_attn=False,
|
79 |
+
n_gpu_layers=0,
|
80 |
+
n_batch=32,
|
81 |
+
n_ctx=8192,
|
82 |
+
)
|
83 |
+
llm_model = model
|
84 |
+
provider = LlamaCppPythonProvider(llm)
|
85 |
+
|
86 |
+
# Create the agent
|
87 |
+
agent = LlamaCppAgent(
|
88 |
+
provider,
|
89 |
+
system_prompt=f"{system_message}",
|
90 |
+
predefined_messages_formatter_type=MessagesFormatterType.CHATML,
|
91 |
+
debug_output=True,
|
92 |
+
)
|
93 |
+
|
94 |
+
# Set the settings like temperature, top-k, top-p, max tokens, etc.
|
95 |
+
settings = provider.get_provider_default_settings()
|
96 |
+
settings.temperature = temperature
|
97 |
+
settings.top_k = top_k
|
98 |
+
settings.top_p = top_p
|
99 |
+
settings.max_tokens = max_tokens
|
100 |
+
settings.repeat_penalty = repeat_penalty
|
101 |
+
settings.stream = True
|
102 |
+
|
103 |
+
messages = BasicChatHistory()
|
104 |
+
|
105 |
+
# Add the chat history
|
106 |
+
for msn in history:
|
107 |
+
user = {"role": Roles.user, "content": msn[0]}
|
108 |
+
assistant = {"role": Roles.assistant, "content": msn[1]}
|
109 |
+
messages.add_message(user)
|
110 |
+
messages.add_message(assistant)
|
111 |
+
|
112 |
+
# Get the response stream
|
113 |
+
stream = agent.get_chat_response(
|
114 |
+
message,
|
115 |
+
llm_sampling_settings=settings,
|
116 |
+
chat_history=messages,
|
117 |
+
returns_streaming_generator=True,
|
118 |
+
print_output=False,
|
119 |
+
)
|
120 |
+
|
121 |
+
# Log the success
|
122 |
+
logging.info("Response stream generated successfully")
|
123 |
+
|
124 |
+
# Generate the response
|
125 |
+
outputs = ""
|
126 |
+
for output in stream:
|
127 |
+
outputs += output
|
128 |
+
yield outputs
|
129 |
+
|
130 |
+
# Handle exceptions that may occur during the process
|
131 |
+
except Exception as e:
|
132 |
+
# Custom exception handling
|
133 |
+
raise CustomExceptionHandling(e, sys) from e
|
134 |
+
|
135 |
+
|
136 |
+
# Create a chat interface
|
137 |
+
demo = gr.ChatInterface(
|
138 |
+
respond,
|
139 |
+
additional_inputs_accordion=gr.Accordion(
|
140 |
+
label="⚙️ Parameters", open=False, render=False
|
141 |
+
),
|
142 |
+
additional_inputs=[
|
143 |
+
gr.Dropdown(
|
144 |
+
choices=[
|
145 |
+
"Qwen2.5-Coder-1.5B-Instruct-Q6_K.gguf",
|
146 |
+
"Qwen2.5-Coder-0.5B-Instruct-Q6_K.gguf",
|
147 |
+
],
|
148 |
+
value="Qwen2.5-Coder-0.5B-Instruct-Q6_K.gguf",
|
149 |
+
label="Model",
|
150 |
+
info="Select the AI model to use for chat",
|
151 |
+
),
|
152 |
+
gr.Textbox(
|
153 |
+
value="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.",
|
154 |
+
label="System Prompt",
|
155 |
+
info="Define the AI assistant's personality and behavior",
|
156 |
+
lines=2,
|
157 |
+
),
|
158 |
+
gr.Slider(
|
159 |
+
minimum=512,
|
160 |
+
maximum=4096,
|
161 |
+
value=2048,
|
162 |
+
step=512,
|
163 |
+
label="Max Tokens",
|
164 |
+
info="Maximum length of response (higher = longer replies)",
|
165 |
+
),
|
166 |
+
gr.Slider(
|
167 |
+
minimum=0.1,
|
168 |
+
maximum=2.0,
|
169 |
+
value=0.7,
|
170 |
+
step=0.1,
|
171 |
+
label="Temperature",
|
172 |
+
info="Creativity level (higher = more creative, lower = more focused)",
|
173 |
+
),
|
174 |
+
gr.Slider(
|
175 |
+
minimum=0.1,
|
176 |
+
maximum=1.0,
|
177 |
+
value=0.95,
|
178 |
+
step=0.05,
|
179 |
+
label="Top-p",
|
180 |
+
info="Nucleus sampling threshold",
|
181 |
+
),
|
182 |
+
gr.Slider(
|
183 |
+
minimum=1,
|
184 |
+
maximum=100,
|
185 |
+
value=40,
|
186 |
+
step=1,
|
187 |
+
label="Top-k",
|
188 |
+
info="Limit vocabulary choices to top K tokens",
|
189 |
+
),
|
190 |
+
gr.Slider(
|
191 |
+
minimum=1.0,
|
192 |
+
maximum=2.0,
|
193 |
+
value=1.1,
|
194 |
+
step=0.1,
|
195 |
+
label="Repetition Penalty",
|
196 |
+
info="Penalize repeated words (higher = less repetition)",
|
197 |
+
),
|
198 |
+
],
|
199 |
+
theme="Ocean",
|
200 |
+
submit_btn="Send",
|
201 |
+
stop_btn="Stop",
|
202 |
+
title=title,
|
203 |
+
description=description,
|
204 |
+
chatbot=gr.Chatbot(scale=1, show_copy_button=True),
|
205 |
+
flagging_mode="never",
|
206 |
+
)
|
207 |
+
|
208 |
+
|
209 |
+
# Launch the chat interface
|
210 |
+
if __name__ == "__main__":
|
211 |
+
demo.launch(debug=False)
|