Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -96,10 +96,18 @@ def generate_response(message, history, max_tokens, temperature, top_p):
|
|
| 96 |
|
| 97 |
# Prepare conversation history
|
| 98 |
messages = []
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# Add current message
|
| 105 |
messages.append({"role": "user", "content": message})
|
|
@@ -179,12 +187,15 @@ def chat_interface(message, history, max_tokens, temperature, top_p):
|
|
| 179 |
if not message.strip():
|
| 180 |
return history, ""
|
| 181 |
|
| 182 |
-
# Add user message to history
|
| 183 |
-
history.append(
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
# Generate response with streaming
|
| 186 |
-
for partial_response in generate_response(message, history[:-
|
| 187 |
-
history[-1][
|
| 188 |
yield history, ""
|
| 189 |
|
| 190 |
return history, ""
|
|
@@ -492,7 +503,7 @@ with gr.Blocks(
|
|
| 492 |
"""
|
| 493 |
) as demo:
|
| 494 |
# Header Section
|
| 495 |
-
with gr.Row(
|
| 496 |
with gr.Column():
|
| 497 |
gr.HTML("""
|
| 498 |
<div style="text-align: center; padding: 20px 0;">
|
|
@@ -541,7 +552,7 @@ with gr.Blocks(
|
|
| 541 |
""")
|
| 542 |
|
| 543 |
# Main Chat Interface
|
| 544 |
-
with gr.Row(
|
| 545 |
with gr.Column(scale=4):
|
| 546 |
# Status indicator
|
| 547 |
gr.HTML("""
|
|
@@ -559,12 +570,11 @@ with gr.Blocks(
|
|
| 559 |
chatbot = gr.Chatbot(
|
| 560 |
[],
|
| 561 |
elem_id="chatbot",
|
| 562 |
-
|
| 563 |
-
bubble_full_width=False,
|
| 564 |
height=650,
|
| 565 |
show_copy_button=True,
|
| 566 |
show_share_button=True,
|
| 567 |
-
avatar_images=("👤", "
|
| 568 |
render_markdown=True,
|
| 569 |
sanitize_html=False, # Allow HTML for thinking and ser blocks
|
| 570 |
latex_delimiters=[
|
|
@@ -576,7 +586,7 @@ with gr.Blocks(
|
|
| 576 |
)
|
| 577 |
|
| 578 |
# Enhanced input section
|
| 579 |
-
with gr.Row(
|
| 580 |
with gr.Column(scale=1):
|
| 581 |
msg = gr.Textbox(
|
| 582 |
container=False,
|
|
@@ -584,25 +594,22 @@ with gr.Blocks(
|
|
| 584 |
label="",
|
| 585 |
autofocus=True,
|
| 586 |
lines=1,
|
| 587 |
-
max_lines=4
|
| 588 |
-
elem_classes="message-input"
|
| 589 |
)
|
| 590 |
with gr.Column(scale=0, min_width=120):
|
| 591 |
with gr.Row():
|
| 592 |
send_btn = gr.Button(
|
| 593 |
"🚀 Send",
|
| 594 |
variant="primary",
|
| 595 |
-
size="lg"
|
| 596 |
-
elem_classes="send-button"
|
| 597 |
)
|
| 598 |
clear_btn = gr.Button(
|
| 599 |
"🗑️",
|
| 600 |
variant="secondary",
|
| 601 |
-
size="lg"
|
| 602 |
-
elem_classes="clear-button"
|
| 603 |
)
|
| 604 |
|
| 605 |
-
with gr.Column(scale=1, min_width=320
|
| 606 |
# Parameter header
|
| 607 |
gr.HTML("""
|
| 608 |
<div style="text-align: center; margin-bottom: 20px;">
|
|
@@ -618,8 +625,7 @@ with gr.Blocks(
|
|
| 618 |
value=2048,
|
| 619 |
step=50,
|
| 620 |
label="🎯 Max Tokens",
|
| 621 |
-
info="Maximum number of tokens to generate"
|
| 622 |
-
elem_classes="parameter-slider"
|
| 623 |
)
|
| 624 |
|
| 625 |
temperature = gr.Slider(
|
|
@@ -628,8 +634,7 @@ with gr.Blocks(
|
|
| 628 |
value=0.7,
|
| 629 |
step=0.1,
|
| 630 |
label="🌡️ Temperature",
|
| 631 |
-
info="Higher = more creative, Lower = more focused"
|
| 632 |
-
elem_classes="parameter-slider"
|
| 633 |
)
|
| 634 |
|
| 635 |
top_p = gr.Slider(
|
|
@@ -638,8 +643,7 @@ with gr.Blocks(
|
|
| 638 |
value=0.9,
|
| 639 |
step=0.05,
|
| 640 |
label="🎲 Top-p",
|
| 641 |
-
info="Nucleus sampling threshold"
|
| 642 |
-
elem_classes="parameter-slider"
|
| 643 |
)
|
| 644 |
|
| 645 |
# Action buttons
|
|
@@ -647,8 +651,7 @@ with gr.Blocks(
|
|
| 647 |
stop_btn = gr.Button(
|
| 648 |
"⏹️ Stop",
|
| 649 |
variant="stop",
|
| 650 |
-
scale=1
|
| 651 |
-
elem_classes="action-button"
|
| 652 |
)
|
| 653 |
|
| 654 |
# Model information card
|
|
@@ -679,7 +682,7 @@ with gr.Blocks(
|
|
| 679 |
""")
|
| 680 |
|
| 681 |
# Enhanced Examples Section
|
| 682 |
-
with gr.Row(
|
| 683 |
with gr.Column():
|
| 684 |
gr.HTML("""
|
| 685 |
<div style="text-align: center; margin: 24px 0 16px 0;">
|
|
@@ -704,8 +707,7 @@ with gr.Blocks(
|
|
| 704 |
["🔬 How does CRISPR gene editing work in simple terms?"]
|
| 705 |
],
|
| 706 |
inputs=msg,
|
| 707 |
-
examples_per_page=6
|
| 708 |
-
elem_classes="example-grid"
|
| 709 |
)
|
| 710 |
|
| 711 |
# Event handlers
|
|
@@ -738,7 +740,7 @@ with gr.Blocks(
|
|
| 738 |
)
|
| 739 |
|
| 740 |
# Enhanced Footer
|
| 741 |
-
with gr.Row(
|
| 742 |
with gr.Column():
|
| 743 |
gr.HTML("""
|
| 744 |
<div style="text-align: center; padding: 20px 0;">
|
|
|
|
| 96 |
|
| 97 |
# Prepare conversation history
|
| 98 |
messages = []
|
| 99 |
+
|
| 100 |
+
# Handle both old tuple format and new message format
|
| 101 |
+
for item in history:
|
| 102 |
+
if isinstance(item, dict):
|
| 103 |
+
# New message format
|
| 104 |
+
messages.append(item)
|
| 105 |
+
elif isinstance(item, (list, tuple)) and len(item) == 2:
|
| 106 |
+
# Old tuple format
|
| 107 |
+
user_msg, assistant_msg = item
|
| 108 |
+
messages.append({"role": "user", "content": user_msg})
|
| 109 |
+
if assistant_msg:
|
| 110 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 111 |
|
| 112 |
# Add current message
|
| 113 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 187 |
if not message.strip():
|
| 188 |
return history, ""
|
| 189 |
|
| 190 |
+
# Add user message to history in the new message format
|
| 191 |
+
history.append({"role": "user", "content": message})
|
| 192 |
+
|
| 193 |
+
# Add placeholder for assistant response
|
| 194 |
+
history.append({"role": "assistant", "content": ""})
|
| 195 |
|
| 196 |
# Generate response with streaming
|
| 197 |
+
for partial_response in generate_response(message, history[:-2], max_tokens, temperature, top_p):
|
| 198 |
+
history[-1]["content"] = partial_response
|
| 199 |
yield history, ""
|
| 200 |
|
| 201 |
return history, ""
|
|
|
|
| 503 |
"""
|
| 504 |
) as demo:
|
| 505 |
# Header Section
|
| 506 |
+
with gr.Row():
|
| 507 |
with gr.Column():
|
| 508 |
gr.HTML("""
|
| 509 |
<div style="text-align: center; padding: 20px 0;">
|
|
|
|
| 552 |
""")
|
| 553 |
|
| 554 |
# Main Chat Interface
|
| 555 |
+
with gr.Row():
|
| 556 |
with gr.Column(scale=4):
|
| 557 |
# Status indicator
|
| 558 |
gr.HTML("""
|
|
|
|
| 570 |
chatbot = gr.Chatbot(
|
| 571 |
[],
|
| 572 |
elem_id="chatbot",
|
| 573 |
+
type='messages',
|
|
|
|
| 574 |
height=650,
|
| 575 |
show_copy_button=True,
|
| 576 |
show_share_button=True,
|
| 577 |
+
avatar_images=("👤", "🤖"),
|
| 578 |
render_markdown=True,
|
| 579 |
sanitize_html=False, # Allow HTML for thinking and ser blocks
|
| 580 |
latex_delimiters=[
|
|
|
|
| 586 |
)
|
| 587 |
|
| 588 |
# Enhanced input section
|
| 589 |
+
with gr.Row():
|
| 590 |
with gr.Column(scale=1):
|
| 591 |
msg = gr.Textbox(
|
| 592 |
container=False,
|
|
|
|
| 594 |
label="",
|
| 595 |
autofocus=True,
|
| 596 |
lines=1,
|
| 597 |
+
max_lines=4
|
|
|
|
| 598 |
)
|
| 599 |
with gr.Column(scale=0, min_width=120):
|
| 600 |
with gr.Row():
|
| 601 |
send_btn = gr.Button(
|
| 602 |
"🚀 Send",
|
| 603 |
variant="primary",
|
| 604 |
+
size="lg"
|
|
|
|
| 605 |
)
|
| 606 |
clear_btn = gr.Button(
|
| 607 |
"🗑️",
|
| 608 |
variant="secondary",
|
| 609 |
+
size="lg"
|
|
|
|
| 610 |
)
|
| 611 |
|
| 612 |
+
with gr.Column(scale=1, min_width=320):
|
| 613 |
# Parameter header
|
| 614 |
gr.HTML("""
|
| 615 |
<div style="text-align: center; margin-bottom: 20px;">
|
|
|
|
| 625 |
value=2048,
|
| 626 |
step=50,
|
| 627 |
label="🎯 Max Tokens",
|
| 628 |
+
info="Maximum number of tokens to generate"
|
|
|
|
| 629 |
)
|
| 630 |
|
| 631 |
temperature = gr.Slider(
|
|
|
|
| 634 |
value=0.7,
|
| 635 |
step=0.1,
|
| 636 |
label="🌡️ Temperature",
|
| 637 |
+
info="Higher = more creative, Lower = more focused"
|
|
|
|
| 638 |
)
|
| 639 |
|
| 640 |
top_p = gr.Slider(
|
|
|
|
| 643 |
value=0.9,
|
| 644 |
step=0.05,
|
| 645 |
label="🎲 Top-p",
|
| 646 |
+
info="Nucleus sampling threshold"
|
|
|
|
| 647 |
)
|
| 648 |
|
| 649 |
# Action buttons
|
|
|
|
| 651 |
stop_btn = gr.Button(
|
| 652 |
"⏹️ Stop",
|
| 653 |
variant="stop",
|
| 654 |
+
scale=1
|
|
|
|
| 655 |
)
|
| 656 |
|
| 657 |
# Model information card
|
|
|
|
| 682 |
""")
|
| 683 |
|
| 684 |
# Enhanced Examples Section
|
| 685 |
+
with gr.Row():
|
| 686 |
with gr.Column():
|
| 687 |
gr.HTML("""
|
| 688 |
<div style="text-align: center; margin: 24px 0 16px 0;">
|
|
|
|
| 707 |
["🔬 How does CRISPR gene editing work in simple terms?"]
|
| 708 |
],
|
| 709 |
inputs=msg,
|
| 710 |
+
examples_per_page=6
|
|
|
|
| 711 |
)
|
| 712 |
|
| 713 |
# Event handlers
|
|
|
|
| 740 |
)
|
| 741 |
|
| 742 |
# Enhanced Footer
|
| 743 |
+
with gr.Row():
|
| 744 |
with gr.Column():
|
| 745 |
gr.HTML("""
|
| 746 |
<div style="text-align: center; padding: 20px 0;">
|