srijaydeshpande commited on
Commit
ab91534
1 Parent(s): 009b11d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +341 -335
app.py CHANGED
@@ -30,162 +30,168 @@ hf_hub_download(
30
  local_dir = "./models"
31
  )
32
 
33
- # def process_document(pdf_path, page_ids=None):
34
- # extracted_pages = extract_pages(pdf_path, page_numbers=page_ids)
35
-
36
- # page2content = {}
37
-
38
- # for extracted_page in tqdm(extracted_pages):
39
- # page_id = extracted_page.pageid
40
- # content = process_page(extracted_page)
41
- # page2content[page_id] = content
42
-
43
- # return page2content
44
-
45
-
46
- # def process_page(extracted_page):
47
- # content = []
48
- # elements = [element for element in extracted_page._objs]
49
- # elements.sort(key=lambda a: a.y1, reverse=True)
50
- # for i, element in enumerate(elements):
51
- # if isinstance(element, LTTextContainer):
52
- # line_text = extract_text_and_normalize(element)
53
- # content.append(line_text)
54
- # content = re.sub('\n+', ' ', ''.join(content))
55
- # return content
56
-
57
-
58
- # def extract_text_and_normalize(element):
59
- # # Extract text from line and split it with new lines
60
- # line_texts = element.get_text().split('\n')
61
- # norm_text = ''
62
- # for line_text in line_texts:
63
- # line_text = line_text.strip()
64
- # if not line_text:
65
- # line_text = '\n'
66
- # else:
67
- # line_text = re.sub('\s+', ' ', line_text)
68
- # if not re.search('[\w\d\,\-]', line_text[-1]):
69
- # line_text += '\n'
70
- # else:
71
- # line_text += ' '
72
- # norm_text += line_text
73
- # return norm_text
74
-
75
-
76
- # def txt_to_html(text):
77
- # html_content = "<html><body>"
78
- # for line in text.split('\n'):
79
- # html_content += "<p>{}</p>".format(line.strip())
80
- # html_content += "</body></html>"
81
- # return html_content
82
-
83
- # @spaces.GPU(duration=60)
84
- # def deidentify_doc(pdftext, maxtokens, temperature, top_probability):
85
- # # prompt = "In the following text replace any person name and any address with term [redacted], replace any Date of Birth and NHS number with term [redacted]"
86
- # prompt = "Perform the following actions on given report: 1. Replace any person names and person date of birth and person age and person gender with term [redacted] 2. Replace any person addresses with term [redacted] 3. Replace any person age with term [redacted] 4. DO NOT REPLACE ANY MEDICAL MEASUREMENTS 5. Replace only the CALENDAR DATES of format 'day/month/year' with term [redacted]"
 
 
 
 
 
 
87
 
88
- # # model_id = "models/Meta-Llama-3-70B-Instruct-Q3_K_M.gguf"
89
- # # # model = Llama(model_path=model_id, n_ctx=2048, n_threads=8, n_gpu_layers=-1, n_batch=128)
90
- # # model = Llama(
91
- # # model_path=model_id,
92
- # # flash_attn=True,
93
- # # n_gpu_layers=81,
94
- # # n_batch=1024,
95
- # # n_ctx=8192,
96
- # # )
97
-
98
- # chat_template = MessagesFormatterType.LLAMA_3
99
 
100
- # llm = Llama(
101
- # model_path="models/Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",
102
- # flash_attn=True,
103
- # n_gpu_layers=81,
104
- # n_batch=1024,
105
- # n_ctx=8192,
106
- # )
107
 
108
- # provider = LlamaCppPythonProvider(llm)
109
 
110
- # agent = LlamaCppAgent(
111
- # provider,
112
- # system_prompt="You are a helpful assistant.",
113
- # predefined_messages_formatter_type=chat_template,
114
- # debug_output=True
115
- # )
116
 
117
- # settings = provider.get_provider_default_settings()
118
- # settings.temperature = 0.7
119
- # settings.top_k = 40
120
- # settings.top_p = 0.95
121
- # settings.max_tokens = 2048
122
- # settings.repeat_penalty = 1.1
123
- # settings.stream = True
124
 
125
- # messages = BasicChatHistory()
126
 
127
- # stream = agent.get_chat_response(
128
- # prompt + ' : ' + pdftext,
129
- # llm_sampling_settings=settings,
130
- # chat_history=messages,
131
- # returns_streaming_generator=True,
132
- # print_output=False
133
- # )
134
 
135
- # outputs = ""
136
- # for output in stream:
137
- # outputs += output
138
 
