Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ from llama_cpp import Llama
|
|
10 |
|
11 |
# ================== ANNOTATIONS ========================
|
12 |
|
13 |
-
CHAT_HISTORY = List[
|
14 |
MODEL_DICT = Dict[str, Llama]
|
15 |
|
16 |
|
@@ -70,7 +70,7 @@ def download_gguf_and_init_model(gguf_url: str, model_dict: MODEL_DICT) -> Tuple
|
|
70 |
|
71 |
def user_message_to_chatbot(user_message: str, chatbot: CHAT_HISTORY) -> Tuple[str, CHAT_HISTORY]:
|
72 |
if user_message:
|
73 |
-
chatbot.append(
|
74 |
return '', chatbot
|
75 |
|
76 |
|
@@ -85,38 +85,42 @@ def bot_response_to_chatbot(
|
|
85 |
):
|
86 |
|
87 |
model = model_dict.get('model')
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
gen_kwargs = dict(zip(GENERATE_KWARGS.keys(), generate_args))
|
92 |
gen_kwargs['top_k'] = int(gen_kwargs['top_k'])
|
93 |
-
|
94 |
if not do_sample:
|
95 |
gen_kwargs['top_p'] = 0.0
|
96 |
gen_kwargs['top_k'] = 1
|
97 |
gen_kwargs['repeat_penalty'] = 1.0
|
98 |
|
99 |
-
if support_system_role and system_prompt:
|
100 |
-
messages.append({'role': 'system', 'content': system_prompt})
|
101 |
-
|
102 |
-
if history_len != 0:
|
103 |
-
for user_msg, bot_msg in chatbot[:-1][-history_len:]:
|
104 |
-
print(user_msg, bot_msg)
|
105 |
-
messages.append({'role': 'user', 'content': user_msg})
|
106 |
-
messages.append({'role': 'assistant', 'content': bot_msg})
|
107 |
-
|
108 |
-
messages.append({'role': 'user', 'content': user_message})
|
109 |
stream_response = model.create_chat_completion(
|
110 |
messages=messages,
|
111 |
stream=True,
|
112 |
**gen_kwargs,
|
113 |
)
|
114 |
|
115 |
-
chatbot
|
116 |
for chunk in stream_response:
|
117 |
token = chunk['choices'][0]['delta'].get('content')
|
118 |
if token is not None:
|
119 |
-
chatbot[-1][
|
120 |
yield chatbot
|
121 |
|
122 |
|
@@ -169,10 +173,15 @@ with gr.Blocks(theme=theme, css=css) as interface:
|
|
169 |
support_system_role = gr.State(start_support_system_role)
|
170 |
|
171 |
# ================= CHAT BOT PAGE ======================
|
172 |
-
with gr.Tab('
|
173 |
with gr.Row():
|
174 |
with gr.Column(scale=3):
|
175 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
176 |
user_message = gr.Textbox(label='User')
|
177 |
|
178 |
with gr.Row():
|
@@ -259,5 +268,5 @@ with gr.Blocks(theme=theme, css=css) as interface:
|
|
259 |
gr.HTML("""<h3 style='text-align: center'>
|
260 |
<a href="https://github.com/sergey21000/gradio-llamacpp-chatbot" target='_blank'>GitHub Repository</a></h3>
|
261 |
""")
|
262 |
-
|
263 |
interface.launch(server_name='0.0.0.0', server_port=7860)
|
|
|
10 |
|
11 |
# ================== ANNOTATIONS ========================
|
12 |
|
13 |
+
CHAT_HISTORY = List[Optional[Dict[str, Optional[str]]]]
|
14 |
MODEL_DICT = Dict[str, Llama]
|
15 |
|
16 |
|
|
|
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 |
|
|
|
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 |
|
|
|
173 |
support_system_role = gr.State(start_support_system_role)
|
174 |
|
175 |
# ================= CHAT BOT PAGE ======================
|
176 |
+
with gr.Tab('Chatbot'):
|
177 |
with gr.Row():
|
178 |
with gr.Column(scale=3):
|
179 |
+
chatbot = gr.Chatbot(
|
180 |
+
type='messages', # new in gradio 5+
|
181 |
+
show_copy_button=True,
|
182 |
+
bubble_full_width=False,
|
183 |
+
height=480,
|
184 |
+
)
|
185 |
user_message = gr.Textbox(label='User')
|
186 |
|
187 |
with gr.Row():
|
|
|
268 |
gr.HTML("""<h3 style='text-align: center'>
|
269 |
<a href="https://github.com/sergey21000/gradio-llamacpp-chatbot" target='_blank'>GitHub Repository</a></h3>
|
270 |
""")
|
271 |
+
|
272 |
interface.launch(server_name='0.0.0.0', server_port=7860)
|