Spaces:
Sleeping
Sleeping
import streamlit as st | |
import numpy as np | |
from tensorflow.keras.models import load_model | |
from tensorflow.keras.preprocessing.sequence import pad_sequences | |
from tensorflow.keras.preprocessing.text import Tokenizer | |
# Constants | |
MAX_LENGTH = 100 | |
TOKENIZER_PATH = "tokenizer.json" # Assuming the tokenizer is saved separately. | |
# Load pre-trained model | |
def load_trained_model(): | |
return load_model("deep_learning_model.h5") | |
# Load tokenizer | |
def load_tokenizer(): | |
import json | |
from tensorflow.keras.preprocessing.text import tokenizer_from_json | |
with open(TOKENIZER_PATH, "r") as f: | |
tokenizer_data = json.load(f) | |
return tokenizer_from_json(tokenizer_data) | |
# Preprocessing function | |
def preprocess_prompt(prompt, tokenizer, max_length): | |
sequence = tokenizer.texts_to_sequences([prompt]) | |
padded_sequence = pad_sequences(sequence, maxlen=max_length) | |
return padded_sequence | |
# Predict function | |
def detect_prompt(prompt, model, tokenizer, max_length): | |
processed_prompt = preprocess_prompt(prompt, tokenizer, max_length) | |
prediction = model.predict(processed_prompt)[0][0] | |
class_label = "Malicious" if prediction >= 0.5 else "Valid" | |
confidence_score = prediction * 100 if prediction >= 0.5 else (1 - prediction) * 100 | |
return class_label, confidence_score | |
# Streamlit App | |
st.title("Prompt Injection Detection App") | |
st.write("Detect and prevent prompt injection attacks using a deep learning model.") | |
# Load model and tokenizer | |
model = load_trained_model() | |
tokenizer = load_tokenizer() | |
# Input Section | |
user_input = st.text_area("Enter a prompt to test:", "") | |
if st.button("Detect"): | |
if user_input: | |
label, confidence = detect_prompt(user_input, model, tokenizer, MAX_LENGTH) | |
st.write(f"**Predicted Class:** {label}") | |
st.write(f"**Confidence Score:** {confidence:.2f}%") | |
else: | |
st.warning("Please enter a prompt to test.") | |
import os | |
if st.button("Train Model"): | |
os.system("python train_model.py") | |
st.success("Model training complete. Saved as deep_learning_model.h5") | |
if not os.path.exists("deep_learning_model.h5"): | |
st.info("Training the model for the first time...") | |
os.system("python train_model.py") | |
st.success("Model trained successfully and saved as deep_learning_model.h5") | |