139
- # return outputs
140
-
141
- # # output = model.create_chat_completion(
142
- # # messages=[
143
- # # {"role": "assistant", "content": prompt},
144
- # # {
145
- # # "role": "user",
146
- # # "content": pdftext
147
- # # }
148
- # # ],
149
- # # max_tokens=maxtokens,
150
- # # temperature=temperature
151
- # # )
152
- # # output = output['choices'][0]['message']['content']
153
-
154
- # # prompt = "Perform the following actions on given text: 1. Replace any person age with term [redacted] 2. DO NOT REPLACE ANY MEDICAL MEASUREMENTS 3. Replace only the CALENDAR DATES of format 'day/month/year' with term [redacted]"
155
- # # output = model.create_chat_completion(
156
- # # messages=[
157
- # # {"role": "assistant", "content": prompt},
158
- # # {
159
- # # "role": "user",
160
- # # "content": output
161
- # # }
162
- # # ],
163
- # # max_tokens=maxtokens,
164
- # # temperature=temperature
165
- # # )
166
- # # output = output['choices'][0]['message']['content']
167
-
168
- # # print(prompt)
169
- # # print(output)
170
- # # print('-------------------------------------------------------')
171
-
172
- # # return outputs
173
-
174
- # def pdf_to_text(files, maxtokens=2048, temperature=0, top_probability=0.95):
175
- # print('Control 0-----------------------------------')
176
- # files=[files]#remove later
177
- # for file in files:
178
- # file_name = os.path.basename(file)
179
- # file_name_splt = file_name.split('.')
180
- # # print('File name is ', file_name)
181
- # if (len(file_name_splt) > 1 and file_name_splt[1] == 'pdf'):
182
- # page2content = process_document(file, page_ids=[0])
183
- # pdftext = page2content[1]
184
- # print(pdftext)
185
- # # pdftext = file # remove later
186
- # if (pdftext): #shift this if block to right later
187
- # anonymized_text = deidentify_doc(pdftext, maxtokens, temperature, top_probability)
188
- # return anonymized_text
189
 
190
 
191
  # css = ".gradio-container {background: 'logo.png'}"
