munichpavel commited on
Commit
275333c
·
1 Parent(s): 7215af4

Add training dataset switcher

Browse files
src/ai_security/chatter_demo.py CHANGED
@@ -3,10 +3,17 @@ import gradio as gr
3
 
4
  from .generative_chatter_detector import GenerativeChatterDetector
5
  from .rules_chatter_detector import simple_normalized_blacbriar_chatter_detector
6
- from .discriminative_chatter_detector import DiscriminativeChatterDetector
 
 
 
 
 
 
 
 
7
 
8
 
9
- discriminative_detector = DiscriminativeChatterDetector(scope='blackbriar-only')
10
  generative_detector = GenerativeChatterDetector(scope='blackbriar-only')
11
 
12
 
@@ -57,14 +64,26 @@ with gr.Blocks(theme=hwr_theme) as demo:
57
  """)
58
 
59
  with gr.Row():
60
- input_text = gr.Textbox(
61
- label="Enter text to analyze",
62
- placeholder="Type your message here...",
63
- lines=3
 
 
 
 
 
 
 
64
  )
 
 
65
 
66
  gr.Markdown("### Compare All Three Models")
67
 
 
 
 
68
  with gr.Row():
69
  with gr.Column():
70
  gr.Markdown("#### Model A")
@@ -81,7 +100,6 @@ with gr.Blocks(theme=hwr_theme) as demo:
81
  output_c = gr.Markdown()
82
  btn_c = gr.Button("Analyze with Model C", variant="primary")
83
 
84
- # Example inputs
85
  gr.Examples(
86
  examples=[
87
  [" Bourne's just the tip of the iceberg. Have you heard of an 'Operation Blackbriar'?"],
@@ -93,8 +111,13 @@ with gr.Blocks(theme=hwr_theme) as demo:
93
  )
94
  clear_btn = gr.Button("Let's try again.")
95
 
96
- # Connect buttons to functions
97
  btn_a.click(fn=detect_chatter_a, inputs=input_text, outputs=output_a)
98
  btn_b.click(fn=detect_chatter_b, inputs=input_text, outputs=output_b)
99
  btn_c.click(fn=detect_chatter_c, inputs=input_text, outputs=output_c)
100
- clear_btn.click(fn=clear_all_outputs, inputs=None, outputs=[input_text, output_a, output_b, output_c])
 
 
 
 
 
 
 
3
 
4
  from .generative_chatter_detector import GenerativeChatterDetector
5
  from .rules_chatter_detector import simple_normalized_blacbriar_chatter_detector
6
+ from .discriminative_chatter_detector import DatasetName, DiscriminativeChatterDetector
7
+
8
+
9
+
10
+ def update_dataset(selected_dataset):
11
+ """Reinitialize discriminative detector with new dataset"""
12
+ global discriminative_detector
13
+ discriminative_detector = DiscriminativeChatterDetector(dataset_name=selected_dataset)
14
+ return f"✓ Classic ML model now trained on: {selected_dataset}"
15
 
16
 
 
17
  generative_detector = GenerativeChatterDetector(scope='blackbriar-only')
18
 
19
 
 
64
  """)
65
 
66
  with gr.Row():
67
+ with gr.Column(scale=2):
68
+ input_text = gr.Textbox(
69
+ label="Enter text to analyze",
70
+ placeholder="Type your message here...",
71
+ lines=3
72
+ )
73
+ with gr.Column(scale=1):
74
+ dataset_name = gr.Dropdown(
75
+ choices=[a_name.value for a_name in DatasetName],
76
+ value='blackbriar', # Set default value
77
+ label="(Optional) Change training Dataset for Classic ML"
78
  )
79
+ dataset_status = gr.Markdown("Currently using: blackbriar")
80
+
81
 
82
  gr.Markdown("### Compare All Three Models")
83
 
84
+ # with gr.Row():
85
+
86
+
87
  with gr.Row():
88
  with gr.Column():
89
  gr.Markdown("#### Model A")
 
