Spaces:
Running
Running
add-model-selector
#1
by
bradnow
- opened
- .gitignore +1 -4
- README.md +4 -4
- __pycache__/utils.cpython-310.pyc +0 -0
- app.py +141 -356
- gradio_runner.py +0 -10
- log_chat.py +0 -237
- requirements.txt +1 -3
- styles.css +0 -118
- theme.py +0 -148
- timer.py +0 -114
- utils.py +0 -127
.gitignore
CHANGED
@@ -1,4 +1 @@
|
|
1 |
-
.idea/*
|
2 |
-
__pycache__/
|
3 |
-
/.run*/
|
4 |
-
/train.csv
|
|
|
1 |
+
.idea/*
|
|
|
|
|
|
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
-
title: Apriel Chat
|
3 |
emoji: 💬
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.29.0
|
8 |
app_file: app.py
|
@@ -11,4 +11,4 @@ license: mit
|
|
11 |
short_description: ServiceNow-AI model chat
|
12 |
---
|
13 |
|
14 |
-
|
|
|
1 |
---
|
2 |
+
title: Apriel Nemotron Chat
|
3 |
emoji: 💬
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: purple
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.29.0
|
8 |
app_file: app.py
|
|
|
11 |
short_description: ServiceNow-AI model chat
|
12 |
---
|
13 |
|
14 |
+
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
__pycache__/utils.cpython-310.pyc
DELETED
Binary file (2.59 kB)
|
|
app.py
CHANGED
@@ -1,381 +1,166 @@
|
|
|
|
|
|
1 |
import datetime
|
2 |
-
from uuid import uuid4
|
3 |
|
4 |
from openai import OpenAI
|
5 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
from utils import COMMUNITY_POSTFIX_URL, get_model_config, check_format, models_config, \
|
9 |
-
logged_event_handler, DEBUG_MODEL, log_debug, log_info, log_error
|
10 |
-
from log_chat import log_chat
|
11 |
|
12 |
-
|
13 |
-
BUTTON_WIDTH = 160
|
14 |
-
DEFAULT_OPT_OUT_VALUE = False
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
BUTTON_ENABLED = gr.update(interactive=True)
|
19 |
-
BUTTON_DISABLED = gr.update(interactive=False)
|
20 |
-
INPUT_ENABLED = gr.update(interactive=True)
|
21 |
-
INPUT_DISABLED = gr.update(interactive=False)
|
22 |
-
DROPDOWN_ENABLED = gr.update(interactive=True)
|
23 |
-
DROPDOWN_DISABLED = gr.update(interactive=False)
|
24 |
-
|
25 |
-
SEND_BUTTON_ENABLED = gr.update(interactive=True, visible=True)
|
26 |
-
SEND_BUTTON_DISABLED = gr.update(interactive=True, visible=False)
|
27 |
-
STOP_BUTTON_ENABLED = gr.update(interactive=True, visible=True)
|
28 |
-
STOP_BUTTON_DISABLED = gr.update(interactive=True, visible=False)
|
29 |
|
30 |
chat_start_count = 0
|
31 |
-
model_config = {}
|
32 |
-
openai_client = None
|
33 |
-
|
34 |
-
|
35 |
-
def app_loaded(state, request: gr.Request):
|
36 |
-
message_html = setup_model(DEFAULT_MODEL_NAME, intial=False)
|
37 |
-
state['session'] = request.session_hash if request else uuid4().hex
|
38 |
-
log_debug(f"app_loaded() --> Session: {state['session']}")
|
39 |
-
return state, message_html
|
40 |
-
|
41 |
-
|
42 |
-
def update_model_and_clear_chat(model_name):
|
43 |
-
actual_model_name = model_name.replace("Model: ", "")
|
44 |
-
desc = setup_model(actual_model_name)
|
45 |
-
return desc, []
|
46 |
-
|
47 |
-
|
48 |
-
def setup_model(model_name, intial=False):
|
49 |
-
global model_config, openai_client
|
50 |
-
model_config = get_model_config(model_name)
|
51 |
-
log_debug(f"update_model() --> Model config: {model_config}")
|
52 |
-
openai_client = OpenAI(
|
53 |
-
api_key=model_config.get('AUTH_TOKEN'),
|
54 |
-
base_url=model_config.get('VLLM_API_URL')
|
55 |
-
)
|
56 |
-
|
57 |
-
_model_hf_name = model_config.get("MODEL_HF_URL").split('https://huggingface.co/')[1]
|
58 |
-
_link = f"<a href='{model_config.get('MODEL_HF_URL')}{COMMUNITY_POSTFIX_URL}' target='_blank'>{_model_hf_name}</a>"
|
59 |
-
_description = f"We'd love to hear your thoughts on the model. Click here to provide feedback - {_link}"
|
60 |
-
|
61 |
-
log_debug(f"Switched to model {_model_hf_name}")
|
62 |
-
|
63 |
-
if intial:
|
64 |
-
return
|
65 |
-
else:
|
66 |
-
return _description
|
67 |
-
|
68 |
-
|
69 |
-
def chat_started():
|
70 |
-
# outputs: model_dropdown, user_input, send_btn, stop_btn, clear_btn
|
71 |
-
return (DROPDOWN_DISABLED, gr.update(value="", interactive=False),
|
72 |
-
SEND_BUTTON_DISABLED, STOP_BUTTON_ENABLED, BUTTON_DISABLED)
|
73 |
-
|
74 |
-
|
75 |
-
def chat_finished():
|
76 |
-
# outputs: model_dropdown, user_input, send_btn, stop_btn, clear_btn
|
77 |
-
return DROPDOWN_ENABLED, INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED
|
78 |
-
|
79 |
-
|
80 |
-
def stop_chat(state):
|
81 |
-
state["stop_flag"] = True
|
82 |
-
gr.Info("Chat stopped")
|
83 |
-
return state
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
-
def toggle_opt_out(state, checkbox):
|
87 |
-
state["opt_out"] = checkbox
|
88 |
-
return state
|
89 |
|
|
|
|
|
90 |
|
91 |
-
def run_chat_inference(history, message, state):
|
92 |
global chat_start_count
|
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 |
-
user_messages_count = sum(1 for item in history if isinstance(item, dict) and item.get("role") == "user")
|
120 |
-
log_info(f"chat_start_count: {chat_start_count}, turns: {user_messages_count}, model: {model_name}")
|
121 |
-
|
122 |
-
is_reasoning = model_config.get("REASONING")
|
123 |
-
|
124 |
-
# Remove any assistant messages with metadata from history for multiple turns
|
125 |
-
log_debug(f"Initial History: {history}")
|
126 |
-
check_format(history, "messages")
|
127 |
-
history.append({"role": "user", "content": message})
|
128 |
-
log_debug(f"History with user message: {history}")
|
129 |
-
check_format(history, "messages")
|
130 |
-
|
131 |
-
# Create the streaming response
|
132 |
-
try:
|
133 |
-
history_no_thoughts = [item for item in history if
|
134 |
-
not (isinstance(item, dict) and
|
135 |
-
item.get("role") == "assistant" and
|
136 |
-
isinstance(item.get("metadata"), dict) and
|
137 |
-
item.get("metadata", {}).get("title") is not None)]
|
138 |
-
log_debug(f"Updated History: {history_no_thoughts}")
|
139 |
-
check_format(history_no_thoughts, "messages")
|
140 |
-
log_debug(f"history_no_thoughts with user message: {history_no_thoughts}")
|
141 |
-
|
142 |
-
stream = openai_client.chat.completions.create(
|
143 |
-
model=model_name,
|
144 |
-
messages=history_no_thoughts,
|
145 |
-
temperature=MODEL_TEMPERATURE,
|
146 |
-
stream=True
|
147 |
-
)
|
148 |
-
except Exception as e:
|
149 |
-
log_error(f"Error: {e}")
|
150 |
-
error = str(e)
|
151 |
-
yield ([{"role": "assistant",
|
152 |
-
"content": "😔 The model is unavailable at the moment. Please try again later."}],
|
153 |
-
INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED, state)
|
154 |
-
if state["opt_out"] is not True:
|
155 |
-
log_chat(chat_id=state["chat_id"],
|
156 |
-
session_id=state["session"],
|
157 |
-
model_name=model_name,
|
158 |
-
prompt=message,
|
159 |
-
history=history,
|
160 |
-
info={"is_reasoning": model_config.get("REASONING"), "temperature": MODEL_TEMPERATURE,
|
161 |
-
"stopped": True, "error": str(e)},
|
162 |
-
)
|
163 |
-
else:
|
164 |
-
log_info(f"User opted out of chat history. Not logging chat. model: {model_name}")
|
165 |
-
return history, INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED, state
|
166 |
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
role="assistant",
|
170 |
-
content=
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
check_format(history, "messages")
|
175 |
-
else:
|
176 |
history.append(gr.ChatMessage(
|
177 |
role="assistant",
|
178 |
-
content=
|
179 |
))
|
180 |
-
log_debug(f"History added empty assistant: {history}")
|
181 |
-
check_format(history, "messages")
|
182 |
-
|
183 |
-
output = ""
|
184 |
-
completion_started = False
|
185 |
-
for chunk in stream:
|
186 |
-
if state["stop_flag"]:
|
187 |
-
log_debug(f"chat_fn() --> Stopping streaming...")
|
188 |
-
break # Exit the loop if the stop flag is set
|
189 |
-
# Extract the new content from the delta field
|
190 |
-
content = getattr(chunk.choices[0].delta, "content", "")
|
191 |
-
output += content
|
192 |
-
|
193 |
-
if is_reasoning:
|
194 |
-
parts = output.split("[BEGIN FINAL RESPONSE]")
|
195 |
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
parts[1] = parts[1].replace("[END FINAL RESPONSE]\n<|end|>", "")
|
201 |
-
if parts[1].endswith("<|end|>"):
|
202 |
-
parts[1] = parts[1].replace("<|end|>", "")
|
203 |
|
204 |
-
|
205 |
-
|
206 |
-
content=parts[0],
|
207 |
-
metadata={"title": "🧠 Thought"}
|
208 |
-
)
|
209 |
-
if completion_started:
|
210 |
-
history[-1] = gr.ChatMessage(
|
211 |
-
role="assistant",
|
212 |
-
content=parts[1]
|
213 |
-
)
|
214 |
-
elif len(parts) > 1 and not completion_started:
|
215 |
-
completion_started = True
|
216 |
-
history.append(gr.ChatMessage(
|
217 |
-
role="assistant",
|
218 |
-
content=parts[1]
|
219 |
-
))
|
220 |
-
else:
|
221 |
-
if output.endswith("<|end|>"):
|
222 |
-
output = output.replace("<|end|>", "")
|
223 |
-
history[-1] = gr.ChatMessage(
|
224 |
-
role="assistant",
|
225 |
-
content=output
|
226 |
-
)
|
227 |
|
228 |
-
# log_message(f"Yielding messages: {history}")
|
229 |
-
yield history, INPUT_DISABLED, SEND_BUTTON_DISABLED, STOP_BUTTON_ENABLED, BUTTON_DISABLED, state
|
230 |
|
231 |
-
|
232 |
-
|
233 |
-
yield history, INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED, state
|
234 |
-
finally:
|
235 |
-
if error is None:
|
236 |
-
log_debug(f"chat_fn() --> Finished streaming. {chat_start_count} chats started.")
|
237 |
-
if state["opt_out"] is not True:
|
238 |
-
log_chat(chat_id=state["chat_id"],
|
239 |
-
session_id=state["session"],
|
240 |
-
model_name=model_name,
|
241 |
-
prompt=message,
|
242 |
-
history=history,
|
243 |
-
info={"is_reasoning": model_config.get("REASONING"), "temperature": MODEL_TEMPERATURE,
|
244 |
-
"stopped": state["stop_flag"]},
|
245 |
-
)
|
246 |
|
247 |
-
|
248 |
-
log_info(f"User opted out of chat history. Not logging chat. model: {model_name}")
|
249 |
-
state["is_streaming"] = False
|
250 |
-
state["stop_flag"] = False
|
251 |
-
return history, INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED, state
|
252 |
-
|
253 |
-
|
254 |
-
log_info(f"Gradio version: {gr.__version__}")
|
255 |
-
|
256 |
-
title = None
|
257 |
-
description = None
|
258 |
-
theme = apriel
|
259 |
-
|
260 |
-
with open('styles.css', 'r') as f:
|
261 |
-
custom_css = f.read()
|
262 |
-
|
263 |
-
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
264 |
-
session_state = gr.State(value={
|
265 |
-
"is_streaming": False,
|
266 |
-
"stop_flag": False,
|
267 |
-
"chat_id": None,
|
268 |
-
"session": None,
|
269 |
-
"opt_out": DEFAULT_OPT_OUT_VALUE,
|
270 |
-
}) # Store session state as a dictionary
|
271 |
-
|
272 |
-
gr.HTML(f"""
|
273 |
-
<style>
|
274 |
-
@media (min-width: 1024px) {{
|
275 |
-
.send-button-container, .clear-button-container {{
|
276 |
-
max-width: {BUTTON_WIDTH}px;
|
277 |
-
}}
|
278 |
-
}}
|
279 |
-
</style>
|
280 |
-
""", elem_classes="css-styles")
|
281 |
-
with gr.Row(variant="panel", elem_classes="responsive-row"):
|
282 |
-
with gr.Column(scale=1, min_width=400, elem_classes="model-dropdown-container"):
|
283 |
-
model_dropdown = gr.Dropdown(
|
284 |
-
choices=[f"Model: {model}" for model in models_config.keys()],
|
285 |
-
value=f"Model: {DEFAULT_MODEL_NAME}",
|
286 |
-
label=None,
|
287 |
-
interactive=True,
|
288 |
-
container=False,
|
289 |
-
scale=0,
|
290 |
-
min_width=400
|
291 |
-
)
|
292 |
-
with gr.Column(scale=4, min_width=0):
|
293 |
-
feedback_message_html = gr.HTML(description, elem_classes="model-message")
|
294 |
-
|
295 |
-
chatbot = gr.Chatbot(
|
296 |
-
type="messages",
|
297 |
-
height="calc(100dvh - 310px)",
|
298 |
-
elem_classes="chatbot",
|
299 |
-
)
|
300 |
-
|
301 |
-
with gr.Row():
|
302 |
-
with gr.Column(scale=10, min_width=400):
|
303 |
-
with gr.Row():
|
304 |
-
user_input = gr.Textbox(
|
305 |
-
show_label=False,
|
306 |
-
placeholder="Type your message here and press Enter",
|
307 |
-
container=False
|
308 |
-
)
|
309 |
-
with gr.Column(scale=1, min_width=BUTTON_WIDTH * 2 + 20):
|
310 |
-
with gr.Row():
|
311 |
-
with gr.Column(scale=1, min_width=BUTTON_WIDTH, elem_classes="send-button-container"):
|
312 |
-
send_btn = gr.Button("Send", variant="primary")
|
313 |
-
stop_btn = gr.Button("Stop", variant="cancel", visible=False)
|
314 |
-
with gr.Column(scale=1, min_width=BUTTON_WIDTH, elem_classes="clear-button-container"):
|
315 |
-
clear_btn = gr.ClearButton(chatbot, value="New Chat", variant="secondary")
|
316 |
-
with gr.Row():
|
317 |
-
with gr.Column(min_width=400, elem_classes="opt-out-container"):
|
318 |
-
with gr.Row():
|
319 |
-
gr.HTML(
|
320 |
-
"We may use your chats to improve our AI. You may opt out if you don’t want your conversations saved.",
|
321 |
-
elem_classes="opt-out-message")
|
322 |
-
with gr.Row():
|
323 |
-
opt_out_checkbox = gr.Checkbox(
|
324 |
-
label="Don’t save my chat history for improvements or training",
|
325 |
-
value=DEFAULT_OPT_OUT_VALUE,
|
326 |
-
elem_classes="opt-out-checkbox",
|
327 |
-
interactive=True,
|
328 |
-
container=False
|
329 |
-
)
|
330 |
-
|
331 |
-
gr.on(
|
332 |
-
triggers=[send_btn.click, user_input.submit],
|
333 |
-
fn=run_chat_inference, # this generator streams results. do not use logged_event_handler wrapper
|
334 |
-
inputs=[chatbot, user_input, session_state],
|
335 |
-
outputs=[chatbot, user_input, send_btn, stop_btn, clear_btn, session_state],
|
336 |
-
concurrency_limit=4,
|
337 |
-
api_name=False
|
338 |
-
).then(
|
339 |
-
fn=chat_finished, inputs=None, outputs=[model_dropdown, user_input, send_btn, stop_btn, clear_btn], queue=False)
|
340 |
-
|
341 |
-
# In parallel, disable or update the UI controls
|
342 |
-
gr.on(
|
343 |
-
triggers=[send_btn.click, user_input.submit],
|
344 |
-
fn=chat_started,
|
345 |
-
inputs=None,
|
346 |
-
outputs=[model_dropdown, user_input, send_btn, stop_btn, clear_btn],
|
347 |
-
queue=False,
|
348 |
-
show_progress='hidden',
|
349 |
-
api_name=False
|
350 |
-
)
|
351 |
-
|
352 |
-
stop_btn.click(
|
353 |
-
fn=stop_chat,
|
354 |
-
inputs=[session_state],
|
355 |
-
outputs=[session_state],
|
356 |
-
api_name=False
|
357 |
-
)
|
358 |
-
|
359 |
-
opt_out_checkbox.change(fn=toggle_opt_out, inputs=[session_state, opt_out_checkbox], outputs=[session_state])
|
360 |
-
|
361 |
-
# Ensure the model is reset to default on page reload
|
362 |
-
demo.load(
|
363 |
-
fn=logged_event_handler(
|
364 |
-
log_msg="Browser session started",
|
365 |
-
event_handler=app_loaded
|
366 |
-
),
|
367 |
-
inputs=[session_state],
|
368 |
-
outputs=[session_state, feedback_message_html],
|
369 |
-
queue=True,
|
370 |
-
api_name=False
|
371 |
-
)
|
372 |
-
|
373 |
-
model_dropdown.change(
|
374 |
-
fn=update_model_and_clear_chat,
|
375 |
-
inputs=[model_dropdown],
|
376 |
-
outputs=[feedback_message_html, chatbot],
|
377 |
-
api_name=False
|
378 |
-
)
|
379 |
|
380 |
-
|
381 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
import datetime
|
|
|
4 |
|
5 |
from openai import OpenAI
|
6 |
import gradio as gr
|
7 |
+
from gradio.components.chatbot import ChatMessage, Message
|
8 |
+
from typing import (
|
9 |
+
Any,
|
10 |
+
Literal,
|
11 |
+
)
|
12 |
|
13 |
+
DEBUG_LOG = False or os.environ.get("DEBUG_LOG") == "True"
|
|
|
|
|
|
|
14 |
|
15 |
+
print(f"Gradio version: {gr.__version__}")
|
|
|
|
|
16 |
|
17 |
+
title = None # "ServiceNow-AI Chat" # modelConfig.get('MODE_DISPLAY_NAME')
|
18 |
+
description = "Please use the community section on this space to provide feedback! <a href=\"https://huggingface.co/ServiceNow-AI/Apriel-Nemotron-15b-Thinker/discussions\">ServiceNow-AI/Apriel-Nemotron-Chat</a>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
chat_start_count = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
model_config = {
|
23 |
+
"MODEL_NAME": os.environ.get("MODEL_NAME"),
|
24 |
+
"MODE_DISPLAY_NAME": os.environ.get("MODE_DISPLAY_NAME"),
|
25 |
+
"MODEL_HF_URL": os.environ.get("MODEL_HF_URL"),
|
26 |
+
"VLLM_API_URL": os.environ.get("VLLM_API_URL"),
|
27 |
+
"AUTH_TOKEN": os.environ.get("AUTH_TOKEN")
|
28 |
+
}
|
29 |
+
|
30 |
+
# Initialize the OpenAI client with the vLLM API URL and token
|
31 |
+
client = OpenAI(
|
32 |
+
api_key=model_config.get('AUTH_TOKEN'),
|
33 |
+
base_url=model_config.get('VLLM_API_URL')
|
34 |
+
)
|
35 |
+
|
36 |
+
|
37 |
+
def log_message(message):
|
38 |
+
if DEBUG_LOG is True:
|
39 |
+
print(message)
|
40 |
+
|
41 |
+
|
42 |
+
# Gradio 5.0.1 had issues with checking the message formats. 5.29.0 does not!
|
43 |
+
def _check_format(messages: Any, type: Literal["messages", "tuples"] = "messages") -> None:
|
44 |
+
if type == "messages":
|
45 |
+
all_valid = all(
|
46 |
+
isinstance(message, dict)
|
47 |
+
and "role" in message
|
48 |
+
and "content" in message
|
49 |
+
or isinstance(message, ChatMessage | Message)
|
50 |
+
for message in messages
|
51 |
+
)
|
52 |
+
if not all_valid:
|
53 |
+
# Display which message is not valid
|
54 |
+
for i, message in enumerate(messages):
|
55 |
+
if not (isinstance(message, dict) and
|
56 |
+
"role" in message and
|
57 |
+
"content" in message) and not isinstance(message, ChatMessage | Message):
|
58 |
+
print(f"_check_format() --> Invalid message at index {i}: {message}\n", file=sys.stderr)
|
59 |
+
break
|
60 |
+
|
61 |
+
raise Exception(
|
62 |
+
"Data incompatible with messages format. Each message should be a dictionary with 'role' and 'content' keys or a ChatMessage object."
|
63 |
+
)
|
64 |
+
# else:
|
65 |
+
# print("_check_format() --> All messages are valid.")
|
66 |
+
elif not all(
|
67 |
+
isinstance(message, (tuple, list)) and len(message) == 2
|
68 |
+
for message in messages
|
69 |
+
):
|
70 |
+
raise Exception(
|
71 |
+
"Data incompatible with tuples format. Each message should be a list of length 2."
|
72 |
+
)
|
73 |
|
|
|
|
|
|
|
74 |
|
75 |
+
def chat_fn(message, history):
|
76 |
+
log_message(f"{'-' * 80}\nchat_fn() --> Message: {message}")
|
77 |
|
|
|
78 |
global chat_start_count
|
79 |
+
chat_start_count = chat_start_count + 1
|
80 |
+
print(
|
81 |
+
f"{datetime.datetime.now()}: chat_start_count: {chat_start_count}, turns: {int(len(history if history else []) / 3)}")
|
82 |
+
|
83 |
+
# Remove any assistant messages with metadata from history for multiple turns
|
84 |
+
log_message(f"Original History: {history}")
|
85 |
+
_check_format(history, "messages")
|
86 |
+
history = [item for item in history if
|
87 |
+
not (isinstance(item, dict) and
|
88 |
+
item.get("role") == "assistant" and
|
89 |
+
isinstance(item.get("metadata"), dict) and
|
90 |
+
item.get("metadata", {}).get("title") is not None)]
|
91 |
+
log_message(f"Updated History: {history}")
|
92 |
+
_check_format(history, "messages")
|
93 |
+
|
94 |
+
history.append({"role": "user", "content": message})
|
95 |
+
log_message(f"History with user message: {history}")
|
96 |
+
_check_format(history, "messages")
|
97 |
+
|
98 |
+
# Create the streaming response
|
99 |
+
stream = client.chat.completions.create(
|
100 |
+
model=model_config.get('MODEL_NAME'),
|
101 |
+
messages=history,
|
102 |
+
temperature=0.8,
|
103 |
+
stream=True
|
104 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
history.append(gr.ChatMessage(
|
107 |
+
role="assistant",
|
108 |
+
content="Thinking...",
|
109 |
+
metadata={"title": "🧠 Thought"}
|
110 |
+
))
|
111 |
+
log_message(f"History added thinking: {history}")
|
112 |
+
_check_format(history, "messages")
|
113 |
+
|
114 |
+
output = ""
|
115 |
+
completion_started = False
|
116 |
+
for chunk in stream:
|
117 |
+
# Extract the new content from the delta field
|
118 |
+
content = getattr(chunk.choices[0].delta, "content", "")
|
119 |
+
output += content
|
120 |
+
|
121 |
+
parts = output.split("[BEGIN FINAL RESPONSE]")
|
122 |
+
|
123 |
+
if len(parts) > 1:
|
124 |
+
if parts[1].endswith("[END FINAL RESPONSE]"):
|
125 |
+
parts[1] = parts[1].replace("[END FINAL RESPONSE]", "")
|
126 |
+
if parts[1].endswith("[END FINAL RESPONSE]\n<|end|>"):
|
127 |
+
parts[1] = parts[1].replace("[END FINAL RESPONSE]\n<|end|>", "")
|
128 |
+
|
129 |
+
history[-1 if not completion_started else -2] = gr.ChatMessage(
|
130 |
+
role="assistant",
|
131 |
+
content=parts[0],
|
132 |
+
metadata={"title": "🧠 Thought"}
|
133 |
+
)
|
134 |
+
if completion_started:
|
135 |
+
history[-1] = gr.ChatMessage(
|
136 |
role="assistant",
|
137 |
+
content=parts[1]
|
138 |
+
)
|
139 |
+
elif len(parts) > 1 and not completion_started:
|
140 |
+
completion_started = True
|
|
|
|
|
141 |
history.append(gr.ChatMessage(
|
142 |
role="assistant",
|
143 |
+
content=parts[1]
|
144 |
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
+
# only yield the most recent assistant messages
|
147 |
+
messages_to_yield = history[-1:] if not completion_started else history[-2:]
|
148 |
+
# _check_format(messages_to_yield, "messages")
|
149 |
+
yield messages_to_yield
|
|
|
|
|
|
|
150 |
|
151 |
+
log_message(f"Final History: {history}")
|
152 |
+
_check_format(history, "messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
|
|
|
|
154 |
|
155 |
+
# Add the model display name and Hugging Face URL to the description
|
156 |
+
# description = f"### Model: [{MODE_DISPLAY_NAME}]({MODEL_HF_URL})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
+
print(f"Running model {model_config.get('MODE_DISPLAY_NAME')} ({model_config.get('MODEL_NAME')})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
+
gr.ChatInterface(
|
161 |
+
chat_fn,
|
162 |
+
title=title,
|
163 |
+
description=description,
|
164 |
+
theme=gr.themes.Default(primary_hue="green"),
|
165 |
+
type="messages",
|
166 |
+
).launch()
|
gradio_runner.py
DELETED
@@ -1,10 +0,0 @@
|
|
1 |
-
import re
|
2 |
-
import sys
|
3 |
-
from gradio.cli import cli
|
4 |
-
|
5 |
-
# This runs a gradio app so that it can be automatically reloaded in the browser
|
6 |
-
# Example: python gradio_runner.py app.py
|
7 |
-
|
8 |
-
if __name__ == '__main__':
|
9 |
-
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
10 |
-
sys.exit(cli())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log_chat.py
DELETED
@@ -1,237 +0,0 @@
|
|
1 |
-
import csv
|
2 |
-
import os
|
3 |
-
import time
|
4 |
-
from datetime import datetime
|
5 |
-
from queue import Queue
|
6 |
-
import threading
|
7 |
-
|
8 |
-
import pandas as pd
|
9 |
-
from gradio import ChatMessage
|
10 |
-
from huggingface_hub import HfApi, hf_hub_download
|
11 |
-
|
12 |
-
from timer import Timer
|
13 |
-
from utils import log_warning, log_info, log_debug, log_error
|
14 |
-
|
15 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
16 |
-
DATASET_REPO_ID = os.environ.get("APRIEL_PROMPT_DATASET")
|
17 |
-
CSV_FILENAME = "train.csv"
|
18 |
-
|
19 |
-
|
20 |
-
def log_chat(chat_id: str, session_id: str, model_name: str, prompt: str, history: list[str], info: dict) -> None:
|
21 |
-
log_info(f"log_chat() called for chat: {chat_id}, queue size: {log_chat_queue.qsize()}, model: {model_name}")
|
22 |
-
log_chat_queue.put((chat_id, session_id, model_name, prompt, history, info))
|
23 |
-
|
24 |
-
|
25 |
-
def _log_chat_worker():
|
26 |
-
while True:
|
27 |
-
chat_id, session_id, model_name, prompt, history, info = log_chat_queue.get()
|
28 |
-
try:
|
29 |
-
try:
|
30 |
-
_log_chat(chat_id, session_id, model_name, prompt, history, info)
|
31 |
-
except Exception as e:
|
32 |
-
log_error(f"Error logging chat: {e}")
|
33 |
-
finally:
|
34 |
-
log_chat_queue.task_done()
|
35 |
-
|
36 |
-
|
37 |
-
def _log_chat(chat_id: str, session_id: str, model_name: str, prompt: str, history: list[str], info: dict) -> bool:
|
38 |
-
log_info(f"_log_chat() storing chat {chat_id}")
|
39 |
-
if DATASET_REPO_ID is None:
|
40 |
-
log_warning("No dataset repo ID provided. Skipping logging of prompt.")
|
41 |
-
return False
|
42 |
-
if HF_TOKEN is None:
|
43 |
-
log_warning("No HF token provided. Skipping logging of prompt.")
|
44 |
-
return False
|
45 |
-
|
46 |
-
log_timer = Timer('log_chat')
|
47 |
-
log_timer.start()
|
48 |
-
|
49 |
-
# Initialize HF API
|
50 |
-
api = HfApi(token=HF_TOKEN)
|
51 |
-
|
52 |
-
# Check if the dataset repo exists, if not, create it
|
53 |
-
try:
|
54 |
-
repo_info = api.repo_info(repo_id=DATASET_REPO_ID, repo_type="dataset")
|
55 |
-
log_debug(f"log_chat() --> Dataset repo found: {repo_info.id} private={repo_info.private}")
|
56 |
-
except Exception: # Create new dataset if none exists
|
57 |
-
log_debug(f"log_chat() --> No dataset repo found, creating a new one...")
|
58 |
-
api.create_repo(repo_id=DATASET_REPO_ID, repo_type="dataset", private=True)
|
59 |
-
|
60 |
-
# Ensure messages are in the correct format
|
61 |
-
messages = [
|
62 |
-
{"role": item.role, "content": item.content,
|
63 |
-
"type": "thought" if item.metadata else "completion"} if isinstance(
|
64 |
-
item, ChatMessage) else item
|
65 |
-
for item in history
|
66 |
-
if isinstance(item, dict) and "role" in item and "content" in item or isinstance(item, ChatMessage)
|
67 |
-
]
|
68 |
-
if len(messages) != len(history):
|
69 |
-
log_warning("log_chat() --> Some messages in history are missing 'role' or 'content' keys.")
|
70 |
-
|
71 |
-
user_messages_count = sum(1 for item in messages if isinstance(item, dict) and item.get("role") == "user")
|
72 |
-
|
73 |
-
# These must match the keys in the new row
|
74 |
-
expected_headers = ["timestamp", "chat_id", "turns", "prompt", "messages", "model", "session_id", "info"]
|
75 |
-
# Prepare new data row
|
76 |
-
new_row = {
|
77 |
-
"timestamp": datetime.now().isoformat(),
|
78 |
-
"chat_id": chat_id,
|
79 |
-
"turns": user_messages_count,
|
80 |
-
"prompt": prompt,
|
81 |
-
"messages": messages,
|
82 |
-
"model": model_name,
|
83 |
-
"session_id": session_id,
|
84 |
-
"info": info,
|
85 |
-
}
|
86 |
-
log_timer.add_step("Prepared new data row")
|
87 |
-
|
88 |
-
# Try to download existing CSV with retry logic
|
89 |
-
max_retries = 3
|
90 |
-
retry_count = 0
|
91 |
-
file_exists = False
|
92 |
-
while retry_count < max_retries:
|
93 |
-
try:
|
94 |
-
csv_path = hf_hub_download(
|
95 |
-
repo_id=DATASET_REPO_ID,
|
96 |
-
filename=CSV_FILENAME,
|
97 |
-
repo_type="dataset",
|
98 |
-
token=HF_TOKEN # Only needed if not already logged in
|
99 |
-
)
|
100 |
-
pd.read_csv(csv_path)
|
101 |
-
file_exists = True
|
102 |
-
log_debug(f"log_chat() --> Downloaded existing CSV with {len(pd.read_csv(csv_path))} rows")
|
103 |
-
break # Success, exit the loop
|
104 |
-
except Exception as e:
|
105 |
-
retry_count += 1
|
106 |
-
if retry_count < max_retries:
|
107 |
-
retry_delay = 2 * retry_count # Exponential backoff: 2s, 4s, 6s, 8s
|
108 |
-
log_warning(
|
109 |
-
f"log_chat() --> Download attempt {retry_count} failed: {e}. Retrying in {retry_delay} seconds...")
|
110 |
-
time.sleep(retry_delay)
|
111 |
-
else:
|
112 |
-
log_warning(f"log_chat() --> Failed to download CSV after {max_retries} attempts: {e}")
|
113 |
-
file_exists = False
|
114 |
-
|
115 |
-
log_timer.add_step(f"Downloaded existing CSV (attempts: {retry_count + 1})")
|
116 |
-
|
117 |
-
# Handle the case where the CSV file does not exist or is invalid
|
118 |
-
if file_exists and len(pd.read_csv(csv_path)) == 0:
|
119 |
-
log_warning(f"log_chat() --> CSV {csv_path} exists but is empty, will create a new one.")
|
120 |
-
dump_hub_csv()
|
121 |
-
file_exists = False
|
122 |
-
elif file_exists:
|
123 |
-
# Check that the headers match our standard headers of "timestamp", "chat_id", "turns", ...
|
124 |
-
existing_headers = pd.read_csv(csv_path).columns.tolist()
|
125 |
-
if set(existing_headers) != set(expected_headers):
|
126 |
-
log_warning(f"log_chat() --> CSV {csv_path} has unexpected headers: {existing_headers}. "
|
127 |
-
f"\nExpected {existing_headers} "
|
128 |
-
f"Will create a new one.")
|
129 |
-
dump_hub_csv()
|
130 |
-
file_exists = False
|
131 |
-
else:
|
132 |
-
log_debug(f"log_chat() --> CSV {csv_path} has expected headers: {existing_headers}")
|
133 |
-
|
134 |
-
# Write out the new row to the CSV file (append isn't working in HF container, so recreate each time)
|
135 |
-
log_debug(f"log_chat() --> Writing CSV file, file_exists={file_exists}")
|
136 |
-
try:
|
137 |
-
with open(CSV_FILENAME, "w", newline="\n") as f:
|
138 |
-
writer = csv.DictWriter(f, fieldnames=new_row.keys())
|
139 |
-
writer.writeheader() # Always write the header
|
140 |
-
if file_exists:
|
141 |
-
for _, row in pd.read_csv(csv_path).iterrows():
|
142 |
-
writer.writerow(row.to_dict()) # Write existing rows
|
143 |
-
writer.writerow(new_row) # Write the new row
|
144 |
-
|
145 |
-
log_debug("log_chat() --> Wrote out CSV with new row")
|
146 |
-
# dump_local_csv()
|
147 |
-
except Exception as e:
|
148 |
-
log_error(f"log_chat() --> Error writing to CSV: {e}")
|
149 |
-
return False
|
150 |
-
|
151 |
-
# Upload updated CSV
|
152 |
-
api.upload_file(
|
153 |
-
path_or_fileobj=CSV_FILENAME,
|
154 |
-
path_in_repo=CSV_FILENAME,
|
155 |
-
repo_id=DATASET_REPO_ID,
|
156 |
-
repo_type="dataset",
|
157 |
-
commit_message=f"Added new chat entry at {datetime.now().isoformat()}"
|
158 |
-
)
|
159 |
-
log_timer.add_step("Uploaded updated CSV")
|
160 |
-
log_timer.end()
|
161 |
-
log_debug("log_chat() --> Finished logging chat")
|
162 |
-
log_debug(log_timer.formatted_result())
|
163 |
-
|
164 |
-
return True
|
165 |
-
|
166 |
-
|
167 |
-
def dump_hub_csv():
|
168 |
-
# Verify the file contents by loading it from the hub and printing it out
|
169 |
-
try:
|
170 |
-
csv_path = hf_hub_download(
|
171 |
-
repo_id=DATASET_REPO_ID,
|
172 |
-
filename=CSV_FILENAME,
|
173 |
-
repo_type="dataset",
|
174 |
-
token=HF_TOKEN # Only needed if not already logged in
|
175 |
-
)
|
176 |
-
df = pd.read_csv(csv_path)
|
177 |
-
log_info(df)
|
178 |
-
if (df.empty):
|
179 |
-
# show raw contents of downloaded csv file
|
180 |
-
log_info("Raw file contents:")
|
181 |
-
with open(csv_path, 'r') as f:
|
182 |
-
print(f.read())
|
183 |
-
except Exception as e:
|
184 |
-
log_error(f"Error loading CSV from hub: {e}")
|
185 |
-
|
186 |
-
|
187 |
-
def dump_local_csv():
|
188 |
-
# Verify the file contents by loading it from the local file and printing it out
|
189 |
-
try:
|
190 |
-
df = pd.read_csv(CSV_FILENAME)
|
191 |
-
log_info(df)
|
192 |
-
except Exception as e:
|
193 |
-
log_error(f"Error loading CSV from local file: {e}")
|
194 |
-
|
195 |
-
|
196 |
-
def test_log_chat():
|
197 |
-
# Example usage
|
198 |
-
chat_id = "12345"
|
199 |
-
session_id = "67890"
|
200 |
-
model_name = "Apriel-Model"
|
201 |
-
prompt = "Hello"
|
202 |
-
history = [{"role": "user", "content": prompt}, {"role": "assistant", "content": "Hi there!"}]
|
203 |
-
prompt = "100 + 1"
|
204 |
-
history = [{'role': 'user', 'content': prompt}, ChatMessage(
|
205 |
-
content='Okay, that\'s a simple addition problem. , answer is 2.\n', role='assistant',
|
206 |
-
metadata={'title': '🧠 Thought'}, options=[]),
|
207 |
-
ChatMessage(content='\nThe result of adding 1 and 1 is:\n\n**2**\n', role='assistant', metadata={},
|
208 |
-
options=[])
|
209 |
-
]
|
210 |
-
info = {"additional_info": "Some extra data"}
|
211 |
-
|
212 |
-
log_debug("Starting test_log_chat()")
|
213 |
-
dump_hub_csv()
|
214 |
-
log_chat(chat_id, session_id, model_name, prompt, history, info)
|
215 |
-
log_debug("log_chat 1 returned")
|
216 |
-
log_chat(chat_id, session_id, model_name, prompt + " + 2", history, info)
|
217 |
-
log_debug("log_chat 2 returned")
|
218 |
-
log_chat(chat_id, session_id, model_name, prompt + " + 3", history, info)
|
219 |
-
log_debug("log_chat 3 returned")
|
220 |
-
log_chat(chat_id, session_id, model_name, prompt + " + 4", history, info)
|
221 |
-
log_debug("log_chat 4 returned")
|
222 |
-
|
223 |
-
sleep_seconds = 10
|
224 |
-
log_debug(f"Sleeping {sleep_seconds} seconds to let it finish and log the result.")
|
225 |
-
time.sleep(sleep_seconds)
|
226 |
-
log_debug("Finished sleeping.")
|
227 |
-
dump_hub_csv()
|
228 |
-
|
229 |
-
|
230 |
-
# Create a queue for logging chat messages
|
231 |
-
log_chat_queue = Queue()
|
232 |
-
|
233 |
-
# Start the worker thread
|
234 |
-
threading.Thread(target=_log_chat_worker, daemon=True).start()
|
235 |
-
|
236 |
-
if __name__ == "__main__":
|
237 |
-
test_log_chat()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
huggingface_hub==0.28.1
|
2 |
gradio==5.29.0
|
3 |
-
openai
|
4 |
-
pandas~=2.2.3
|
5 |
-
datasets~=2.14.4
|
|
|
1 |
huggingface_hub==0.28.1
|
2 |
gradio==5.29.0
|
3 |
+
openai
|
|
|
|
styles.css
DELETED
@@ -1,118 +0,0 @@
|
|
1 |
-
:root {
|
2 |
-
--color-grey-50: #f9fafb;
|
3 |
-
}
|
4 |
-
|
5 |
-
.toast-body {
|
6 |
-
background-color: var(--color-grey-50);
|
7 |
-
}
|
8 |
-
|
9 |
-
.html-container:has(.css-styles) {
|
10 |
-
padding: 0;
|
11 |
-
margin: 0;
|
12 |
-
}
|
13 |
-
|
14 |
-
.css-styles {
|
15 |
-
height: 0;
|
16 |
-
}
|
17 |
-
|
18 |
-
.model-message {
|
19 |
-
text-align: end;
|
20 |
-
}
|
21 |
-
|
22 |
-
.model-dropdown-container {
|
23 |
-
display: flex;
|
24 |
-
align-items: center;
|
25 |
-
gap: 10px;
|
26 |
-
padding: 0;
|
27 |
-
}
|
28 |
-
|
29 |
-
.chatbot {
|
30 |
-
max-height: 1400px;
|
31 |
-
}
|
32 |
-
|
33 |
-
button.cancel {
|
34 |
-
border: var(--button-border-width) solid var(--button-cancel-border-color);
|
35 |
-
background: var(--button-cancel-background-fill);
|
36 |
-
color: var(--button-cancel-text-color);
|
37 |
-
box-shadow: var(--button-cancel-shadow);
|
38 |
-
}
|
39 |
-
|
40 |
-
button.cancel:hover, .cancel[disabled] {
|
41 |
-
background: var(--button-cancel-background-fill-hover);
|
42 |
-
color: var(--button-cancel-text-color-hover);
|
43 |
-
}
|
44 |
-
|
45 |
-
.opt-out-message {
|
46 |
-
top: 8px;
|
47 |
-
}
|
48 |
-
|
49 |
-
.opt-out-message .html-container, .opt-out-checkbox label {
|
50 |
-
font-size: 14px !important;
|
51 |
-
padding: 0 !important;
|
52 |
-
margin: 0 !important;
|
53 |
-
color: var(--neutral-400) !important;
|
54 |
-
}
|
55 |
-
|
56 |
-
@media (max-width: 800px) {
|
57 |
-
.responsive-row {
|
58 |
-
flex-direction: column;
|
59 |
-
}
|
60 |
-
|
61 |
-
.model-message {
|
62 |
-
text-align: start;
|
63 |
-
font-size: 10px !important;
|
64 |
-
}
|
65 |
-
|
66 |
-
.model-dropdown-container {
|
67 |
-
flex-direction: column;
|
68 |
-
align-items: flex-start;
|
69 |
-
}
|
70 |
-
|
71 |
-
.chatbot {
|
72 |
-
max-height: 800px;
|
73 |
-
}
|
74 |
-
}
|
75 |
-
|
76 |
-
@media (max-width: 400px) {
|
77 |
-
.responsive-row {
|
78 |
-
flex-direction: column;
|
79 |
-
}
|
80 |
-
|
81 |
-
.model-message {
|
82 |
-
text-align: start;
|
83 |
-
font-size: 10px !important;
|
84 |
-
}
|
85 |
-
|
86 |
-
.model-dropdown-container {
|
87 |
-
flex-direction: column;
|
88 |
-
align-items: flex-start;
|
89 |
-
}
|
90 |
-
|
91 |
-
.chatbot {
|
92 |
-
max-height: 360px;
|
93 |
-
}
|
94 |
-
}
|
95 |
-
|
96 |
-
@media (max-width: 1280px) {
|
97 |
-
.chatbot {
|
98 |
-
max-height: 900px;
|
99 |
-
}
|
100 |
-
}
|
101 |
-
|
102 |
-
@media (max-height: 932px) {
|
103 |
-
.chatbot {
|
104 |
-
max-height: calc(100dvh - 400px);
|
105 |
-
}
|
106 |
-
}
|
107 |
-
|
108 |
-
@media (max-height: 1280px) {
|
109 |
-
.chatbot {
|
110 |
-
max-height: calc(100dvh - 400px);
|
111 |
-
}
|
112 |
-
}
|
113 |
-
|
114 |
-
@media (min-height: 1281px) {
|
115 |
-
.chatbot {
|
116 |
-
/*max-height: calc(100dvh - 400px);*/
|
117 |
-
}
|
118 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
theme.py
DELETED
@@ -1,148 +0,0 @@
|
|
1 |
-
from typing import Iterable
|
2 |
-
from gradio.themes import Soft
|
3 |
-
from gradio.themes.utils import colors, fonts, sizes
|
4 |
-
|
5 |
-
colors.teal_gray = colors.Color(
|
6 |
-
name="teal_gray",
|
7 |
-
c50="#e8f1f4",
|
8 |
-
c100="#cddde3",
|
9 |
-
c200="#a8c3cf",
|
10 |
-
c300="#7da6b8",
|
11 |
-
c400="#588aa2",
|
12 |
-
c500="#3d6e87",
|
13 |
-
c600="#335b70",
|
14 |
-
c700="#2b495a",
|
15 |
-
c800="#2c5364",
|
16 |
-
c900="#233f4b",
|
17 |
-
c950="#1b323c",
|
18 |
-
)
|
19 |
-
|
20 |
-
colors.red_gray = colors.Color(
|
21 |
-
name="red_gray",
|
22 |
-
c50="#f7eded",
|
23 |
-
c100="#f5dcdc",
|
24 |
-
c200="#efb4b4",
|
25 |
-
c300="#e78f8f",
|
26 |
-
c400="#d96a6a",
|
27 |
-
c500="#c65353",
|
28 |
-
c600="#b24444",
|
29 |
-
c700="#8f3434",
|
30 |
-
c800="#732d2d",
|
31 |
-
c900="#5f2626",
|
32 |
-
c950="#4d2020",
|
33 |
-
)
|
34 |
-
|
35 |
-
|
36 |
-
class Apriel(Soft):
|
37 |
-
def __init__(
|
38 |
-
self,
|
39 |
-
*,
|
40 |
-
primary_hue: colors.Color | str = colors.gray,
|
41 |
-
secondary_hue: colors.Color | str = colors.teal_gray,
|
42 |
-
neutral_hue: colors.Color | str = colors.slate,
|
43 |
-
# spacing_size: sizes.Size | str = sizes.spacing_md,
|
44 |
-
# radius_size: sizes.Size | str = sizes.radius_md,
|
45 |
-
text_size: sizes.Size | str = sizes.text_md,
|
46 |
-
font: fonts.Font
|
47 |
-
| str
|
48 |
-
| Iterable[fonts.Font | str] = (
|
49 |
-
fonts.GoogleFont("Inconsolata"),
|
50 |
-
"Arial",
|
51 |
-
"sans-serif",
|
52 |
-
),
|
53 |
-
font_mono: fonts.Font
|
54 |
-
| str
|
55 |
-
| Iterable[fonts.Font | str] = (
|
56 |
-
fonts.GoogleFont("IBM Plex Mono"),
|
57 |
-
"ui-monospace",
|
58 |
-
"monospace",
|
59 |
-
),
|
60 |
-
):
|
61 |
-
super().__init__(
|
62 |
-
primary_hue=primary_hue,
|
63 |
-
secondary_hue=secondary_hue,
|
64 |
-
neutral_hue=neutral_hue,
|
65 |
-
# spacing_size=spacing_size,
|
66 |
-
# radius_size=radius_size,
|
67 |
-
text_size=text_size,
|
68 |
-
font=font,
|
69 |
-
font_mono=font_mono,
|
70 |
-
)
|
71 |
-
super().set(
|
72 |
-
background_fill_primary="*primary_50",
|
73 |
-
background_fill_primary_dark="*primary_900",
|
74 |
-
|
75 |
-
body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
|
76 |
-
body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
|
77 |
-
|
78 |
-
button_primary_text_color="white",
|
79 |
-
button_primary_text_color_hover="black",
|
80 |
-
button_primary_background_fill="linear-gradient(90deg, *secondary_400, *secondary_400)",
|
81 |
-
button_primary_background_fill_hover="linear-gradient(90deg, *secondary_300, *secondary_300)",
|
82 |
-
button_primary_background_fill_dark="linear-gradient(90deg, *secondary_600, *secondary_800)",
|
83 |
-
button_primary_background_fill_hover_dark="linear-gradient(90deg, *secondary_500, *secondary_500)",
|
84 |
-
|
85 |
-
button_secondary_text_color="black",
|
86 |
-
button_secondary_text_color_hover="white",
|
87 |
-
button_secondary_background_fill="linear-gradient(90deg, *primary_300, *primary_300)",
|
88 |
-
button_secondary_background_fill_hover="linear-gradient(90deg, *primary_400, *primary_400)",
|
89 |
-
button_secondary_background_fill_dark="linear-gradient(90deg, *primary_500, *primary_600)",
|
90 |
-
button_secondary_background_fill_hover_dark="linear-gradient(90deg, *primary_500, *primary_500)",
|
91 |
-
|
92 |
-
button_cancel_background_fill=f"linear-gradient(90deg, {colors.red_gray.c400}, {colors.red_gray.c500})",
|
93 |
-
button_cancel_background_fill_dark=f"linear-gradient(90deg, {colors.red_gray.c700}, {colors.red_gray.c800})",
|
94 |
-
button_cancel_background_fill_hover=f"linear-gradient(90deg, {colors.red_gray.c500}, {colors.red_gray.c600})",
|
95 |
-
button_cancel_background_fill_hover_dark=f"linear-gradient(90deg, {colors.red_gray.c800}, {colors.red_gray.c900})",
|
96 |
-
# button_cancel_background_fill=f"linear-gradient(90deg, {colors.red.c400}, {colors.red.c500})",
|
97 |
-
# button_cancel_background_fill_dark=f"linear-gradient(90deg, {colors.red.c700}, {colors.red.c800})",
|
98 |
-
# button_cancel_background_fill_hover=f"linear-gradient(90deg, {colors.red.c500}, {colors.red.c600})",
|
99 |
-
# button_cancel_background_fill_hover_dark=f"linear-gradient(90deg, {colors.red.c800}, {colors.red.c900})",
|
100 |
-
button_cancel_text_color="white",
|
101 |
-
button_cancel_text_color_dark="white",
|
102 |
-
button_cancel_text_color_hover="white",
|
103 |
-
button_cancel_text_color_hover_dark="white",
|
104 |
-
|
105 |
-
# button_cancel_background_fill=colors.red.c500,
|
106 |
-
# button_cancel_background_fill_dark=colors.red.c700,
|
107 |
-
# button_cancel_background_fill_hover=colors.red.c600,
|
108 |
-
# button_cancel_background_fill_hover_dark=colors.red.c800,
|
109 |
-
# button_cancel_text_color="white",
|
110 |
-
# button_cancel_text_color_dark="white",
|
111 |
-
# button_cancel_text_color_hover="white",
|
112 |
-
# button_cancel_text_color_hover_dark="white",
|
113 |
-
|
114 |
-
slider_color="*secondary_300",
|
115 |
-
slider_color_dark="*secondary_600",
|
116 |
-
block_title_text_weight="600",
|
117 |
-
block_border_width="3px",
|
118 |
-
block_shadow="*shadow_drop_lg",
|
119 |
-
button_primary_shadow="*shadow_drop_lg",
|
120 |
-
button_large_padding="11px",
|
121 |
-
|
122 |
-
color_accent_soft="*primary_100",
|
123 |
-
|
124 |
-
block_label_background_fill="*primary_200",
|
125 |
-
|
126 |
-
)
|
127 |
-
|
128 |
-
|
129 |
-
apriel = Apriel()
|
130 |
-
|
131 |
-
# with gr.Blocks(theme=apriel) as demo:
|
132 |
-
# textbox = gr.Textbox(label="Name")
|
133 |
-
# slider = gr.Slider(label="Count", minimum=0, maximum=100, step=1)
|
134 |
-
# with gr.Row():
|
135 |
-
# button = gr.Button("Submit", variant="primary")
|
136 |
-
# clear = gr.Button("Clear")
|
137 |
-
# output = gr.Textbox(label="Output")
|
138 |
-
#
|
139 |
-
#
|
140 |
-
# def repeat(name, count):
|
141 |
-
# time.sleep(3)
|
142 |
-
# return name * count
|
143 |
-
#
|
144 |
-
#
|
145 |
-
# button.click(repeat, [textbox, slider], output)
|
146 |
-
#
|
147 |
-
# if __name__ == "__main__":
|
148 |
-
# demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
timer.py
DELETED
@@ -1,114 +0,0 @@
|
|
1 |
-
import time
|
2 |
-
import json
|
3 |
-
|
4 |
-
|
5 |
-
class Timer:
|
6 |
-
def __init__(self, name=None):
|
7 |
-
self.name = name
|
8 |
-
self.start_time = None
|
9 |
-
self.steps = []
|
10 |
-
self.total_time = None
|
11 |
-
|
12 |
-
def clear(self):
|
13 |
-
self.start_time = None
|
14 |
-
self.steps = []
|
15 |
-
self.total_time = None
|
16 |
-
|
17 |
-
def start(self):
|
18 |
-
"""Start the timer."""
|
19 |
-
self.start_time = time.time()
|
20 |
-
|
21 |
-
def is_running(self):
|
22 |
-
return self.start_time is not None
|
23 |
-
|
24 |
-
def add_step(self, step_name):
|
25 |
-
"""Add a step with its duration since the last step or start."""
|
26 |
-
if self.start_time is None:
|
27 |
-
self.start()
|
28 |
-
|
29 |
-
current_time = time.time()
|
30 |
-
if not self.steps:
|
31 |
-
elapsed = current_time - self.start_time
|
32 |
-
else:
|
33 |
-
elapsed = current_time - self.steps[-1]['timestamp']
|
34 |
-
|
35 |
-
self.steps.append({
|
36 |
-
"step_name": step_name,
|
37 |
-
"duration": round(elapsed, 4),
|
38 |
-
"total_duration": round(current_time - self.start_time, 4),
|
39 |
-
"timestamp": current_time
|
40 |
-
})
|
41 |
-
|
42 |
-
def end(self):
|
43 |
-
"""End the timer and calculate the total duration."""
|
44 |
-
if self.start_time is None:
|
45 |
-
raise RuntimeError("Timer has not been started.")
|
46 |
-
|
47 |
-
if not self.steps:
|
48 |
-
raise RuntimeError("No steps have been added.")
|
49 |
-
|
50 |
-
self.total_time = time.time() - self.start_time
|
51 |
-
|
52 |
-
def to_json(self):
|
53 |
-
"""Return a JSON of the timing steps."""
|
54 |
-
if self.total_time is None:
|
55 |
-
raise RuntimeError("Timer has not been ended.")
|
56 |
-
|
57 |
-
output_steps = {}
|
58 |
-
for step in self.steps:
|
59 |
-
output_steps[step["step_name"]] = step["duration"]
|
60 |
-
|
61 |
-
highlights = {"total_time": round(self.total_time, 4)}
|
62 |
-
|
63 |
-
if self.name:
|
64 |
-
highlights = {"name": self.name, **highlights}
|
65 |
-
|
66 |
-
output = {
|
67 |
-
**highlights,
|
68 |
-
**output_steps
|
69 |
-
}
|
70 |
-
return output
|
71 |
-
|
72 |
-
def to_json_str(self):
|
73 |
-
"""Return a human-readable JSON of the timing steps."""
|
74 |
-
return json.dumps(self.to_json(), indent=4)
|
75 |
-
|
76 |
-
def formatted_result(self):
|
77 |
-
"""Return a list of the steps, their duration, and total duration."""
|
78 |
-
if self.total_time is None:
|
79 |
-
raise RuntimeError("Timer has not been ended.")
|
80 |
-
line_buffer = []
|
81 |
-
if self.name:
|
82 |
-
line_buffer.append(f"Timer: {self.name}")
|
83 |
-
for step in self.steps:
|
84 |
-
line_buffer.append(f"[{step['duration']:05.2f}s, {step['total_duration']:05.2f}s] {step['step_name']}")
|
85 |
-
# for step in self.steps:
|
86 |
-
# line_buffer.append(f"{step['step_name']}: {step['duration']:.2f}s ({step['total_duration']:.2f}s)")
|
87 |
-
line_buffer.append(f"Total time: {self.total_time:.2f}s")
|
88 |
-
return "\n".join(line_buffer)
|
89 |
-
|
90 |
-
def log_formatted_result(self):
|
91 |
-
print(self.formatted_result())
|
92 |
-
|
93 |
-
|
94 |
-
def example():
|
95 |
-
# Example usage
|
96 |
-
timer = Timer()
|
97 |
-
timer.start()
|
98 |
-
|
99 |
-
# Simulating some steps
|
100 |
-
time.sleep(1) # Simulate work for step 1
|
101 |
-
timer.add_step("Step 1")
|
102 |
-
|
103 |
-
time.sleep(2) # Simulate work for step 2
|
104 |
-
timer.add_step("Step 2")
|
105 |
-
|
106 |
-
timer.end()
|
107 |
-
|
108 |
-
# Print the timer output
|
109 |
-
print(timer.formatted_result())
|
110 |
-
print(timer.to_json_str())
|
111 |
-
|
112 |
-
|
113 |
-
if __name__ == "__main__":
|
114 |
-
example()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
utils.py
DELETED
@@ -1,127 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import sys
|
3 |
-
import time
|
4 |
-
from functools import wraps
|
5 |
-
from typing import Any, Literal
|
6 |
-
|
7 |
-
from gradio import ChatMessage
|
8 |
-
from gradio.components.chatbot import Message
|
9 |
-
|
10 |
-
COMMUNITY_POSTFIX_URL = "/discussions"
|
11 |
-
DEBUG_MODE = False or os.environ.get("DEBUG_MODE") == "True"
|
12 |
-
DEBUG_MODEL = False or os.environ.get("DEBUG_MODEL") == "True"
|
13 |
-
|
14 |
-
models_config = {
|
15 |
-
"Apriel-Nemotron-15b-Thinker": {
|
16 |
-
"MODEL_DISPLAY_NAME": "Apriel-Nemotron-15b-Thinker",
|
17 |
-
"MODEL_HF_URL": "https://huggingface.co/ServiceNow-AI/Apriel-Nemotron-15b-Thinker",
|
18 |
-
"MODEL_NAME": os.environ.get("MODEL_NAME_NEMO_15B"),
|
19 |
-
"VLLM_API_URL": os.environ.get("VLLM_API_URL_NEMO_15B"),
|
20 |
-
"AUTH_TOKEN": os.environ.get("AUTH_TOKEN"),
|
21 |
-
"REASONING": True
|
22 |
-
},
|
23 |
-
"Apriel-5b": {
|
24 |
-
"MODEL_DISPLAY_NAME": "Apriel-5b",
|
25 |
-
"MODEL_HF_URL": "https://huggingface.co/ServiceNow-AI/Apriel-5B-Instruct",
|
26 |
-
"MODEL_NAME": os.environ.get("MODEL_NAME_5B"),
|
27 |
-
"VLLM_API_URL": os.environ.get("VLLM_API_URL_5B"),
|
28 |
-
"AUTH_TOKEN": os.environ.get("AUTH_TOKEN"),
|
29 |
-
"REASONING": False
|
30 |
-
}
|
31 |
-
}
|
32 |
-
|
33 |
-
|
34 |
-
def get_model_config(model_name: str) -> dict:
|
35 |
-
config = models_config.get(model_name)
|
36 |
-
if not config:
|
37 |
-
raise ValueError(f"Model {model_name} not found in models_config")
|
38 |
-
if not config.get("MODEL_NAME"):
|
39 |
-
raise ValueError(f"Model name not found in config for {model_name}")
|
40 |
-
if not config.get("VLLM_API_URL"):
|
41 |
-
raise ValueError(f"VLLM API URL not found in config for {model_name}")
|
42 |
-
|
43 |
-
return config
|
44 |
-
|
45 |
-
|
46 |
-
def _log_message(prefix, message, icon=""):
|
47 |
-
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
48 |
-
if len(icon) > 0:
|
49 |
-
icon = f"{icon} "
|
50 |
-
print(f"{timestamp}: {prefix} {icon}{message}")
|
51 |
-
|
52 |
-
|
53 |
-
def log_debug(message):
|
54 |
-
if DEBUG_MODE is True:
|
55 |
-
_log_message("DEBUG", message)
|
56 |
-
|
57 |
-
|
58 |
-
def log_info(message):
|
59 |
-
_log_message("INFO ", message)
|
60 |
-
|
61 |
-
|
62 |
-
def log_warning(message):
|
63 |
-
_log_message("WARN ", message, "⚠️")
|
64 |
-
|
65 |
-
|
66 |
-
def log_error(message):
|
67 |
-
_log_message("ERROR", message, "‼️")
|
68 |
-
|
69 |
-
|
70 |
-
# Gradio 5.0.1 had issues with checking the message formats. 5.29.0 does not!
|
71 |
-
def check_format(messages: Any, type: Literal["messages", "tuples"] = "messages") -> None:
|
72 |
-
if not DEBUG_MODE:
|
73 |
-
return
|
74 |
-
|
75 |
-
if type == "messages":
|
76 |
-
all_valid = all(
|
77 |
-
isinstance(message, dict)
|
78 |
-
and "role" in message
|
79 |
-
and "content" in message
|
80 |
-
or isinstance(message, ChatMessage | Message)
|
81 |
-
for message in messages
|
82 |
-
)
|
83 |
-
if not all_valid:
|
84 |
-
# Display which message is not valid
|
85 |
-
for i, message in enumerate(messages):
|
86 |
-
if not (isinstance(message, dict) and
|
87 |
-
"role" in message and
|
88 |
-
"content" in message) and not isinstance(message, ChatMessage | Message):
|
89 |
-
print(f"_check_format() --> Invalid message at index {i}: {message}\n", file=sys.stderr)
|
90 |
-
break
|
91 |
-
|
92 |
-
raise Exception(
|
93 |
-
"Data incompatible with messages format. Each message should be a dictionary with 'role' and 'content' keys or a ChatMessage object."
|
94 |
-
)
|
95 |
-
# else:
|
96 |
-
# print("_check_format() --> All messages are valid.")
|
97 |
-
elif not all(
|
98 |
-
isinstance(message, (tuple, list)) and len(message) == 2
|
99 |
-
for message in messages
|
100 |
-
):
|
101 |
-
raise Exception(
|
102 |
-
"Data incompatible with tuples format. Each message should be a list of length 2."
|
103 |
-
)
|
104 |
-
|
105 |
-
|
106 |
-
# Adds timing info for a gradio event handler (non-generator functions)
|
107 |
-
def logged_event_handler(log_msg='', event_handler=None, log_timer=None, clear_timer=False):
|
108 |
-
@wraps(event_handler)
|
109 |
-
def wrapped_event_handler(*args, **kwargs):
|
110 |
-
# Log before
|
111 |
-
if log_timer:
|
112 |
-
if clear_timer:
|
113 |
-
log_timer.clear()
|
114 |
-
log_timer.add_step(f"Start: {log_debug}")
|
115 |
-
log_debug(f"::: Before event: {log_msg}")
|
116 |
-
|
117 |
-
# Call the original event handler
|
118 |
-
result = event_handler(*args, **kwargs)
|
119 |
-
|
120 |
-
# Log after
|
121 |
-
if log_timer:
|
122 |
-
log_timer.add_step(f"Completed: {log_msg}")
|
123 |
-
log_debug(f"::: After event: {log_msg}")
|
124 |
-
|
125 |
-
return result
|
126 |
-
|
127 |
-
return wrapped_event_handler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|