MadsGalsgaard commited on
Commit
65baa5e
·
verified ·
1 Parent(s): b9acf24

ccp update

Browse files
Files changed (1) hide show
  1. app.py +146 -10
app.py CHANGED
@@ -114,16 +114,152 @@
114
 
115
 
116
  # Use a pipeline as a high-level helper
117
- from transformers import pipeline
 
 
 
 
 
 
 
 
 
118
 
119
- messages = [
120
- {"role": "user", "content": "Who are you?"},
121
- ]
122
- pipe = pipeline("text-generation", model="meta-llama/Meta-Llama-3.1-8B-Instruct")
123
- pipe(messages)
124
 
125
- # # Load model directly
126
- # from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
 
 
127
 
128
- # tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3.1-8B-Instruct")
129
- # model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3.1-8B-Instruct")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
 
116
  # Use a pipeline as a high-level helper
117
+ import spaces
118
+ import os
119
+ import subprocess
120
+ from llama_cpp import Llama
121
+ from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
122
+ from llama_cpp_agent.providers import LlamaCppPythonProvider
123
+ from llama_cpp_agent.chat_history import BasicChatHistory
124
+ from llama_cpp_agent.chat_history.messages import Roles
125
+ import gradio as gr
126
+ from huggingface_hub import hf_hub_download
127
 
128
+ huggingface_token = os.getenv("HF_TOKEN")
 
 
 
 
129
 
130
+ # Download the Meta-Llama-3.1-8B-Instruct model
131
+ hf_hub_download(
132
+ repo_id="bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
133
+ filename="Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf",
134
+ local_dir="./models",
135
+ token=huggingface_token
136
+ )
137
 
138
+ llm = None
139
+ llm_model = None
140
+
141
+ @spaces.GPU(duration=120)
142
+ def respond(
143
+ message,
144
+ history: list[tuple[str, str]],
145
+ model,
146
+ system_message,
147
+ max_tokens,
148
+ temperature,
149
+ top_p,
150
+ top_k,
151
+ repeat_penalty,
152
+ ):
153
+ chat_template = MessagesFormatterType.GEMMA_2
154
+
155
+ global llm
156
+ global llm_model
157
+
158
+ # Load model only if it's not already loaded or if a new model is selected
159
+ if llm is None or llm_model != model:
160
+ try:
161
+ llm = Llama(
162
+ model_path=f"models/{model}",
163
+ flash_attn=True,
164
+ n_gpu_layers=81, # Adjust based on available GPU resources
165
+ n_batch=1024,
166
+ n_ctx=8192,
167
+ )
168
+ llm_model = model
169
+ except Exception as e:
170
+ return f"Error loading model: {str(e)}"
171
+
172
+ provider = LlamaCppPythonProvider(llm)
173
+
174
+ agent = LlamaCppAgent(
175
+ provider,
176
+ system_prompt=f"{system_message}",
177
+ predefined_messages_formatter_type=chat_template,
178
+ debug_output=True
179
+ )
180
+
181
+ settings = provider.get_provider_default_settings()
182
+ settings.temperature = temperature
183
+ settings.top_k = top_k
184
+ settings.top_p = top_p
185
+ settings.max_tokens = max_tokens
186
+ settings.repeat_penalty = repeat_penalty
187
+ settings.stream = True
188
+
189
+ messages = BasicChatHistory()
190
+
191
+ # Add user and assistant messages to the history
192
+ for msn in history:
193
+ user = {'role': Roles.user, 'content': msn[0]}
194
+ assistant = {'role': Roles.assistant, 'content': msn[1]}
195
+ messages.add_message(user)
196
+ messages.add_message(assistant)
197
+
198
+ # Stream the response
199
+ try:
200
+ stream = agent.get_chat_response(
201
+ message,
202
+ llm_sampling_settings=settings,
203
+ chat_history=messages,
204
+ returns_streaming_generator=True,
205
+ print_output=False
206
+ )
207
+
208
+ outputs = ""
209
+ for output in stream:
210
+ outputs += output
211
+ yield outputs
212
+ except Exception as e:
213
+ yield f"Error during response generation: {str(e)}"
214
+
215
+ description = """<p align="center">Using the Meta-Llama-3.1-8B-Instruct Model</p>"""
216
+
217
+ demo = gr.ChatInterface(
218
+ respond,
219
+ additional_inputs=[
220
+ gr.Dropdown([
221
+ 'Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf'
222
+ ],
223
+ value="Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf",
224
+ label="Model"
225
+ ),
226
+ gr.Textbox(value="You are a helpful assistant.", label="System message"),
227
+ gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
228
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
229
+ gr.Slider(
230
+ minimum=0.1,
231
+ maximum=1.0,
232
+ value=0.95,
233
+ step=0.05,
234
+ label="Top-p",
235
+ ),
236
+ gr.Slider(
237
+ minimum=0,
238
+ maximum=100,
239
+ value=40,
240
+ step=1,
241
+ label="Top-k",
242
+ ),
243
+ gr.Slider(
244
+ minimum=0.0,
245
+ maximum=2.0,
246
+ value=1.1,
247
+ step=0.1,
248
+ label="Repetition penalty",
249
+ ),
250
+ ],
251
+ retry_btn="Retry",
252
+ undo_btn="Undo",
253
+ clear_btn="Clear",
254
+ submit_btn="Send",
255
+ title="Chat with Meta-Llama-3.1-8B-Instruct using llama.cpp",
256
+ description=description,
257
+ chatbot=gr.Chatbot(
258
+ scale=1,
259
+ likeable=False,
260
+ show_copy_button=True
261
+ )
262
+ )
263
+
264
+ if __name__ == "__main__":
265
+ demo.launch()