Update app.py
Browse files
app.py
CHANGED
|
@@ -264,12 +264,11 @@ def chatbot_respond(
|
|
| 264 |
uploaded_files,
|
| 265 |
) -> Generator[List, None, None]:
|
| 266 |
"""
|
| 267 |
-
Called on every user message. history is
|
| 268 |
-
|
| 269 |
-
file list from gr.File.
|
| 270 |
|
| 271 |
-
Yields the updated history list after each token chunk
|
| 272 |
-
streaming effect in the Gradio Chatbot.
|
| 273 |
"""
|
| 274 |
# ---- 1. Parse any newly uploaded files ----
|
| 275 |
file_context = parse_all_files(uploaded_files)
|
|
@@ -277,7 +276,7 @@ def chatbot_respond(
|
|
| 277 |
# ---- 2. Build the message list for llama-cpp-python (ChatML format) ----
|
| 278 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 279 |
|
| 280 |
-
# If files were uploaded, inject their content
|
| 281 |
if file_context:
|
| 282 |
messages.append({
|
| 283 |
"role": "user",
|
|
@@ -295,21 +294,21 @@ def chatbot_respond(
|
|
| 295 |
),
|
| 296 |
})
|
| 297 |
|
| 298 |
-
# Append the real conversation history
|
| 299 |
-
for
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
if
|
| 303 |
-
messages.append({"role":
|
| 304 |
|
| 305 |
-
# Append the current message
|
| 306 |
messages.append({"role": "user", "content": message})
|
| 307 |
|
| 308 |
# ---- 3. Stream the completion ----
|
| 309 |
llm = _get_llm()
|
| 310 |
stream = llm.create_chat_completion(
|
| 311 |
messages=messages,
|
| 312 |
-
temperature=0.6,
|
| 313 |
top_p=0.8,
|
| 314 |
top_k=20,
|
| 315 |
max_tokens=4096,
|
|
@@ -325,11 +324,13 @@ def chatbot_respond(
|
|
| 325 |
content = delta.get("content", "")
|
| 326 |
if content:
|
| 327 |
partial += content
|
| 328 |
-
# YIELD
|
| 329 |
-
yield history + [
|
|
|
|
| 330 |
|
| 331 |
-
# Final yield
|
| 332 |
-
yield history + [
|
|
|
|
| 333 |
|
| 334 |
|
| 335 |
# ===========================================================================
|
|
@@ -337,13 +338,7 @@ def chatbot_respond(
|
|
| 337 |
# ===========================================================================
|
| 338 |
|
| 339 |
def create_demo() -> gr.Blocks:
|
| 340 |
-
css = """
|
| 341 |
-
.file-upload-col { background: var(--background-fill-secondary); border-radius: 12px; padding: 16px; }
|
| 342 |
-
footer { display: none !important; }
|
| 343 |
-
"""
|
| 344 |
with gr.Blocks(
|
| 345 |
-
css=css,
|
| 346 |
-
theme=gr.themes.Soft(primary_hue="violet", secondary_hue="slate"),
|
| 347 |
title="Qwen 3.6 27B CoderBot",
|
| 348 |
) as demo:
|
| 349 |
|
|
@@ -361,7 +356,7 @@ def create_demo() -> gr.Blocks:
|
|
| 361 |
chatbot = gr.Chatbot(
|
| 362 |
label="Chat",
|
| 363 |
height=580,
|
| 364 |
-
|
| 365 |
avatar_images=(
|
| 366 |
None,
|
| 367 |
"https://huggingface.co/front/assets/huggingface_logo-noborder.svg",
|
|
@@ -441,8 +436,8 @@ def create_demo() -> gr.Blocks:
|
|
| 441 |
outputs=[chatbot],
|
| 442 |
).then(lambda: "", None, [msg])
|
| 443 |
|
| 444 |
-
# Clear chat
|
| 445 |
-
clear_btn.click(lambda:
|
| 446 |
|
| 447 |
# File upload feedback
|
| 448 |
files.change(update_file_info, files, [uploaded_info, file_content_state])
|
|
@@ -457,4 +452,9 @@ def create_demo() -> gr.Blocks:
|
|
| 457 |
if __name__ == "__main__":
|
| 458 |
demo = create_demo()
|
| 459 |
demo.queue(default_concurrency_limit=1, max_size=4)
|
| 460 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
uploaded_files,
|
| 265 |
) -> Generator[List, None, None]:
|
| 266 |
"""
|
| 267 |
+
Called on every user message. history is in Gradio 6 "messages" format:
|
| 268 |
+
list of {"role": "user"|"assistant", "content": "..."} dicts.
|
| 269 |
+
uploaded_files is the current file list from gr.File.
|
| 270 |
|
| 271 |
+
Yields the updated history list after each token chunk for streaming.
|
|
|
|
| 272 |
"""
|
| 273 |
# ---- 1. Parse any newly uploaded files ----
|
| 274 |
file_context = parse_all_files(uploaded_files)
|
|
|
|
| 276 |
# ---- 2. Build the message list for llama-cpp-python (ChatML format) ----
|
| 277 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 278 |
|
| 279 |
+
# If files were uploaded, inject their content
|
| 280 |
if file_context:
|
| 281 |
messages.append({
|
| 282 |
"role": "user",
|
|
|
|
| 294 |
),
|
| 295 |
})
|
| 296 |
|
| 297 |
+
# Append the real conversation history (Gradio 6 "messages" format)
|
| 298 |
+
for entry in history:
|
| 299 |
+
role = entry.get("role", "user")
|
| 300 |
+
content = entry.get("content", "")
|
| 301 |
+
if content:
|
| 302 |
+
messages.append({"role": role, "content": content})
|
| 303 |
|
| 304 |
+
# Append the current user message
|
| 305 |
messages.append({"role": "user", "content": message})
|
| 306 |
|
| 307 |
# ---- 3. Stream the completion ----
|
| 308 |
llm = _get_llm()
|
| 309 |
stream = llm.create_chat_completion(
|
| 310 |
messages=messages,
|
| 311 |
+
temperature=0.6,
|
| 312 |
top_p=0.8,
|
| 313 |
top_k=20,
|
| 314 |
max_tokens=4096,
|
|
|
|
| 324 |
content = delta.get("content", "")
|
| 325 |
if content:
|
| 326 |
partial += content
|
| 327 |
+
# YIELD in Gradio 6 "messages" format
|
| 328 |
+
yield history + [{"role": "user", "content": message},
|
| 329 |
+
{"role": "assistant", "content": partial}]
|
| 330 |
|
| 331 |
+
# Final yield
|
| 332 |
+
yield history + [{"role": "user", "content": message},
|
| 333 |
+
{"role": "assistant", "content": partial}]
|
| 334 |
|
| 335 |
|
| 336 |
# ===========================================================================
|
|
|
|
| 338 |
# ===========================================================================
|
| 339 |
|
| 340 |
def create_demo() -> gr.Blocks:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
with gr.Blocks(
|
|
|
|
|
|
|
| 342 |
title="Qwen 3.6 27B CoderBot",
|
| 343 |
) as demo:
|
| 344 |
|
|
|
|
| 356 |
chatbot = gr.Chatbot(
|
| 357 |
label="Chat",
|
| 358 |
height=580,
|
| 359 |
+
type="messages", # Gradio 6 uses "messages" or "tuples"
|
| 360 |
avatar_images=(
|
| 361 |
None,
|
| 362 |
"https://huggingface.co/front/assets/huggingface_logo-noborder.svg",
|
|
|
|
| 436 |
outputs=[chatbot],
|
| 437 |
).then(lambda: "", None, [msg])
|
| 438 |
|
| 439 |
+
# Clear chat — return empty list (Gradio 6 "messages" format)
|
| 440 |
+
clear_btn.click(lambda: [], None, chatbot, queue=False)
|
| 441 |
|
| 442 |
# File upload feedback
|
| 443 |
files.change(update_file_info, files, [uploaded_info, file_content_state])
|
|
|
|
| 452 |
if __name__ == "__main__":
|
| 453 |
demo = create_demo()
|
| 454 |
demo.queue(default_concurrency_limit=1, max_size=4)
|
| 455 |
+
demo.launch(
|
| 456 |
+
css="""
|
| 457 |
+
.file-upload-col { background: var(--background-fill-secondary); border-radius: 12px; padding: 16px; }
|
| 458 |
+
footer { display: none !important; }
|
| 459 |
+
""",
|
| 460 |
+
)
|