@@ -229,199 +235,199 @@ hf_hub_download(
229
  # # local_dir = "./models"
230
  # # )
231
 
232
- css = """
233
- .message-row {
234
- justify-content: space-evenly !important;
235
- }
236
- .message-bubble-border {
237
- border-radius: 6px !important;
238
- }
239
- .message-buttons-bot, .message-buttons-user {
240
- right: 10px !important;
241
- left: auto !important;
242
- bottom: 2px !important;
243
- }
244
- .dark.message-bubble-border {
245
- border-color: #343140 !important;
246
- }
247
- .dark.user {
248
- background: #1e1c26 !important;
249
- }
250
- .dark.assistant.dark, .dark.pending.dark {
251
- background: #16141c !important;
252
- }
253
- """
254
-
255
- def get_messages_formatter_type(model_name):
256
- if "Llama" in model_name:
257
- return MessagesFormatterType.LLAMA_3
258
- elif "Mistral" in model_name:
259
- return MessagesFormatterType.MISTRAL
260
- else:
261
- raise ValueError(f"Unsupported model: {model_name}")
262
-
263
- @spaces.GPU(duration=60)
264
- def respond(
265
- message,
266
- history: list[tuple[str, str]],
267
- model,
268
- system_message,
269
- max_tokens,
270
- temperature,
271
- top_p,
272
- top_k,
273
- repeat_penalty,
274
- ):
275
- chat_template = get_messages_formatter_type(model)
276
 
277
- llm = Llama(
278
- model_path=f"models/{model}",
279
- flash_attn=True,
280
- n_gpu_layers=81,
281
- n_batch=1024,
282
- n_ctx=8192,
283
- )
284
- provider = LlamaCppPythonProvider(llm)
 
 
 
 
 
285
 
286
- agent = LlamaCppAgent(
287
- provider,
288
- system_prompt=f"{system_message}",
289
- predefined_messages_formatter_type=chat_template,
290
- debug_output=True
291
- )
 
 
 
 
 
 
 
 
 
292
 
293
- settings = provider.get_provider_default_settings()
294
- settings.temperature = temperature
295
- settings.top_k = top_k
296
- settings.top_p = top_p
297
- settings.max_tokens = max_tokens
298
- settings.repeat_penalty = repeat_penalty
299
- settings.stream = True
300
 
301
- messages = BasicChatHistory()
302
 
303
- for msn in history:
304
- user = {
305
- 'role': Roles.user,
306
- 'content': msn[0]
307
- }
308
- assistant = {
309
- 'role': Roles.assistant,
310
- 'content': msn[1]
311
- }
312
- messages.add_message(user)
313
- messages.add_message(assistant)
314
 
315
- stream = agent.get_chat_response(
316
- message,
317
- llm_sampling_settings=settings,
318
- chat_history=messages,
319
- returns_streaming_generator=True,
320
- print_output=False
321
- )
322
 
323
- outputs = ""
324
- for output in stream:
325
- outputs += output
326
- yield outputs
327
-
328
- PLACEHOLDER = """
329
- <div class="message-bubble-border" style="display:flex; max-width: 600px; border-radius: 6px; border-width: 1px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); backdrop-filter: blur(10px);">
330
- <figure style="margin: 0;">
331
- <img src="https://huggingface.co/spaces/pabloce/llama-cpp-agent/resolve/main/llama.jpg" alt="Logo" style="width: 100%; height: 100%; border-radius: 8px;">
332
- </figure>
333
- <div style="padding: .5rem 1.5rem;">
334
- <h2 style="text-align: left; font-size: 1.5rem; font-weight: 700; margin-bottom: 0.5rem;">llama-cpp-agent</h2>
335
- <p style="text-align: left; font-size: 16px; line-height: 1.5; margin-bottom: 15px;">The llama-cpp-agent framework simplifies interactions with Large Language Models (LLMs), providing an interface for chatting, executing function calls, generating structured output, performing retrieval augmented generation, and processing text using agentic chains with tools.</p>
336
- <div style="display: flex; justify-content: space-between; align-items: center;">
337
- <div style="display: flex; flex-flow: column; justify-content: space-between;">
338
- <span style="display: inline-flex; align-items: center; border-radius: 0.375rem; background-color: rgba(229, 70, 77, 0.1); padding: 0.1rem 0.75rem; font-size: 0.75rem; font-weight: 500; color: #f88181; margin-bottom: 2.5px;">
339
- Mistral 7B Instruct v0.3
340
- </span>
341
- <span style="display: inline-flex; align-items: center; border-radius: 0.375rem; background-color: rgba(79, 70, 229, 0.1); padding: 0.1rem 0.75rem; font-size: 0.75rem; font-weight: 500; color: #60a5fa; margin-top: 2.5px;">
342
- Meta Llama 3 70B Instruct
343
- </span>
344
- </div>
345
- <div style="display: flex; justify-content: flex-end; align-items: center;">
346
- <a href="https://discord.gg/sRMvWKrh" target="_blank" rel="noreferrer" style="padding: .5rem;">
347
- <svg width="24" height="24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 5 30.67 23.25">
348
- <title>Discord</title>
349
- <path d="M26.0015 6.9529C24.0021 6.03845 21.8787 5.37198 19.6623 5C19.3833 5.48048 19.0733 6.13144 18.8563 6.64292C16.4989 6.30193 14.1585 6.30193 11.8336 6.64292C11.6166 6.13144 11.2911 5.48048 11.0276 5C8.79575 5.37198 6.67235 6.03845 4.6869 6.9529C0.672601 12.8736 -0.41235 18.6548 0.130124 24.3585C2.79599 26.2959 5.36889 27.4739 7.89682 28.2489C8.51679 27.4119 9.07477 26.5129 9.55525 25.5675C8.64079 25.2265 7.77283 24.808 6.93587 24.312C7.15286 24.1571 7.36986 23.9866 7.57135 23.8161C12.6241 26.1255 18.0969 26.1255 23.0876 23.8161C23.3046 23.9866 23.5061 24.1571 23.7231 24.312C22.8861 24.808 22.0182 25.2265 21.1037 25.5675C21.5842 26.5129 22.1422 27.4119 22.7621 28.2489C25.2885 27.4739 27.8769 26.2959 30.5288 24.3585C31.1952 17.7559 29.4733 12.0212 26.0015 6.9529ZM10.2527 20.8402C8.73376 20.8402 7.49382 19.4608 7.49382 17.7714C7.49382 16.082 8.70276 14.7025 10.2527 14.7025C11.7871 14.7025 13.0425 16.082 13.0115 17.7714C13.0115 19.4608 11.7871 20.8402 10.2527 20.8402ZM20.4373 20.8402C18.9183 20.8402 17.6768 19.4608 17.6768 17.7714C17.6768 16.082 18.8873 14.7025 20.4373 14.7025C21.9717 14.7025 23.2271 16.082 23.1961 17.7714C23.1961 19.4608 21.9872 20.8402 20.4373 20.8402Z"></path>
350
- </svg>
351
- </a>
352
- <a href="https://github.com/Maximilian-Winter/llama-cpp-agent" target="_blank" rel="noreferrer" style="padding: .5rem;">
353
- <svg width="24" height="24" fill="currentColor" viewBox="3 3 18 18">
354
- <title>GitHub</title>
355
- <path d="M12 3C7.0275 3 3 7.12937 3 12.2276C3 16.3109 5.57625 19.7597 9.15374 20.9824C9.60374 21.0631 9.77249 20.7863 9.77249 20.5441C9.77249 20.3249 9.76125 19.5982 9.76125 18.8254C7.5 19.2522 6.915 18.2602 6.735 17.7412C6.63375 17.4759 6.19499 16.6569 5.8125 16.4378C5.4975 16.2647 5.0475 15.838 5.80124 15.8264C6.51 15.8149 7.01625 16.4954 7.18499 16.7723C7.99499 18.1679 9.28875 17.7758 9.80625 17.5335C9.885 16.9337 10.1212 16.53 10.38 16.2993C8.3775 16.0687 6.285 15.2728 6.285 11.7432C6.285 10.7397 6.63375 9.9092 7.20749 9.26326C7.1175 9.03257 6.8025 8.08674 7.2975 6.81794C7.2975 6.81794 8.05125 6.57571 9.77249 7.76377C10.4925 7.55615 11.2575 7.45234 12.0225 7.45234C12.7875 7.45234 13.5525 7.55615 14.2725 7.76377C15.9937 6.56418 16.7475 6.81794 16.7475 6.81794C17.2424 8.08674 16.9275 9.03257 16.8375 9.26326C17.4113 9.9092 17.76 10.7281 17.76 11.7432C17.76 15.2843 15.6563 16.0687 13.6537 16.2993C13.98 16.5877 14.2613 17.1414 14.2613 18.0065C14.2613 19.2407 14.25 20.2326 14.25 20.5441C14.25 20.7863 14.4188 21.0746 14.8688 20.9824C16.6554 20.364 18.2079 19.1866 19.3078 17.6162C20.4077 16.0457 20.9995 14.1611 21 12.2276C21 7.12937 16.9725 3 12 3Z"></path>
356
- </svg>
357
- </a>
358
- </div>
359
- </div>
360
- </div>
361
- </div>
362
- """
363
-
364
- demo = gr.ChatInterface(
365
- respond,
366
- additional_inputs=[
367
- gr.Dropdown([
368
- 'Meta-Llama-3-70B-Instruct-Q3_K_M.gguf',
369
- 'Mistral-7B-Instruct-v0.3-f32.gguf'
370
- ],
371
- value="Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",
372
- label="Model"
373
- ),
374
- gr.Textbox(value="You are a helpful assistant.", label="System message"),
375
- gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
376
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
377
- gr.Slider(
378
- minimum=0.1,
379
- maximum=1.0,
380
- value=0.95,
381
- step=0.05,
382
- label="Top-p",
383
- ),
384
- gr.Slider(
385
- minimum=0,
386
- maximum=100,
387
- value=40,
388
- step=1,
389
- label="Top-k",
390
- ),
391
- gr.Slider(
392
- minimum=0.0,
393
- maximum=2.0,
394
- value=1.1,
395
- step=0.1,
396
- label="Repetition penalty",
397
- ),
398
- ],
399
- theme=gr.themes.Soft(primary_hue="violet", secondary_hue="violet", neutral_hue="gray",font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]).set(
400
- body_background_fill_dark="#16141c",
401
- block_background_fill_dark="#16141c",
402
- block_border_width="1px",
403
- block_title_background_fill_dark="#1e1c26",
404
- input_background_fill_dark="#292733",
405
- button_secondary_background_fill_dark="#24212b",
406
- border_color_accent_dark="#343140",
407
- border_color_primary_dark="#343140",
408
- background_fill_secondary_dark="#16141c",
409
- color_accent_soft_dark="transparent",
410
- code_background_fill_dark="#292733",
411
- ),
412
- css=css,
413
- retry_btn="Retry",
414
- undo_btn="Undo",
415
- clear_btn="Clear",
416
- submit_btn="Send",
417
- description="Llama-cpp-agent: Chat multi llm selection",
418
- chatbot=gr.Chatbot(
419
- scale=1,
420
- placeholder=PLACEHOLDER,
421
- likeable=False,
422
- show_copy_button=True
423
- )
424
- )
425
 
