Spaces:
Running
Running
File size: 14,744 Bytes
e66c4c3 1730c92 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
from typing import List, Tuple, Optional
import gradio as gr
from langchain_core.vectorstores import VectorStore
from config import (
LLM_MODEL_REPOS,
EMBED_MODEL_REPOS,
SUBTITLES_LANGUAGES,
GENERATE_KWARGS,
CONTEXT_TEMPLATE,
)
from utils import (
load_llm_model,
load_embed_model,
load_documents_and_create_db,
user_message_to_chatbot,
update_user_message_with_context,
get_llm_response,
get_gguf_model_names,
add_new_model_repo,
clear_llm_folder,
clear_embed_folder,
get_memory_usage,
)
# ============ INTERFACE COMPONENT INITIALIZATION FUNCS ============
def get_rag_mode_component(db: Optional[VectorStore]) -> gr.Checkbox:
value = visible = db is not None
return gr.Checkbox(value=value, label='RAG Mode', scale=1, visible=visible)
def get_rag_settings(
rag_mode: bool,
context_template_value: str,
render: bool = True,
) -> Tuple[gr.component, ...]:
k = gr.Radio(
choices=[1, 2, 3, 4, 5, 'all'],
value=2,
label='Number of relevant documents for search',
visible=rag_mode,
render=render,
)
score_threshold = gr.Slider(
minimum=0,
maximum=1,
value=0.5,
step=0.05,
label='relevance_scores_threshold',
visible=rag_mode,
render=render,
)
context_template = gr.Textbox(
value=context_template_value,
label='Context Template',
lines=len(context_template_value.split('\n')),
visible=rag_mode,
render=render,
)
return k, score_threshold, context_template
def get_user_message_with_context(text: str, rag_mode: bool) -> gr.component:
num_lines = len(text.split('\n'))
max_lines = 10
num_lines = max_lines if num_lines > max_lines else num_lines
return gr.Textbox(
text,
visible=rag_mode,
interactive=False,
label='User Message With Context',
lines=num_lines,
)
def get_system_prompt_component(interactive: bool) -> gr.Textbox:
value = '' if interactive else 'System prompt is not supported by this model'
return gr.Textbox(value=value, label='System prompt', interactive=interactive)
def get_generate_args(do_sample: bool) -> List[gr.component]:
generate_args = [
gr.Slider(minimum=0.1, maximum=3, value=GENERATE_KWARGS['temperature'], step=0.1, label='temperature', visible=do_sample),
gr.Slider(minimum=0, maximum=1, value=GENERATE_KWARGS['top_p'], step=0.01, label='top_p', visible=do_sample),
gr.Slider(minimum=1, maximum=50, value=GENERATE_KWARGS['top_k'], step=1, label='top_k', visible=do_sample),
gr.Slider(minimum=1, maximum=5, value=GENERATE_KWARGS['repeat_penalty'], step=0.1, label='repeat_penalty', visible=do_sample),
]
return generate_args
# ================ LOADING AND INITIALIZING MODELS ========================
start_llm_model, start_support_system_role, load_log = load_llm_model(LLM_MODEL_REPOS[0], 'gemma-2-2b-it-Q8_0.gguf')
start_embed_model, load_log = load_embed_model(EMBED_MODEL_REPOS[0])
# ================== APPLICATION WEB INTERFACE ============================
css = '''.gradio-container {width: 60% !important}'''
with gr.Blocks(css=css) as interface:
# ==================== GRADIO STATES ===============================
documents = gr.State([])
db = gr.State(None)
user_message_with_context = gr.State('')
support_system_role = gr.State(start_support_system_role)
llm_model_repos = gr.State(LLM_MODEL_REPOS)
embed_model_repos = gr.State(EMBED_MODEL_REPOS)
llm_model = gr.State(start_llm_model)
embed_model = gr.State(start_embed_model)
# ==================== BOT PAGE =================================
with gr.Tab(label='Chatbot'):
with gr.Row():
with gr.Column(scale=3):
chatbot = gr.Chatbot(
type='messages', # new in gradio 5+
show_copy_button=True,
bubble_full_width=False,
height=480,
)
user_message = gr.Textbox(label='User')
with gr.Row():
user_message_btn = gr.Button('Send')
stop_btn = gr.Button('Stop')
clear_btn = gr.Button('Clear')
# ------------- GENERATION PARAMETERS -------------------
with gr.Column(scale=1, min_width=80):
with gr.Group():
gr.Markdown('History size')
history_len = gr.Slider(
minimum=0,
maximum=5,
value=0,
step=1,
info='Number of previous messages taken into account in history',
label='history_len',
show_label=False,
)
with gr.Group():
gr.Markdown('Generation parameters')
do_sample = gr.Checkbox(
value=False,
label='do_sample',
info='Activate random sampling',
)
generate_args = get_generate_args(do_sample.value)
do_sample.change(
fn=get_generate_args,
inputs=do_sample,
outputs=generate_args,
show_progress=False,
)
rag_mode = get_rag_mode_component(db=db.value)
k, score_threshold, context_template = get_rag_settings(
rag_mode=rag_mode.value,
context_template_value=CONTEXT_TEMPLATE,
render=False,
)
rag_mode.change(
fn=get_rag_settings,
inputs=[rag_mode, context_template],
outputs=[k, score_threshold, context_template],
)
with gr.Row():
k.render()
score_threshold.render()
# ---------------- SYSTEM PROMPT AND USER MESSAGE -----------
with gr.Accordion('Prompt', open=True):
system_prompt = get_system_prompt_component(interactive=support_system_role.value)
context_template.render()
user_message_with_context = get_user_message_with_context(text='', rag_mode=rag_mode.value)
# ---------------- SEND, CLEAR AND STOP BUTTONS ------------
generate_event = gr.on(
triggers=[user_message.submit, user_message_btn.click],
fn=user_message_to_chatbot,
inputs=[user_message, chatbot],
outputs=[user_message, chatbot],
queue=False,
).then(
fn=update_user_message_with_context,
inputs=[chatbot, rag_mode, db, k, score_threshold, context_template],
outputs=[user_message_with_context],
).then(
fn=get_user_message_with_context,
inputs=[user_message_with_context, rag_mode],
outputs=[user_message_with_context],
).then(
fn=get_llm_response,
inputs=[chatbot, llm_model, user_message_with_context, rag_mode, system_prompt,
support_system_role, history_len, do_sample, *generate_args],
outputs=[chatbot],
)
stop_btn.click(
fn=None,
inputs=None,
outputs=None,
cancels=generate_event,
queue=False,
)
clear_btn.click(
fn=lambda: (None, ''),
inputs=None,
outputs=[chatbot, user_message_with_context],
queue=False,
)
# ================= FILE DOWNLOAD PAGE =========================
with gr.Tab(label='Load documents'):
with gr.Row(variant='compact'):
upload_files = gr.File(file_count='multiple', label='Loading text files')
web_links = gr.Textbox(lines=6, label='Links to Web sites or YouTube')
with gr.Row(variant='compact'):
chunk_size = gr.Slider(50, 2000, value=500, step=50, label='Chunk size')
chunk_overlap = gr.Slider(0, 200, value=20, step=10, label='Chunk overlap')
subtitles_lang = gr.Radio(
SUBTITLES_LANGUAGES,
value=SUBTITLES_LANGUAGES[0],
label='YouTube subtitle language',
)
load_documents_btn = gr.Button(value='Upload documents and initialize database')
load_docs_log = gr.Textbox(label='Status of loading and splitting documents', interactive=False)
load_documents_btn.click(
fn=load_documents_and_create_db,
inputs=[upload_files, web_links, subtitles_lang, chunk_size, chunk_overlap, embed_model],
outputs=[documents, db, load_docs_log],
).success(
fn=get_rag_mode_component,
inputs=[db],
outputs=[rag_mode],
)
gr.HTML("""<h3 style='text-align: center'>
<a href="https://github.com/sergey21000/chatbot-rag" target='_blank'>GitHub Repository</a></h3>
""")
# ================= VIEW PAGE FOR ALL DOCUMENTS =================
with gr.Tab(label='View documents'):
view_documents_btn = gr.Button(value='Show downloaded text chunks')
view_documents_textbox = gr.Textbox(
lines=1,
placeholder='To view chunks, load documents in the Load documents tab',
label='Uploaded chunks',
)
sep = '=' * 20
view_documents_btn.click(
lambda documents: f'\n{sep}\n\n'.join([doc.page_content for doc in documents]),
inputs=[documents],
outputs=[view_documents_textbox],
)
# ============== GGUF MODELS DOWNLOAD PAGE =====================
with gr.Tab('Load LLM model'):
new_llm_model_repo = gr.Textbox(
value='',
label='Add repository',
placeholder='Link to repository of HF models in GGUF format',
)
new_llm_model_repo_btn = gr.Button('Add repository')
curr_llm_model_repo = gr.Dropdown(
choices=LLM_MODEL_REPOS,
value=None,
label='HF Model Repository',
)
curr_llm_model_path = gr.Dropdown(
choices=[],
value=None,
label='GGUF model file',
)
load_llm_model_btn = gr.Button('Loading and initializing model')
load_llm_model_log = gr.Textbox(
value=f'Model {LLM_MODEL_REPOS[0]} loaded at application startup',
label='Model loading status',
lines=6,
)
with gr.Group():
gr.Markdown('Free up disk space by deleting all models except the currently selected one')
clear_llm_folder_btn = gr.Button('Clear folder')
new_llm_model_repo_btn.click(
fn=add_new_model_repo,
inputs=[new_llm_model_repo, llm_model_repos],
outputs=[curr_llm_model_repo, load_llm_model_log],
).success(
fn=lambda: '',
inputs=None,
outputs=[new_llm_model_repo],
)
curr_llm_model_repo.change(
fn=get_gguf_model_names,
inputs=[curr_llm_model_repo],
outputs=[curr_llm_model_path],
)
load_llm_model_btn.click(
fn=load_llm_model,
inputs=[curr_llm_model_repo, curr_llm_model_path],
outputs=[llm_model, support_system_role, load_llm_model_log],
).success(
fn=lambda log: log + get_memory_usage(),
inputs=[load_llm_model_log],
outputs=[load_llm_model_log],
).then(
fn=get_system_prompt_component,
inputs=[support_system_role],
outputs=[system_prompt],
)
clear_llm_folder_btn.click(
fn=clear_llm_folder,
inputs=[curr_llm_model_path],
outputs=None,
).success(
fn=lambda model_path: f'Models other than {model_path} removed',
inputs=[curr_llm_model_path],
outputs=None,
)
# ============== EMBEDDING MODELS DOWNLOAD PAGE =============
with gr.Tab('Load embed model'):
new_embed_model_repo = gr.Textbox(
value='',
label='Add repository',
placeholder='Link to HF model repository',
)
new_embed_model_repo_btn = gr.Button('Add repository')
curr_embed_model_repo = gr.Dropdown(
choices=EMBED_MODEL_REPOS,
value=None,
label='HF model repository',
)
load_embed_model_btn = gr.Button('Loading and initializing model')
load_embed_model_log = gr.Textbox(
value=f'Model {EMBED_MODEL_REPOS[0]} loaded at application startup',
label='Model loading status',
lines=7,
)
with gr.Group():
gr.Markdown('Free up disk space by deleting all models except the currently selected one')
clear_embed_folder_btn = gr.Button('Clear folder')
new_embed_model_repo_btn.click(
fn=add_new_model_repo,
inputs=[new_embed_model_repo, embed_model_repos],
outputs=[curr_embed_model_repo, load_embed_model_log],
).success(
fn=lambda: '',
inputs=None,
outputs=new_embed_model_repo,
)
load_embed_model_btn.click(
fn=load_embed_model,
inputs=[curr_embed_model_repo],
outputs=[embed_model, load_embed_model_log],
).success(
fn=lambda log: log + get_memory_usage(),
inputs=[load_embed_model_log],
outputs=[load_embed_model_log],
)
clear_embed_folder_btn.click(
fn=clear_embed_folder,
inputs=[curr_embed_model_repo],
outputs=None,
).success(
fn=lambda model_repo: f'Models other than {model_repo} removed',
inputs=[curr_embed_model_repo],
outputs=None,
)
interface.launch(server_name='0.0.0.0', server_port=7860) # debug=True |