CyberSecAI / app.py
invincible-jha's picture
Upload 8 files
de38b77 verified
raw
history blame
3.32 kB
import gradio as gr
from transformers import pipeline
import requests
import json
import time
import threading
# Load AI models
def load_models():
models = {
"gpt-3": pipeline("text-generation", model="gpt-3"),
"bert-base-uncased": pipeline("text-classification", model="bert-base-uncased"),
"roberta-large": pipeline("text-classification", model="roberta-large"),
"distilbert-base-uncased": pipeline("text-classification", model="distilbert-base-uncased"),
"albert-base-v2": pipeline("text-classification", model="albert-base-v2"),
"tinybert": pipeline("text-classification", model="tinybert"),
"cybersecurity-bert": pipeline("text-classification", model="cybersecurity-bert"),
"malware-detection-bert": pipeline("text-classification", model="malware-detection-bert"),
"phishing-detection-bert": pipeline("text-classification", model="phishing-detection-bert")
}
return models
models = load_models()
# Define functions to interact with AI models
def analyze_text(text, model_name):
model = models.get(model_name)
if model:
return model(text)
else:
return "Model not found."
def analyze_file(file, model_name):
content = file.read().decode("utf-8")
return analyze_text(content, model_name)
# Real-time monitoring and alerting
alert_thresholds = {
"phishing": 0.8,
"malware": 0.8,
"anomaly": 0.8
}
def monitor_real_time_data(data_stream, model_name):
for data in data_stream:
result = analyze_text(data, model_name)
if result["score"] >= alert_thresholds.get(model_name, 0.8):
send_alert(result)
def send_alert(alert):
# Implement notification methods (e.g., email, SMS, in-app notifications)
print(f"Alert: {alert}")
# Gradio interface
def gradio_interface():
with gr.Blocks() as demo:
gr.Markdown("# Cybersecurity AI Platform")
with gr.Tab("Text Input"):
text_input = gr.Textbox(label="Enter text for analysis")
model_dropdown = gr.Dropdown(choices=list(models.keys()), label="Select AI Model")
text_output = gr.Textbox(label="Analysis Result")
text_button = gr.Button("Analyze Text")
text_button.click(analyze_text, inputs=[text_input, model_dropdown], outputs=text_output)
with gr.Tab("File Upload"):
file_input = gr.File(label="Upload file for analysis")
model_dropdown = gr.Dropdown(choices=list(models.keys()), label="Select AI Model")
file_output = gr.Textbox(label="Analysis Result")
file_button = gr.Button("Analyze File")
file_button.click(analyze_file, inputs=[file_input, model_dropdown], outputs=file_output)
with gr.Tab("Real-time Data Stream"):
data_stream_input = gr.Textbox(label="Enter real-time data stream URL")
model_dropdown = gr.Dropdown(choices=list(models.keys()), label="Select AI Model")
real_time_output = gr.Textbox(label="Real-time Monitoring Result")
real_time_button = gr.Button("Start Monitoring")
real_time_button.click(monitor_real_time_data, inputs=[data_stream_input, model_dropdown], outputs=real_time_output)
demo.launch()
if __name__ == "__main__":
gradio_interface()