426
- # if __name__ == "__main__":
427
- demo.launch()
 
30
  local_dir = "./models"
31
  )
32
 
33
+ def process_document(pdf_path, page_ids=None):
34
+ extracted_pages = extract_pages(pdf_path, page_numbers=page_ids)
35
+
36
+ page2content = {}
37
+
38
+ for extracted_page in tqdm(extracted_pages):
39
+ page_id = extracted_page.pageid
40
+ content = process_page(extracted_page)
41
+ page2content[page_id] = content
42
+
43
+ return page2content
44
+
45
+
46
+ def process_page(extracted_page):
47
+ content = []
48
+ elements = [element for element in extracted_page._objs]
49
+ elements.sort(key=lambda a: a.y1, reverse=True)
50
+ for i, element in enumerate(elements):
51
+ if isinstance(element, LTTextContainer):
52
+ line_text = extract_text_and_normalize(element)
53
+ content.append(line_text)
54
+ content = re.sub('\n+', ' ', ''.join(content))
55
+ return content
56
+
57
+
58
+ def extract_text_and_normalize(element):
59
+ # Extract text from line and split it with new lines
60
+ line_texts = element.get_text().split('\n')
61
+ norm_text = ''
62
+ for line_text in line_texts:
63
+ line_text = line_text.strip()
64
+ if not line_text:
65
+ line_text = '\n'
66
+ else:
67
+ line_text = re.sub('\s+', ' ', line_text)
68
+ if not re.search('[\w\d\,\-]', line_text[-1]):
69
+ line_text += '\n'
70
+ else:
71
+ line_text += ' '
72
+ norm_text += line_text
73
+ return norm_text
74
+
75
+
76
+ def txt_to_html(text):
77
+ html_content = "<html><body>"
78
+ for line in text.split('\n'):
79
+ html_content += "<p>{}</p>".format(line.strip())
80
+ html_content += "</body></html>"
81
+ return html_content
82
+
83
+ @spaces.GPU(duration=120)
84
+ def deidentify_doc(pdftext, maxtokens, temperature, top_probability):
85
+ # prompt = "In the following text replace any person name and any address with term [redacted], replace any Date of Birth and NHS number with term [redacted]"
86
+ prompt = """
87
+ Perform the following actions on given report:
88
+ 1. Replace any person names, age, date of birth, gender with term [redacted]
89
+ 2. Replace any addresses with term [redacted]
90
+ 3. DO NOT REPLACE ANY MEDICAL MEASUREMENTS
91
+ 4. Replace only the CALENDAR DATES of format 'day/month/year' with term [redacted]
92
+ """
93
 
