54nd339's picture
Rename main.py to app.py
ce5dbd5 verified
# app.py
import gradio as gr
import random
# In a real-world scenario, this function would use a trained machine learning model
# to analyze the IP address. For this tutorial, we'll simulate it with random logic.
def classify_ip(ip_address):
"""
Simulates an ML model classifying an IP address.
- It checks for known bad IPs.
- Otherwise, it returns a random confidence score.
"""
# A mock database of known malicious IPs
known_malicious_ips = ["103.21.244.0", "45.133.1.0", "198.51.100.7"]
if ip_address in known_malicious_ips:
# High confidence for known bad IPs
confidence = 0.95 + (random.random() * 0.04) # 95% - 99%
else:
# Random confidence for other IPs
confidence = random.random() * 0.80 # 0% - 80%
# We return the confidence score as a float
return round(confidence, 4)
# Create the Gradio interface
# We define an input of 'text' for the IP address and an output of 'number' for the score.
iface = gr.Interface(
fn=classify_ip,
inputs=gr.Textbox(label="IP Address", placeholder="Enter IP address..."),
outputs=gr.Number(label="DDoS Confidence Score"),
title="DDoS Attack IP Classifier",
description="A simple model to classify the DDoS threat level of an IP address. Returns a score between 0 (Benign) and 1 (Malicious)."
)
# Launch the app
iface.launch()