100
  output_c = gr.Markdown()
101
  btn_c = gr.Button("Analyze with Model C", variant="primary")
102
 
 
103
  gr.Examples(
104
  examples=[
105
  [" Bourne's just the tip of the iceberg. Have you heard of an 'Operation Blackbriar'?"],
 
111
  )
112
  clear_btn = gr.Button("Let's try again.")
113
 
 
114
  btn_a.click(fn=detect_chatter_a, inputs=input_text, outputs=output_a)
115
  btn_b.click(fn=detect_chatter_b, inputs=input_text, outputs=output_b)
116
  btn_c.click(fn=detect_chatter_c, inputs=input_text, outputs=output_c)
117
+ clear_btn.click(fn=clear_all_outputs, inputs=None, outputs=[input_text, output_a, output_b, output_c])
118
+
119
+ dataset_name.change(
120
+ fn=update_dataset,
121
+ inputs=dataset_name,
122
+ outputs=dataset_status
123
+ )
src/ai_security/discriminative_chatter_detector.py CHANGED
@@ -1,19 +1,27 @@
1
  """
2
- Chatter detection using bag-of-words and multinomial bayes
3
  """
4
- import pandas as pd
5
  from pathlib import Path
 
 
6
  from sklearn.feature_extraction.text import CountVectorizer
7
  from sklearn.linear_model import LogisticRegression
8
  from sklearn.pipeline import Pipeline
9
 
10
 
11
- DATASET_NAME_FILENAME_MAPPING = {
12
- 'blackbriar': 'chatter-detection-dataset - blackbriar-chatter-detection-dataset.csv',
13
- 'extended': 'chatter-detection-dataset - extended-chatter-detection-dataset.csv',
14
- 'baby-blackbriar': 'chatter-detection-dataset - baby-chatter-detection-dataset.csv',
15
- 'toddler-blackbriar': 'chatter-detection-dataset - toddler-chatter-detection-dataset.csv'
 
16
 
 
 
 
 
 
17
  }
18
 
19
 
 
1
  """
2
+ Chatter detection using discriminative ML
3
  """
4
+ from enum import Enum
5
  from pathlib import Path
6
+
7
+ import pandas as pd
8
  from sklearn.feature_extraction.text import CountVectorizer
9
  from sklearn.linear_model import LogisticRegression
10
  from sklearn.pipeline import Pipeline
11
 
12
 
13
+ class DatasetName(Enum):
14
+ baby_blackbriar = 'baby-blackbriar'
15
+ toddler_blackbriar = 'toddler-blackbriar'
16
+ blackbriar = 'blackbriar'
17
+ extended = 'extended'
18
+
19
 
20
+ DATASET_NAME_FILENAME_MAPPING = {
21
+ DatasetName.baby_blackbriar.value : 'chatter-detection-dataset - baby-chatter-detection-dataset.csv',
22
+ DatasetName.toddler_blackbriar.value: 'chatter-detection-dataset - toddler-chatter-detection-dataset.csv',
23
+ DatasetName.blackbriar.value: 'chatter-detection-dataset - blackbriar-chatter-detection-dataset.csv',
24
+ DatasetName.extended.value: 'chatter-detection-dataset - extended-chatter-detection-dataset.csv',
25
  }
26
 
27
 
src/ai_security/malware_demo.py CHANGED
@@ -5,7 +5,7 @@ from .discriminative_chatter_detector import DiscriminativeChatterDetector
5
  from .generative_malware_detector import GenerativeMalwareDetector
6
 
7
 
8
- detector_a = DiscriminativeChatterDetector(scope='blackbriar-only')
9
  detector_c = GenerativeMalwareDetector()
10
 
11
 
 
5
  from .generative_malware_detector import GenerativeMalwareDetector
6
 
7
 
8
+ detector_a = DiscriminativeChatterDetector(dataset_name='blackbriar')
9
  detector_c = GenerativeMalwareDetector()
10
 
11