94
+ # model_id = "models/Meta-Llama-3-70B-Instruct-Q3_K_M.gguf"
95
+ # # model = Llama(model_path=model_id, n_ctx=2048, n_threads=8, n_gpu_layers=-1, n_batch=128)
96
+ # model = Llama(
97
+ # model_path=model_id,
98
+ # flash_attn=True,
99
+ # n_gpu_layers=81,
100
+ # n_batch=1024,
101
+ # n_ctx=8192,
102
+ # )
103
+
104
+ chat_template = MessagesFormatterType.LLAMA_3
105
 
106
+ llm = Llama(
107
+ model_path="models/Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",
108
+ flash_attn=True,
109
+ n_gpu_layers=81,
110
+ n_batch=1024,
111
+ n_ctx=8192,
112
+ )
113
 
114
+ provider = LlamaCppPythonProvider(llm)
115
 
116
+ agent = LlamaCppAgent(
117
+ provider,
118
+ system_prompt="You are a helpful assistant.",
119
+ predefined_messages_formatter_type=chat_template,
120
+ debug_output=True
121
+ )
122
 
123
+ settings = provider.get_provider_default_settings()
124
+ settings.temperature = 0.7
125
+ settings.top_k = 40
126
+ settings.top_p = 0.95
127
+ settings.max_tokens = 2048
128
+ settings.repeat_penalty = 1.1
129
+ settings.stream = True
130
 
131
+ messages = BasicChatHistory()
132
 
133
+ stream = agent.get_chat_response(
134
+ prompt + ' : ' + pdftext,
135
+ llm_sampling_settings=settings,
136
+ chat_history=messages,
137
+ returns_streaming_generator=True,
138
+ print_output=False
139
+ )
140
 
141
+ outputs = ""
142
+ for output in stream:
143
+ outputs += output
144
 
