invincible-jha commited on
Commit
4c6dd82
·
verified ·
1 Parent(s): d308b85

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -43
app.py CHANGED
@@ -7,32 +7,53 @@ import threading
7
 
8
  # Load AI models
9
  def load_models():
10
- models = {
11
- "gpt-3": pipeline("text-generation", model="gpt-3"),
12
- "bert-base-uncased": pipeline("text-classification", model="bert-base-uncased"),
13
- "roberta-large": pipeline("text-classification", model="roberta-large"),
14
- "distilbert-base-uncased": pipeline("text-classification", model="distilbert-base-uncased"),
15
- "albert-base-v2": pipeline("text-classification", model="albert-base-v2"),
16
- "tinybert": pipeline("text-classification", model="tinybert"),
17
- "cybersecurity-bert": pipeline("text-classification", model="cybersecurity-bert"),
18
- "malware-detection-bert": pipeline("text-classification", model="malware-detection-bert"),
19
- "phishing-detection-bert": pipeline("text-classification", model="phishing-detection-bert")
20
- }
 
 
 
 
 
 
 
 
 
21
  return models
22
 
23
- models = load_models()
24
-
25
  # Define functions to interact with AI models
26
  def analyze_text(text, model_name):
 
 
 
27
  model = models.get(model_name)
28
- if model:
29
- return model(text)
30
- else:
31
- return "Model not found."
 
 
 
 
 
 
 
 
32
 
33
  def analyze_file(file, model_name):
34
- content = file.read().decode("utf-8")
35
- return analyze_text(content, model_name)
 
 
 
36
 
37
  # Real-time monitoring and alerting
