Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,17 +36,25 @@ custom_css = """
|
|
| 36 |
.main-container {
|
| 37 |
background: rgba(255, 255, 255, 0.98);
|
| 38 |
border-radius: 16px;
|
| 39 |
-
padding:
|
| 40 |
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
| 41 |
border: 1px solid rgba(0, 0, 0, 0.05);
|
| 42 |
-
margin: 12px;
|
| 43 |
}
|
| 44 |
|
| 45 |
-
.
|
| 46 |
-
|
| 47 |
border-radius: 12px;
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
font-weight: 500;
|
| 51 |
}
|
| 52 |
|
|
@@ -71,17 +79,39 @@ custom_css = """
|
|
| 71 |
.rag-context {
|
| 72 |
background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
|
| 73 |
border-left: 4px solid #f59e0b;
|
| 74 |
-
padding:
|
| 75 |
-
margin:
|
| 76 |
-
border-radius:
|
| 77 |
-
font-size: 0.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
|
| 80 |
-
.
|
| 81 |
-
background:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
margin: 8px 0;
|
| 86 |
}
|
| 87 |
"""
|
|
@@ -519,88 +549,149 @@ def update_rag_settings(enable, docs, k):
|
|
| 519 |
|
| 520 |
# Build the interface
|
| 521 |
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, fill_height=True) as demo:
|
| 522 |
-
gr.Markdown("# ๐ GPT-OSS-20B with PDF RAG System")
|
| 523 |
-
gr.Markdown("Enhanced AI assistant with document-based context understanding")
|
| 524 |
-
|
| 525 |
with gr.Row():
|
| 526 |
-
#
|
| 527 |
-
with gr.Column(scale=1):
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
pdf_upload = gr.File(
|
| 532 |
label="Upload PDF",
|
| 533 |
file_types=[".pdf"],
|
| 534 |
-
type="filepath"
|
|
|
|
| 535 |
)
|
| 536 |
|
| 537 |
upload_status = gr.HTML(
|
| 538 |
-
value="<div
|
| 539 |
)
|
| 540 |
|
| 541 |
document_list = gr.CheckboxGroup(
|
| 542 |
choices=[],
|
| 543 |
-
label="
|
| 544 |
-
|
| 545 |
-
)
|
| 546 |
-
|
| 547 |
-
clear_btn = gr.Button("๐๏ธ Clear All Documents", size="sm", variant="secondary")
|
| 548 |
-
|
| 549 |
-
enable_rag = gr.Checkbox(
|
| 550 |
-
label="โจ Enable RAG",
|
| 551 |
-
value=False,
|
| 552 |
-
info="Use documents for context-aware responses"
|
| 553 |
)
|
| 554 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
top_k_slider = gr.Slider(
|
| 556 |
minimum=1,
|
| 557 |
maximum=5,
|
| 558 |
value=3,
|
| 559 |
step=1,
|
| 560 |
label="Context Chunks",
|
| 561 |
-
info="Number of
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
)
|
| 563 |
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
|
|
|
|
|
|
|
|
|
| 567 |
)
|
| 568 |
|
| 569 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
|
| 571 |
-
#
|
| 572 |
-
with gr.Column(scale=
|
| 573 |
-
with gr.Group(elem_classes="main-container"):
|
| 574 |
# Create ChatInterface with custom function
|
| 575 |
chat_interface = gr.ChatInterface(
|
| 576 |
fn=generate_response,
|
| 577 |
additional_inputs=[
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
),
|
| 585 |
-
gr.Slider(label="Temperature", minimum=0.1, maximum=2.0, step=0.1, value=0.7),
|
| 586 |
-
gr.Slider(label="Top-p", minimum=0.05, maximum=1.0, step=0.05, value=0.9),
|
| 587 |
-
gr.Slider(label="Top-k", minimum=1, maximum=100, step=1, value=50),
|
| 588 |
-
gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.0)
|
| 589 |
],
|
| 590 |
examples=[
|
| 591 |
-
[{"text": "
|
| 592 |
-
[{"text": "
|
| 593 |
-
[{"text": "
|
| 594 |
],
|
| 595 |
cache_examples=False,
|
| 596 |
type="messages",
|
| 597 |
-
|
| 598 |
-
|
| 599 |
textbox=gr.Textbox(
|
| 600 |
-
label="
|
| 601 |
-
placeholder="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 602 |
),
|
| 603 |
-
stop_btn="Stop
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
multimodal=False
|
| 605 |
)
|
| 606 |
|
|
@@ -616,21 +707,37 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, fill_height=True) as demo
|
|
| 616 |
outputs=[upload_status, document_list]
|
| 617 |
)
|
| 618 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 619 |
# Update RAG settings when changed
|
| 620 |
enable_rag.change(
|
| 621 |
-
fn=
|
| 622 |
inputs=[enable_rag, document_list, top_k_slider],
|
| 623 |
outputs=[rag_status, context_preview]
|
| 624 |
)
|
| 625 |
|
| 626 |
document_list.change(
|
| 627 |
-
fn=
|
| 628 |
inputs=[enable_rag, document_list, top_k_slider],
|
| 629 |
outputs=[rag_status, context_preview]
|
| 630 |
)
|
| 631 |
|
| 632 |
top_k_slider.change(
|
| 633 |
-
fn=
|
| 634 |
inputs=[enable_rag, document_list, top_k_slider],
|
| 635 |
outputs=[rag_status, context_preview]
|
| 636 |
)
|
|
|
|
| 36 |
.main-container {
|
| 37 |
background: rgba(255, 255, 255, 0.98);
|
| 38 |
border-radius: 16px;
|
| 39 |
+
padding: 20px;
|
| 40 |
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
| 41 |
border: 1px solid rgba(0, 0, 0, 0.05);
|
|
|
|
| 42 |
}
|
| 43 |
|
| 44 |
+
.sidebar-container {
|
| 45 |
+
background: rgba(255, 255, 255, 0.98);
|
| 46 |
border-radius: 12px;
|
| 47 |
+
padding: 16px;
|
| 48 |
+
box-shadow: 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
| 49 |
+
border: 1px solid rgba(0, 0, 0, 0.05);
|
| 50 |
+
height: fit-content;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
.pdf-status {
|
| 54 |
+
padding: 10px 14px;
|
| 55 |
+
border-radius: 10px;
|
| 56 |
+
margin: 8px 0;
|
| 57 |
+
font-size: 0.9rem;
|
| 58 |
font-weight: 500;
|
| 59 |
}
|
| 60 |
|
|
|
|
| 79 |
.rag-context {
|
| 80 |
background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
|
| 81 |
border-left: 4px solid #f59e0b;
|
| 82 |
+
padding: 10px;
|
| 83 |
+
margin: 8px 0;
|
| 84 |
+
border-radius: 6px;
|
| 85 |
+
font-size: 0.85rem;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
.status-badge {
|
| 89 |
+
display: inline-block;
|
| 90 |
+
padding: 4px 12px;
|
| 91 |
+
border-radius: 20px;
|
| 92 |
+
font-size: 0.85rem;
|
| 93 |
+
font-weight: 600;
|
| 94 |
+
margin: 4px 0;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
.status-enabled {
|
| 98 |
+
background: #10b981;
|
| 99 |
+
color: white;
|
| 100 |
}
|
| 101 |
|
| 102 |
+
.status-disabled {
|
| 103 |
+
background: #6b7280;
|
| 104 |
+
color: white;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
/* Chat interface maximization */
|
| 108 |
+
.chat-container {
|
| 109 |
+
height: calc(100vh - 200px) !important;
|
| 110 |
+
min-height: 600px;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
/* Accordion styling */
|
| 114 |
+
.accordion {
|
| 115 |
margin: 8px 0;
|
| 116 |
}
|
| 117 |
"""
|
|
|
|
| 549 |
|
| 550 |
# Build the interface
|
| 551 |
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, fill_height=True) as demo:
|
|
|
|
|
|
|
|
|
|
| 552 |
with gr.Row():
|
| 553 |
+
# Compact sidebar
|
| 554 |
+
with gr.Column(scale=1, min_width=300):
|
| 555 |
+
gr.Markdown("## ๐ GPT-OSS-20B + RAG")
|
| 556 |
+
|
| 557 |
+
# RAG Status Badge
|
| 558 |
+
with gr.Group(elem_classes="sidebar-container"):
|
| 559 |
+
rag_status = gr.HTML(
|
| 560 |
+
value="<div class='status-badge status-disabled'>RAG: Disabled</div>"
|
| 561 |
+
)
|
| 562 |
+
context_preview = gr.HTML(value="", visible=False)
|
| 563 |
+
|
| 564 |
+
# PDF Upload Section
|
| 565 |
+
with gr.Accordion("๐ PDF Documents", open=True, elem_classes="accordion"):
|
| 566 |
pdf_upload = gr.File(
|
| 567 |
label="Upload PDF",
|
| 568 |
file_types=[".pdf"],
|
| 569 |
+
type="filepath",
|
| 570 |
+
elem_classes="compact-upload"
|
| 571 |
)
|
| 572 |
|
| 573 |
upload_status = gr.HTML(
|
| 574 |
+
value="<div style='font-size: 0.85rem; color: #6b7280;'>No documents uploaded</div>"
|
| 575 |
)
|
| 576 |
|
| 577 |
document_list = gr.CheckboxGroup(
|
| 578 |
choices=[],
|
| 579 |
+
label="Select Documents",
|
| 580 |
+
elem_classes="compact-checkbox"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
)
|
| 582 |
|
| 583 |
+
with gr.Row():
|
| 584 |
+
enable_rag = gr.Checkbox(
|
| 585 |
+
label="Enable RAG",
|
| 586 |
+
value=False,
|
| 587 |
+
scale=2
|
| 588 |
+
)
|
| 589 |
+
clear_btn = gr.Button("Clear", size="sm", variant="secondary", scale=1)
|
| 590 |
+
|
| 591 |
+
# RAG Settings
|
| 592 |
+
with gr.Accordion("โ๏ธ RAG Settings", open=False, elem_classes="accordion"):
|
| 593 |
top_k_slider = gr.Slider(
|
| 594 |
minimum=1,
|
| 595 |
maximum=5,
|
| 596 |
value=3,
|
| 597 |
step=1,
|
| 598 |
label="Context Chunks",
|
| 599 |
+
info="Number of chunks to use"
|
| 600 |
+
)
|
| 601 |
+
|
| 602 |
+
# Model Settings
|
| 603 |
+
with gr.Accordion("๐ง Model Settings", open=False, elem_classes="accordion"):
|
| 604 |
+
max_tokens = gr.Slider(
|
| 605 |
+
label="Max tokens",
|
| 606 |
+
minimum=64,
|
| 607 |
+
maximum=4096,
|
| 608 |
+
step=1,
|
| 609 |
+
value=2048
|
| 610 |
)
|
| 611 |
|
| 612 |
+
temperature = gr.Slider(
|
| 613 |
+
label="Temperature",
|
| 614 |
+
minimum=0.1,
|
| 615 |
+
maximum=2.0,
|
| 616 |
+
step=0.1,
|
| 617 |
+
value=0.7
|
| 618 |
)
|
| 619 |
|
| 620 |
+
with gr.Row():
|
| 621 |
+
top_p = gr.Slider(
|
| 622 |
+
label="Top-p",
|
| 623 |
+
minimum=0.05,
|
| 624 |
+
maximum=1.0,
|
| 625 |
+
step=0.05,
|
| 626 |
+
value=0.9
|
| 627 |
+
)
|
| 628 |
+
|
| 629 |
+
top_k = gr.Slider(
|
| 630 |
+
label="Top-k",
|
| 631 |
+
minimum=1,
|
| 632 |
+
maximum=100,
|
| 633 |
+
step=1,
|
| 634 |
+
value=50
|
| 635 |
+
)
|
| 636 |
+
|
| 637 |
+
repetition_penalty = gr.Slider(
|
| 638 |
+
label="Repetition Penalty",
|
| 639 |
+
minimum=1.0,
|
| 640 |
+
maximum=2.0,
|
| 641 |
+
step=0.05,
|
| 642 |
+
value=1.0
|
| 643 |
+
)
|
| 644 |
+
|
| 645 |
+
# System Prompt
|
| 646 |
+
with gr.Accordion("๐ฌ System Prompt", open=False, elem_classes="accordion"):
|
| 647 |
+
system_prompt = gr.Textbox(
|
| 648 |
+
label="System Prompt",
|
| 649 |
+
value="You are a helpful assistant. Reasoning: medium",
|
| 650 |
+
lines=3,
|
| 651 |
+
placeholder="Customize the system prompt..."
|
| 652 |
+
)
|
| 653 |
|
| 654 |
+
# Main chat area - maximized
|
| 655 |
+
with gr.Column(scale=4):
|
| 656 |
+
with gr.Group(elem_classes="main-container chat-container"):
|
| 657 |
# Create ChatInterface with custom function
|
| 658 |
chat_interface = gr.ChatInterface(
|
| 659 |
fn=generate_response,
|
| 660 |
additional_inputs=[
|
| 661 |
+
max_tokens,
|
| 662 |
+
system_prompt,
|
| 663 |
+
temperature,
|
| 664 |
+
top_p,
|
| 665 |
+
top_k,
|
| 666 |
+
repetition_penalty
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 667 |
],
|
| 668 |
examples=[
|
| 669 |
+
[{"text": "Summarize the document"}],
|
| 670 |
+
[{"text": "What are the key points mentioned?"}],
|
| 671 |
+
[{"text": "Explain the main concept"}],
|
| 672 |
],
|
| 673 |
cache_examples=False,
|
| 674 |
type="messages",
|
| 675 |
+
title=None,
|
| 676 |
+
description=None,
|
| 677 |
textbox=gr.Textbox(
|
| 678 |
+
label="Message",
|
| 679 |
+
placeholder="Ask anything... (RAG will be applied if enabled)",
|
| 680 |
+
lines=2,
|
| 681 |
+
max_lines=10,
|
| 682 |
+
autofocus=True
|
| 683 |
+
),
|
| 684 |
+
chatbot=gr.Chatbot(
|
| 685 |
+
height=550,
|
| 686 |
+
show_copy_button=True,
|
| 687 |
+
bubble_full_width=False,
|
| 688 |
+
render_markdown=True
|
| 689 |
),
|
| 690 |
+
stop_btn="Stop",
|
| 691 |
+
retry_btn="Retry",
|
| 692 |
+
undo_btn="Undo",
|
| 693 |
+
clear_btn="Clear",
|
| 694 |
+
submit_btn="Send",
|
| 695 |
multimodal=False
|
| 696 |
)
|
| 697 |
|
|
|
|
| 707 |
outputs=[upload_status, document_list]
|
| 708 |
)
|
| 709 |
|
| 710 |
+
# Simplified RAG status update
|
| 711 |
+
def update_rag_status_simple(enable, docs, k):
|
| 712 |
+
"""Simplified RAG status update"""
|
| 713 |
+
global rag_enabled, selected_docs, top_k_chunks
|
| 714 |
+
rag_enabled = enable
|
| 715 |
+
selected_docs = docs if docs else []
|
| 716 |
+
top_k_chunks = k
|
| 717 |
+
|
| 718 |
+
if enable and docs:
|
| 719 |
+
status_html = "<div class='status-badge status-enabled'>RAG: Active</div>"
|
| 720 |
+
preview = f"<div style='font-size: 0.85rem; color: #10b981;'>๐ {len(docs)} doc(s) | {k} chunks</div>"
|
| 721 |
+
return gr.update(value=status_html), gr.update(value=preview, visible=True)
|
| 722 |
+
else:
|
| 723 |
+
status_html = "<div class='status-badge status-disabled'>RAG: Disabled</div>"
|
| 724 |
+
return gr.update(value=status_html), gr.update(value="", visible=False)
|
| 725 |
+
|
| 726 |
# Update RAG settings when changed
|
| 727 |
enable_rag.change(
|
| 728 |
+
fn=update_rag_status_simple,
|
| 729 |
inputs=[enable_rag, document_list, top_k_slider],
|
| 730 |
outputs=[rag_status, context_preview]
|
| 731 |
)
|
| 732 |
|
| 733 |
document_list.change(
|
| 734 |
+
fn=update_rag_status_simple,
|
| 735 |
inputs=[enable_rag, document_list, top_k_slider],
|
| 736 |
outputs=[rag_status, context_preview]
|
| 737 |
)
|
| 738 |
|
| 739 |
top_k_slider.change(
|
| 740 |
+
fn=update_rag_status_simple,
|
| 741 |
inputs=[enable_rag, document_list, top_k_slider],
|
| 742 |
outputs=[rag_status, context_preview]
|
| 743 |
)
|