145
+ return outputs
146
+
147
+ # output = model.create_chat_completion(
148
+ # messages=[
149
+ # {"role": "assistant", "content": prompt},
150
+ # {
151
+ # "role": "user",
152
+ # "content": pdftext
153
+ # }
154
+ # ],
155
+ # max_tokens=maxtokens,
156
+ # temperature=temperature
157
+ # )
158
+ # output = output['choices'][0]['message']['content']
159
+
160
+ # prompt = "Perform the following actions on given text: 1. Replace any person age with term [redacted] 2. DO NOT REPLACE ANY MEDICAL MEASUREMENTS 3. Replace only the CALENDAR DATES of format 'day/month/year' with term [redacted]"
161
+ # output = model.create_chat_completion(
162
+ # messages=[
163
+ # {"role": "assistant", "content": prompt},
164
+ # {
165
+ # "role": "user",
166
+ # "content": output
167
+ # }
168
+ # ],
169
+ # max_tokens=maxtokens,
170
+ # temperature=temperature
171
+ # )
172
+ # output = output['choices'][0]['message']['content']
173
+
174
+ # print(prompt)
175
+ # print(output)
176
+ # print('-------------------------------------------------------')
177
+
178
+ # return outputs
179
+
180
+ def pdf_to_text(files, maxtokens=2048, temperature=0, top_probability=0.95):
181
+ print('Control 0-----------------------------------')
182
+ files=[files]#remove later
183
+ for file in files:
184
+ file_name = os.path.basename(file)
185
+ file_name_splt = file_name.split('.')
186
+ # print('File name is ', file_name)
187
+ if (len(file_name_splt) > 1 and file_name_splt[1] == 'pdf'):
188
+ page2content = process_document(file, page_ids=[0])
189
+ pdftext = page2content[1]
190
+ print(pdftext)
191
+ # pdftext = file # remove later
192
+ if (pdftext): #shift this if block to right later
193
+ anonymized_text = deidentify_doc(pdftext, maxtokens, temperature, top_probability)
194
+ return anonymized_text
195
 
196
 
197
  # css = ".gradio-container {background: 'logo.png'}"
 
235
  # # local_dir = "./models"
236
  # # )
237
 
238
+ # css = """
239
+ # .message-row {
240
+ # justify-content: space-evenly !important;
241
+ # }
242
+ # .message-bubble-border {
243
+ # border-radius: 6px !important;
244
+ # }
245
+ # .message-buttons-bot, .message-buttons-user {
246
+ # right: 10px !important;
247
+ # left: auto !important;
248
+ # bottom: 2px !important;
249
+ # }
250
+ # .dark.message-bubble-border {
251
+ # border-color: #343140 !important;
252
+ # }
253
+ # .dark.user {
254
+ # background: #1e1c26 !important;
255
+ # }
256
+ # .dark.assistant.dark, .dark.pending.dark {
257
+ # background: #16141c !important;
258
+ # }
259
+ # """
260
+
261
+ # def get_messages_formatter_type(model_name):
262
+ # if "Llama" in model_name:
263
+ # return MessagesFormatterType.LLAMA_3
264
+ # elif "Mistral" in model_name:
265
+ # return MessagesFormatterType.MISTRAL
266
+ # else:
267
+ # raise ValueError(f"Unsupported model: {model_name}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
 
269
+ # @spaces.GPU(duration=60)
270
+ # def respond(
271
+ # message,
272
+ # history: list[tuple[str, str]],
273
+ # model,
274
+ # system_message,
275
+ # max_tokens,
276
+ # temperature,
277
+ # top_p,
278
+ # top_k,
279
+ # repeat_penalty,
280
+ # ):
281
+ # chat_template = get_messages_formatter_type(model)
282
 
283
+ # llm = Llama(
284
+ # model_path=f"models/{model}",
285
+ # flash_attn=True,
286
+ # n_gpu_layers=81,
287
+ # n_batch=1024,
288
+ # n_ctx=8192,
289
+ # )
290
+ # provider = LlamaCppPythonProvider(llm)
291
+
292
+ # agent = LlamaCppAgent(
293
+ # provider,
294
+ # system_prompt=f"{system_message}",
295
+ # predefined_messages_formatter_type=chat_template,
296
+ # debug_output=True
297
+ # )
298
 
299
+ # settings = provider.get_provider_default_settings()
300
+ # settings.temperature = temperature
301
+ # settings.top_k = top_k
302
+ # settings.top_p = top_p
303
+ # settings.max_tokens = max_tokens
304
+ # settings.repeat_penalty = repeat_penalty
305
+ # settings.stream = True
306
 
307
+ # messages = BasicChatHistory()
308
 
309
+ # for msn in history:
310
+ # user = {
311
+ # 'role': Roles.user,
312
+ # 'content': msn[0]
313
+ # }
314
+ # assistant = {
315
+ # 'role': Roles.assistant,
316
+ # 'content': msn[1]
317
+ # }
318
+ # messages.add_message(user)
319
+ # messages.add_message(assistant)
320
 
321
+ # stream = agent.get_chat_response(
322
+ # message,
323
+ # llm_sampling_settings=settings,
324
+ # chat_history=messages,
325
+ # returns_streaming_generator=True,
326
+ # print_output=False
327
+ # )
328
 
