Spaces:
Sleeping
Sleeping
Sofia Santos
commited on
Commit
·
6d79d16
1
Parent(s):
79fafa0
feat: improves ui
Browse files- tdagent/grchat.py +175 -152
tdagent/grchat.py
CHANGED
|
@@ -550,8 +550,15 @@ def _read_markdown_body_as_html(path: str = "README.md") -> str:
|
|
| 550 |
|
| 551 |
|
| 552 |
## UI components ##
|
| 553 |
-
|
| 554 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
with (
|
| 556 |
gr.Blocks(
|
| 557 |
theme=gr_themes.Origin(
|
|
@@ -562,177 +569,193 @@ with (
|
|
| 562 |
title="TDAgent",
|
| 563 |
fill_height=True,
|
| 564 |
fill_width=True,
|
|
|
|
| 565 |
) as gr_app,
|
| 566 |
-
gr.Tabs(),
|
| 567 |
):
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
value=None,
|
| 593 |
-
label="Select Model Provider",
|
| 594 |
-
)
|
| 595 |
-
|
| 596 |
-
## Amazon Bedrock Configuration ##
|
| 597 |
-
with gr.Group(visible=False) as aws_bedrock_conf_group:
|
| 598 |
-
aws_access_key_textbox = gr.Textbox(
|
| 599 |
-
label="AWS Access Key ID",
|
| 600 |
-
type="password",
|
| 601 |
-
placeholder="Enter your AWS Access Key ID",
|
| 602 |
-
)
|
| 603 |
-
aws_secret_key_textbox = gr.Textbox(
|
| 604 |
-
label="AWS Secret Access Key",
|
| 605 |
-
type="password",
|
| 606 |
-
placeholder="Enter your AWS Secret Access Key",
|
| 607 |
-
)
|
| 608 |
-
aws_region_dropdown = gr.Dropdown(
|
| 609 |
-
label="AWS Region",
|
| 610 |
-
choices=[
|
| 611 |
-
"us-east-1",
|
| 612 |
-
"us-west-2",
|
| 613 |
-
"eu-west-1",
|
| 614 |
-
"eu-central-1",
|
| 615 |
-
"ap-southeast-1",
|
| 616 |
],
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
placeholder="Enter your AWS session token",
|
| 623 |
)
|
| 624 |
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
placeholder="Enter your Hugging Face Access Token",
|
| 631 |
)
|
| 632 |
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 637 |
type="text",
|
| 638 |
-
placeholder="Enter
|
|
|
|
|
|
|
| 639 |
)
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
)
|
| 645 |
-
|
| 646 |
-
label="
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
value=
|
|
|
|
| 650 |
)
|
| 651 |
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
choices=[],
|
| 656 |
visible=False,
|
| 657 |
)
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
placeholder="Enter the model ID",
|
| 662 |
visible=False,
|
| 663 |
-
interactive=True,
|
| 664 |
)
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
choices=list(AGENT_SYSTEM_MESSAGES.keys()),
|
| 670 |
-
value=next(iter(AGENT_SYSTEM_MESSAGES.keys())),
|
| 671 |
-
label="Agent type",
|
| 672 |
-
info=(
|
| 673 |
-
"Changes the system message to pre-condition the agent"
|
| 674 |
-
" to act in a desired way."
|
| 675 |
-
),
|
| 676 |
-
)
|
| 677 |
-
agent_trace_tools_checkbox = gr.Checkbox(
|
| 678 |
-
value=False,
|
| 679 |
-
label="Trace tool calls",
|
| 680 |
-
info="Add the invoked tools trace at the end of the message",
|
| 681 |
-
)
|
| 682 |
-
|
| 683 |
-
# Initialize the temperature and max tokens based on model specs
|
| 684 |
-
temperature = gr.Slider(
|
| 685 |
-
label="Temperature",
|
| 686 |
-
minimum=0.0,
|
| 687 |
-
maximum=1.0,
|
| 688 |
-
value=0.8,
|
| 689 |
-
step=0.1,
|
| 690 |
)
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
value=2048,
|
| 696 |
-
step=64,
|
| 697 |
)
|
| 698 |
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
)
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
with gr.Column(scale=2):
|
| 718 |
-
chat_interface = gr.ChatInterface(
|
| 719 |
-
fn=gr_chat_function,
|
| 720 |
-
type="messages",
|
| 721 |
-
examples=[], # Add examples if needed
|
| 722 |
-
title="👩💻 TDAgent 👨💻",
|
| 723 |
-
description="A simple threat analyst agent with MCP tools.",
|
| 724 |
)
|
| 725 |
-
with gr.TabItem("Demo"):
|
| 726 |
-
gr.Markdown(
|
| 727 |
-
"""
|
| 728 |
-
This is a demo of TDAgent, a simple threat analyst agent with MCP tools.
|
| 729 |
-
You can configure the agent to use different LLM providers and connect to
|
| 730 |
-
various MCP servers to access tools.
|
| 731 |
-
""",
|
| 732 |
-
)
|
| 733 |
-
gr.HTML(
|
| 734 |
-
"""<iframe width="560" height="315" src="https://youtu.be/C6Z9EOW-3lE?feature=shared" frameborder="0" allowfullscreen></iframe>""", # noqa: E501
|
| 735 |
-
)
|
| 736 |
|
| 737 |
## UI Events ##
|
| 738 |
|
|
|
|
| 550 |
|
| 551 |
|
| 552 |
## UI components ##
|
| 553 |
+
custom_css = """
|
| 554 |
+
.main-header {
|
| 555 |
+
background: linear-gradient(135deg, #00a388 0%, #ffae00 100%);
|
| 556 |
+
padding: 30px;
|
| 557 |
+
border-radius: 5px;
|
| 558 |
+
margin-bottom: 20px;
|
| 559 |
+
text-align: center;
|
| 560 |
+
}
|
| 561 |
+
"""
|
| 562 |
with (
|
| 563 |
gr.Blocks(
|
| 564 |
theme=gr_themes.Origin(
|
|
|
|
| 569 |
title="TDAgent",
|
| 570 |
fill_height=True,
|
| 571 |
fill_width=True,
|
| 572 |
+
css=custom_css,
|
| 573 |
) as gr_app,
|
|
|
|
| 574 |
):
|
| 575 |
+
gr.HTML(
|
| 576 |
+
"""
|
| 577 |
+
<div class="main-header">
|
| 578 |
+
<h1>👩💻 TDAgentTools & TDAgent 👨💻</h1>
|
| 579 |
+
<p style="font-size: 1.2em; margin: 10px 0 0 0;">
|
| 580 |
+
Empowering Cybersecurity with Agentic AI
|
| 581 |
+
</p>
|
| 582 |
+
</div>
|
| 583 |
+
""",
|
| 584 |
+
)
|
| 585 |
+
with gr.Tabs():
|
| 586 |
+
with gr.TabItem("About"), gr.Row():
|
| 587 |
+
html_content = _read_markdown_body_as_html("README.md")
|
| 588 |
+
gr.Markdown(html_content)
|
| 589 |
+
|
| 590 |
+
with gr.TabItem("TDAgent"), gr.Row():
|
| 591 |
+
with gr.Column(scale=1):
|
| 592 |
+
with gr.Accordion("🔌 MCP Servers", open=False):
|
| 593 |
+
mcp_list = MutableCheckBoxGroup(
|
| 594 |
+
values=[
|
| 595 |
+
MutableCheckBoxGroupEntry(
|
| 596 |
+
name="TDAgent tools",
|
| 597 |
+
value="https://agents-mcp-hackathon-tdagenttools.hf.space/gradio_api/mcp/sse",
|
| 598 |
+
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
],
|
| 600 |
+
label="MCP Servers",
|
| 601 |
+
new_value_label="MCP endpoint",
|
| 602 |
+
new_name_label="MCP endpoint name",
|
| 603 |
+
new_value_placeholder="https://my-cool-mcp-server.com/mcp/sse",
|
| 604 |
+
new_name_placeholder="Swiss army knife of MCPs",
|
|
|
|
| 605 |
)
|
| 606 |
|
| 607 |
+
with gr.Accordion("⚙️ Provider Configuration", open=True):
|
| 608 |
+
model_provider = gr.Dropdown(
|
| 609 |
+
choices=list(MODEL_OPTIONS.keys()),
|
| 610 |
+
value=None,
|
| 611 |
+
label="Select Model Provider",
|
|
|
|
| 612 |
)
|
| 613 |
|
| 614 |
+
## Amazon Bedrock Configuration ##
|
| 615 |
+
with gr.Group(visible=False) as aws_bedrock_conf_group:
|
| 616 |
+
aws_access_key_textbox = gr.Textbox(
|
| 617 |
+
label="AWS Access Key ID",
|
| 618 |
+
type="password",
|
| 619 |
+
placeholder="Enter your AWS Access Key ID",
|
| 620 |
+
)
|
| 621 |
+
aws_secret_key_textbox = gr.Textbox(
|
| 622 |
+
label="AWS Secret Access Key",
|
| 623 |
+
type="password",
|
| 624 |
+
placeholder="Enter your AWS Secret Access Key",
|
| 625 |
+
)
|
| 626 |
+
aws_region_dropdown = gr.Dropdown(
|
| 627 |
+
label="AWS Region",
|
| 628 |
+
choices=[
|
| 629 |
+
"us-east-1",
|
| 630 |
+
"us-west-2",
|
| 631 |
+
"eu-west-1",
|
| 632 |
+
"eu-central-1",
|
| 633 |
+
"ap-southeast-1",
|
| 634 |
+
],
|
| 635 |
+
value="eu-west-1",
|
| 636 |
+
)
|
| 637 |
+
aws_session_token_textbox = gr.Textbox(
|
| 638 |
+
label="AWS Session Token",
|
| 639 |
+
type="password",
|
| 640 |
+
placeholder="Enter your AWS session token",
|
| 641 |
+
)
|
| 642 |
+
|
| 643 |
+
## Huggingface Configuration ##
|
| 644 |
+
with gr.Group(visible=False) as hf_conf_group:
|
| 645 |
+
hf_token = gr.Textbox(
|
| 646 |
+
label="HuggingFace Token",
|
| 647 |
+
type="password",
|
| 648 |
+
placeholder="Enter your Hugging Face Access Token",
|
| 649 |
+
)
|
| 650 |
+
|
| 651 |
+
## Azure Configuration ##
|
| 652 |
+
with gr.Group(visible=False) as azure_conf_group:
|
| 653 |
+
azure_endpoint = gr.Textbox(
|
| 654 |
+
label="Azure OpenAI Endpoint",
|
| 655 |
+
type="text",
|
| 656 |
+
placeholder="Enter your Azure OpenAI Endpoint",
|
| 657 |
+
)
|
| 658 |
+
azure_api_token = gr.Textbox(
|
| 659 |
+
label="Azure Access Token",
|
| 660 |
+
type="password",
|
| 661 |
+
placeholder="Enter your Azure OpenAI Access Token",
|
| 662 |
+
)
|
| 663 |
+
azure_api_version = gr.Textbox(
|
| 664 |
+
label="Azure OpenAI API Version",
|
| 665 |
+
type="text",
|
| 666 |
+
placeholder="Enter your Azure OpenAI API Version",
|
| 667 |
+
value="2024-12-01-preview",
|
| 668 |
+
)
|
| 669 |
+
|
| 670 |
+
with gr.Accordion("🧠 Model Configuration", open=True):
|
| 671 |
+
model_id_dropdown = gr.Dropdown(
|
| 672 |
+
label="Select known model id or type your own below",
|
| 673 |
+
choices=[],
|
| 674 |
+
visible=False,
|
| 675 |
+
)
|
| 676 |
+
model_id_textbox = gr.Textbox(
|
| 677 |
+
label="Model ID",
|
| 678 |
type="text",
|
| 679 |
+
placeholder="Enter the model ID",
|
| 680 |
+
visible=False,
|
| 681 |
+
interactive=True,
|
| 682 |
)
|
| 683 |
+
|
| 684 |
+
# Agent configuration options
|
| 685 |
+
with gr.Group():
|
| 686 |
+
agent_system_message_radio = gr.Radio(
|
| 687 |
+
choices=list(AGENT_SYSTEM_MESSAGES.keys()),
|
| 688 |
+
value=next(iter(AGENT_SYSTEM_MESSAGES.keys())),
|
| 689 |
+
label="Agent type",
|
| 690 |
+
info=(
|
| 691 |
+
"Changes the system message to pre-condition the agent"
|
| 692 |
+
" to act in a desired way."
|
| 693 |
+
),
|
| 694 |
+
)
|
| 695 |
+
agent_trace_tools_checkbox = gr.Checkbox(
|
| 696 |
+
value=False,
|
| 697 |
+
label="Trace tool calls",
|
| 698 |
+
info=(
|
| 699 |
+
"Add the invoked tools trace at the end of the"
|
| 700 |
+
" message"
|
| 701 |
+
),
|
| 702 |
+
)
|
| 703 |
+
|
| 704 |
+
# Initialize the temperature and max tokens based on model specs
|
| 705 |
+
temperature = gr.Slider(
|
| 706 |
+
label="Temperature",
|
| 707 |
+
minimum=0.0,
|
| 708 |
+
maximum=1.0,
|
| 709 |
+
value=0.8,
|
| 710 |
+
step=0.1,
|
| 711 |
)
|
| 712 |
+
max_tokens = gr.Slider(
|
| 713 |
+
label="Max Tokens",
|
| 714 |
+
minimum=128,
|
| 715 |
+
maximum=8192,
|
| 716 |
+
value=2048,
|
| 717 |
+
step=64,
|
| 718 |
)
|
| 719 |
|
| 720 |
+
connect_aws_bedrock_btn = gr.Button(
|
| 721 |
+
"🔌 Connect to Bedrock",
|
| 722 |
+
variant="primary",
|
|
|
|
| 723 |
visible=False,
|
| 724 |
)
|
| 725 |
+
connect_hf_btn = gr.Button(
|
| 726 |
+
"🔌 Connect to Huggingface 🤗",
|
| 727 |
+
variant="primary",
|
|
|
|
| 728 |
visible=False,
|
|
|
|
| 729 |
)
|
| 730 |
+
connect_azure_btn = gr.Button(
|
| 731 |
+
"🔌 Connect to Azure",
|
| 732 |
+
variant="primary",
|
| 733 |
+
visible=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
)
|
| 735 |
+
|
| 736 |
+
status_textbox = gr.Textbox(
|
| 737 |
+
label="Connection Status",
|
| 738 |
+
interactive=False,
|
|
|
|
|
|
|
| 739 |
)
|
| 740 |
|
| 741 |
+
with gr.Column(scale=2):
|
| 742 |
+
chat_interface = gr.ChatInterface(
|
| 743 |
+
fn=gr_chat_function,
|
| 744 |
+
type="messages",
|
| 745 |
+
examples=[], # Add examples if needed
|
| 746 |
+
description="A simple threat analyst agent with MCP tools.",
|
| 747 |
+
)
|
| 748 |
+
with gr.TabItem("Demo"):
|
| 749 |
+
gr.Markdown(
|
| 750 |
+
"""
|
| 751 |
+
This is a demo of TDAgent, a simple threat analyst agent with MCP tools.
|
| 752 |
+
You can configure the agent to use different LLM providers and connect to
|
| 753 |
+
various MCP servers to access tools.
|
| 754 |
+
""",
|
| 755 |
)
|
| 756 |
+
gr.HTML(
|
| 757 |
+
"""<iframe width="560" height="315" src="https://youtu.be/C6Z9EOW-3lE?feature=shared" frameborder="0" allowfullscreen></iframe>""", # noqa: E501
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 758 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 759 |
|
| 760 |
## UI Events ##
|
| 761 |
|