File size: 3,482 Bytes
d4e077b
 
edf2c04
d4e077b
8be8463
d4e077b
 
275333c
8be8463
d4e077b
 
1b6778c
d4e077b
1b6778c
d4e077b
 
 
1b6778c
d4e077b
edf2c04
d4e077b
 
 
1b6778c
d4e077b
8be8463
d4e077b
 
 
 
 
 
 
 
 
 
 
 
5ce3fc0
 
d4e077b
5ce3fc0
d4e077b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3eee53
 
edf2c04
d4e077b
f3eee53
d4e077b
 
 
 
1b6778c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import gradio as gr

from .expert_malware_detector import  expert_detect_malware
from .discriminative_chatter_detector import DiscriminativeChatterDetector
from .generative_malware_detector import GenerativeMalwareDetector


detector_a = DiscriminativeChatterDetector(dataset_name='blackbriar')
detector_c = GenerativeMalwareDetector()


def detect_malware_a(code: str, dependencies: str) -> str:
    """Model A detection"""
    result = detector_a.predict(code)
    return f"**Prediction:** {result['label']}"


def detect_malware_b(code: str, dependencies: str) -> str:
    """Model B detection"""
    result = expert_detect_malware(source_code=code, dependencies=dependencies)
    return f"**Prediction:** {result}"


def detect_malware_c(code: str, dependencies: str) -> str:
    """Model C detection"""
    result = detector_c.detect(code, dependencies)
    return f"**Prediction:** {result['label']}"


def clear_all_outputs():
    """
    Source - https://stackoverflow.com/a
    Posted by Vaishnav
    Retrieved 2025-11-17, License - CC BY-SA 4.0
    """
    return None, None, None, None, None


# hwr_theme = gr.themes.Default(primary_hue=gr.themes.colors.rose, secondary_hue=gr.themes.colors.red)
# with gr.Blocks(theme=hwr_theme) as demo:

with gr.Blocks() as demo:

    gr.Markdown("""
    # Malware Detection Demo

    Test three different AI models for detecting malicious code.
    One demo uses a rules-based model, one uses predictive ML ('discriminative') and one uses generative ML.

    Can you guess which is which?

    **Your task:** Try various inputs and track the responses to guess which demo is using which model type.
    """)

    with gr.Row():
        code_text = gr.Textbox(
            label="Enter source code to analyze",
            placeholder="Add your code here...",
            lines=3
        )
        dependencies_text = gr.Textbox(
            label="Enter dependency specificiation to analyze",
            placeholder="Add your dependencies here...",
            lines=3
        )

    gr.Markdown("### Compare All Three Models")


    with gr.Row():
        with gr.Column():
            gr.Markdown("#### Model A")
            output_a = gr.Markdown()
            btn_a = gr.Button("Analyze with Model A", variant="primary")

        with gr.Column():
            gr.Markdown("#### Model B")
            output_b = gr.Markdown()
            btn_b = gr.Button("Analyze with Model B", variant="primary")

        with gr.Column():
            gr.Markdown("#### Model C")
            output_c = gr.Markdown()
            btn_c = gr.Button("Analyze with Model C", variant="primary")

    # Example inputs
    gr.Examples(
        examples=[
            ["import os\nos.system('rm -rf /')", "requests==2.28.0\npandas==1.5.0"],
            ["print('Hello World')", "numpy==1.24.0"],
            ["from dspy import answer_every_question_no_hallucinations_trust_me", "dspy-ai==42.3.14"],
        ],
        inputs=[code_text, dependencies_text]
    )
    clear_btn = gr.Button("Let's try again.")

    # Connect buttons to functions
    btn_a.click(fn=detect_malware_a, inputs=[code_text, dependencies_text], outputs=output_a)
    btn_b.click(fn=detect_malware_b, inputs=[code_text, dependencies_text], outputs=output_b)
    btn_c.click(fn=detect_malware_c, inputs=[code_text, dependencies_text], outputs=output_c)
    clear_btn.click(fn=clear_all_outputs, inputs=None, outputs=[code_text, dependencies_text, output_a, output_b, output_c])