Spaces:
Sleeping
Sleeping
invincible-jha
commited on
Upload app.py
Browse files
app.py
CHANGED
@@ -7,32 +7,53 @@ import threading
|
|
7 |
|
8 |
# Load AI models
|
9 |
def load_models():
|
10 |
-
models = {
|
11 |
-
|
12 |
-
|
13 |
-
"
|
14 |
-
|
15 |
-
|
16 |
-
"
|
17 |
-
"
|
18 |
-
|
19 |
-
|
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
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def analyze_file(file, model_name):
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
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 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
print(f"Alert: {alert}")
|
53 |
|
54 |
# Gradio interface
|
55 |
-
def
|
56 |
with gr.Blocks() as demo:
|
57 |
gr.Markdown("# Cybersecurity AI Platform")
|
58 |
-
|
59 |
-
with gr.Tab("Text
|
60 |
-
text_input = gr.Textbox(
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
text_output = gr.Textbox(label="Analysis Result")
|
63 |
text_button = gr.Button("Analyze Text")
|
64 |
-
text_button.click(
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
with gr.Tab("File
|
67 |
file_input = gr.File(label="Upload file for analysis")
|
68 |
-
|
|
|
|
|
|
|
|
|
69 |
file_output = gr.Textbox(label="Analysis Result")
|
70 |
file_button = gr.Button("Analyze File")
|
71 |
-
file_button.click(
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
with gr.Tab("Real-time
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
demo
|
81 |
|
82 |
if __name__ == "__main__":
|
83 |
-
|
|
|
|
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()
|