38
  alert_thresholds = {
@@ -42,42 +63,77 @@ alert_thresholds = {
42
  }
43
 
44
  def monitor_real_time_data(data_stream, model_name):
45
- for data in data_stream:
46
- result = analyze_text(data, model_name)
47
- if result["score"] >= alert_thresholds.get(model_name, 0.8):
48
- send_alert(result)
 
 
 
 
 
49
 
50
- def send_alert(alert):
51
- # Implement notification methods (e.g., email, SMS, in-app notifications)
52
- print(f"Alert: {alert}")
53
 
54
  # Gradio interface
55
- def gradio_interface():
56
  with gr.Blocks() as demo:
57
  gr.Markdown("# Cybersecurity AI Platform")
58
-
59
- with gr.Tab("Text Input"):
60
- text_input = gr.Textbox(label="Enter text for analysis")
61
- model_dropdown = gr.Dropdown(choices=list(models.keys()), label="Select AI Model")
 
 
 
 
 
 
 
62
  text_output = gr.Textbox(label="Analysis Result")
63
  text_button = gr.Button("Analyze Text")
64
- text_button.click(analyze_text, inputs=[text_input, model_dropdown], outputs=text_output)
 
 
 
 
65
 
66
- with gr.Tab("File Upload"):
67
  file_input = gr.File(label="Upload file for analysis")
68
- model_dropdown = gr.Dropdown(choices=list(models.keys()), label="Select AI Model")
 
 
 
 
69
  file_output = gr.Textbox(label="Analysis Result")
70
  file_button = gr.Button("Analyze File")
71
- file_button.click(analyze_file, inputs=[file_input, model_dropdown], outputs=file_output)
 
 
 
 
72
 
73
- with gr.Tab("Real-time Data Stream"):
74
- data_stream_input = gr.Textbox(label="Enter real-time data stream URL")
75
- model_dropdown = gr.Dropdown(choices=list(models.keys()), label="Select AI Model")
76
- real_time_output = gr.Textbox(label="Real-time Monitoring Result")
77
- real_time_button = gr.Button("Start Monitoring")
78
- real_time_button.click(monitor_real_time_data, inputs=[data_stream_input, model_dropdown], outputs=real_time_output)
 
 
 
 
 
 
 
 
 
 
 
79
 
80
- demo.launch()
81
 
82
  if __name__ == "__main__":
83
- gradio_interface()
 
 
7
 
8
  # Load AI models
9
  def load_models():
10
+ models = {}
11
+ try:
12
+ # Text generation model (using smaller open source alternative)
13
+ models["gpt2"] = pipeline("text-generation", model="gpt2")
14
+
15
+ # Classification models
16
+ models["bert-base"] = pipeline("text-classification", model="bert-base-uncased")
17
+ models["distilbert"] = pipeline("text-classification", model="distilbert-base-uncased")
18
+
19
+ # Cybersecurity specific models
20
+ models["phishing-bert"] = pipeline(
21
+ "text-classification",
22
+ model="deepset/bert-base-cased-squad2" # Using a QA model that can be fine-tuned for security
23
+ )
24
+
25
+ except Exception as e:
26
+ print(f"Error loading models: {str(e)}")
27
+ # Fallback to at least one working model
28
+ models["distilbert"] = pipeline("text-classification", model="distilbert-base-uncased")
29
+
30
  return models
31
 
 
 
32
  # Define functions to interact with AI models
33
  def analyze_text(text, model_name):
34
+ if not text.strip():
35
+ return "Please provide some text to analyze."
36
+
37
  model = models.get(model_name)
38
+ if not model:
39
+ return f"Model {model_name} not found. Available models: {', '.join(models.keys())}"
40
+
41
+ try:
42
+ if model_name == "gpt2":
43
+ result = model(text, max_length=100, num_return_sequences=1)
44
+ return result[0]['generated_text']
45
+ else:
46
+ result = model(text)
47
+ return str(result)
48
+ except Exception as e:
49
+ return f"Error analyzing text: {str(e)}"
50
 
51
  def analyze_file(file, model_name):
52
+ try:
53
+ content = file.read().decode("utf-8")
54
+ return analyze_text(content, model_name)
55
+ except Exception as e:
56
+ return f"Error processing file: {str(e)}"
57
 
58
  # Real-time monitoring and alerting
59
  alert_thresholds = {
 
63
  }
64
 
65
  def monitor_real_time_data(data_stream, model_name):
66
+ if not data_stream.strip():
67
+ return "Please provide a data stream URL or content."
68
+
69
+ try:
70
+ # For demo purposes, we'll analyze the provided text as a single data point
71
+ result = analyze_text(data_stream, model_name)
72
+ return f"Monitoring result: {result}"
73
+ except Exception as e:
74
+ return f"Error monitoring data: {str(e)}"
75
 
76
+ # Load models at startup
77
+ models = load_models()
 
78
 
79
  # Gradio interface
80
+ def create_gradio_interface():
81
  with gr.Blocks() as demo:
82
  gr.Markdown("# Cybersecurity AI Platform")
83
+
84
+ with gr.Tab("Text Analysis"):
85
+ text_input = gr.Textbox(
86
+ label="Enter text for analysis",
87
+ placeholder="Enter text here..."
88
+ )
89
+ model_dropdown = gr.Dropdown(
90
+ choices=list(models.keys()),
91
+ value=list(models.keys())[0],
92
+ label="Select AI Model"
93
+ )
94
  text_output = gr.Textbox(label="Analysis Result")
95
  text_button = gr.Button("Analyze Text")
96
+ text_button.click(
97
+ analyze_text,
98
+ inputs=[text_input, model_dropdown],
99
+ outputs=text_output
100
+ )
101
 
102
+ with gr.Tab("File Analysis"):
103
  file_input = gr.File(label="Upload file for analysis")
104
+ file_model_dropdown = gr.Dropdown(
105
+ choices=list(models.keys()),
106
+ value=list(models.keys())[0],
107
+ label="Select AI Model"
108
+ )
109
  file_output = gr.Textbox(label="Analysis Result")
110
  file_button = gr.Button("Analyze File")
111
+ file_button.click(
112
+ analyze_file,
113
+ inputs=[file_input, file_model_dropdown],
114
+ outputs=file_output
115
+ )
116
 
117
+ with gr.Tab("Real-time Monitoring"):
118
+ stream_input = gr.Textbox(
119
+ label="Enter data stream content",
120
+ placeholder="Enter data to monitor..."
121
+ )
122
+ stream_model_dropdown = gr.Dropdown(
123
+ choices=list(models.keys()),
124
+ value=list(models.keys())[0],
125
+ label="Select AI Model"
126
+ )
127
+ stream_output = gr.Textbox(label="Monitoring Result")
128
+ stream_button = gr.Button("Start Monitoring")
129
+ stream_button.click(
130
+ monitor_real_time_data,
131
+ inputs=[stream_input, stream_model_dropdown],
132
+ outputs=stream_output
133
+ )
134
 
135
+ return demo
136
 
137
  if __name__ == "__main__":
138
+ demo = create_gradio_interface()
139
+ demo.launch()