329
+ # outputs = ""
330
+ # for output in stream:
331
+ # outputs += output
332
+ # yield outputs
333
+
334
+ # PLACEHOLDER = """
335
+ # <div class="message-bubble-border" style="display:flex; max-width: 600px; border-radius: 6px; border-width: 1px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); backdrop-filter: blur(10px);">
336
+ # <figure style="margin: 0;">
337
+ # <img src="https://huggingface.co/spaces/pabloce/llama-cpp-agent/resolve/main/llama.jpg" alt="Logo" style="width: 100%; height: 100%; border-radius: 8px;">
338
+ # </figure>
339
+ # <div style="padding: .5rem 1.5rem;">
340
+ # <h2 style="text-align: left; font-size: 1.5rem; font-weight: 700; margin-bottom: 0.5rem;">llama-cpp-agent</h2>
341
+ # <p style="text-align: left; font-size: 16px; line-height: 1.5; margin-bottom: 15px;">The llama-cpp-agent framework simplifies interactions with Large Language Models (LLMs), providing an interface for chatting, executing function calls, generating structured output, performing retrieval augmented generation, and processing text using agentic chains with tools.</p>
342
+ # <div style="display: flex; justify-content: space-between; align-items: center;">
343
+ # <div style="display: flex; flex-flow: column; justify-content: space-between;">
344
+ # <span style="display: inline-flex; align-items: center; border-radius: 0.375rem; background-color: rgba(229, 70, 77, 0.1); padding: 0.1rem 0.75rem; font-size: 0.75rem; font-weight: 500; color: #f88181; margin-bottom: 2.5px;">
345
+ # Mistral 7B Instruct v0.3
346
+ # </span>
347
+ # <span style="display: inline-flex; align-items: center; border-radius: 0.375rem; background-color: rgba(79, 70, 229, 0.1); padding: 0.1rem 0.75rem; font-size: 0.75rem; font-weight: 500; color: #60a5fa; margin-top: 2.5px;">
348
+ # Meta Llama 3 70B Instruct
349
+ # </span>
350
+ # </div>
351
+ # <div style="display: flex; justify-content: flex-end; align-items: center;">
352
+ # <a href="https://discord.gg/sRMvWKrh" target="_blank" rel="noreferrer" style="padding: .5rem;">
353
+ # <svg width="24" height="24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 5 30.67 23.25">
354
+ # <title>Discord</title>
355
+ # <path d="M26.0015 6.9529C24.0021 6.03845 21.8787 5.37198 19.6623 5C19.3833 5.48048 19.0733 6.13144 18.8563 6.64292C16.4989 6.30193 14.1585 6.30193 11.8336 6.64292C11.6166 6.13144 11.2911 5.48048 11.0276 5C8.79575 5.37198 6.67235 6.03845 4.6869 6.9529C0.672601 12.8736 -0.41235 18.6548 0.130124 24.3585C2.79599 26.2959 5.36889 27.4739 7.89682 28.2489C8.51679 27.4119 9.07477 26.5129 9.55525 25.5675C8.64079 25.2265 7.77283 24.808 6.93587 24.312C7.15286 24.1571 7.36986 23.9866 7.57135 23.8161C12.6241 26.1255 18.0969 26.1255 23.0876 23.8161C23.3046 23.9866 23.5061 24.1571 23.7231 24.312C22.8861 24.808 22.0182 25.2265 21.1037 25.5675C21.5842 26.5129 22.1422 27.4119 22.7621 28.2489C25.2885 27.4739 27.8769 26.2959 30.5288 24.3585C31.1952 17.7559 29.4733 12.0212 26.0015 6.9529ZM10.2527 20.8402C8.73376 20.8402 7.49382 19.4608 7.49382 17.7714C7.49382 16.082 8.70276 14.7025 10.2527 14.7025C11.7871 14.7025 13.0425 16.082 13.0115 17.7714C13.0115 19.4608 11.7871 20.8402 10.2527 20.8402ZM20.4373 20.8402C18.9183 20.8402 17.6768 19.4608 17.6768 17.7714C17.6768 16.082 18.8873 14.7025 20.4373 14.7025C21.9717 14.7025 23.2271 16.082 23.1961 17.7714C23.1961 19.4608 21.9872 20.8402 20.4373 20.8402Z"></path>
356
+ # </svg>
357
+ # </a>
358
+ # <a href="https://github.com/Maximilian-Winter/llama-cpp-agent" target="_blank" rel="noreferrer" style="padding: .5rem;">
359
+ # <svg width="24" height="24" fill="currentColor" viewBox="3 3 18 18">
360
+ # <title>GitHub</title>
361
+ # <path d="M12 3C7.0275 3 3 7.12937 3 12.2276C3 16.3109 5.57625 19.7597 9.15374 20.9824C9.60374 21.0631 9.77249 20.7863 9.77249 20.5441C9.77249 20.3249 9.76125 19.5982 9.76125 18.8254C7.5 19.2522 6.915 18.2602 6.735 17.7412C6.63375 17.4759 6.19499 16.6569 5.8125 16.4378C5.4975 16.2647 5.0475 15.838 5.80124 15.8264C6.51 15.8149 7.01625 16.4954 7.18499 16.7723C7.99499 18.1679 9.28875 17.7758 9.80625 17.5335C9.885 16.9337 10.1212 16.53 10.38 16.2993C8.3775 16.0687 6.285 15.2728 6.285 11.7432C6.285 10.7397 6.63375 9.9092 7.20749 9.26326C7.1175 9.03257 6.8025 8.08674 7.2975 6.81794C7.2975 6.81794 8.05125 6.57571 9.77249 7.76377C10.4925 7.55615 11.2575 7.45234 12.0225 7.45234C12.7875 7.45234 13.5525 7.55615 14.2725 7.76377C15.9937 6.56418 16.7475 6.81794 16.7475 6.81794C17.2424 8.08674 16.9275 9.03257 16.8375 9.26326C17.4113 9.9092 17.76 10.7281 17.76 11.7432C17.76 15.2843 15.6563 16.0687 13.6537 16.2993C13.98 16.5877 14.2613 17.1414 14.2613 18.0065C14.2613 19.2407 14.25 20.2326 14.25 20.5441C14.25 20.7863 14.4188 21.0746 14.8688 20.9824C16.6554 20.364 18.2079 19.1866 19.3078 17.6162C20.4077 16.0457 20.9995 14.1611 21 12.2276C21 7.12937 16.9725 3 12 3Z"></path>
362
+ # </svg>
363
+ # </a>
364
+ # </div>
365
+ # </div>
366
+ # </div>
367
+ # </div>
368
+ # """
369
+
370
+ # demo = gr.ChatInterface(
371
+ # respond,
372
+ # additional_inputs=[
373
+ # gr.Dropdown([
374
+ # 'Meta-Llama-3-70B-Instruct-Q3_K_M.gguf',
375
+ # 'Mistral-7B-Instruct-v0.3-f32.gguf'
376
+ # ],
377
+ # value="Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",
378
+ # label="Model"
379
+ # ),
380
+ # gr.Textbox(value="You are a helpful assistant.", label="System message"),
381
+ # gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
382
+ # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
383
+ # gr.Slider(
384
+ # minimum=0.1,
385
+ # maximum=1.0,
386
+ # value=0.95,
387
+ # step=0.05,
388
+ # label="Top-p",
389
+ # ),
390
+ # gr.Slider(
391
+ # minimum=0,
392
+ # maximum=100,
393
+ # value=40,
394
+ # step=1,
395
+ # label="Top-k",
396
+ # ),
397
+ # gr.Slider(
398
+ # minimum=0.0,
399
+ # maximum=2.0,
400
+ # value=1.1,
401
+ # step=0.1,
402
+ # label="Repetition penalty",
403
+ # ),
404
+ # ],
405
+ # theme=gr.themes.Soft(primary_hue="violet", secondary_hue="violet", neutral_hue="gray",font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]).set(
406
+ # body_background_fill_dark="#16141c",
407
+ # block_background_fill_dark="#16141c",
408
+ # block_border_width="1px",
409
+ # block_title_background_fill_dark="#1e1c26",
410
+ # input_background_fill_dark="#292733",
411
+ # button_secondary_background_fill_dark="#24212b",
412
+ # border_color_accent_dark="#343140",
413
+ # border_color_primary_dark="#343140",
414
+ # background_fill_secondary_dark="#16141c",
415
+ # color_accent_soft_dark="transparent",
416
+ # code_background_fill_dark="#292733",
417
+ # ),
418
+ # css=css,
419
+ # retry_btn="Retry",
420
+ # undo_btn="Undo",
421
+ # clear_btn="Clear",
422
+ # submit_btn="Send",
423
+ # description="Llama-cpp-agent: Chat multi llm selection",
424
+ # chatbot=gr.Chatbot(
425
+ # scale=1,
426
+ # placeholder=PLACEHOLDER,
427
+ # likeable=False,
428
+ # show_copy_button=True
429
+ # )
430
+ # )
431
 
432
+ # # if __name__ == "__main__":
433
+ # demo.launch()