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