deepugaur commited on
Commit
a921e9d
·
verified ·
1 Parent(s): fed896f

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -75
app.py DELETED
@@ -1,75 +0,0 @@
1
-
2
-
3
- import streamlit as st
4
- import numpy as np
5
- import pandas as pd
6
- from tensorflow.keras.preprocessing.text import Tokenizer
7
- from tensorflow.keras.preprocessing.sequence import pad_sequences
8
- from tensorflow.keras.models import load_model
9
- from lime.lime_text import LimeTextExplainer
10
- import matplotlib.pyplot as plt
11
- import seaborn as sns
12
-
13
- # Load the trained model
14
- @st.cache_resource
15
- def load_trained_model():
16
- model = load_model("deep_learning_model.h5")
17
- return model
18
-
19
- model = load_trained_model()
20
-
21
- # Tokenizer setup
22
- tokenizer = Tokenizer(num_words=5000)
23
- max_length = 100
24
-
25
- # Load Data
26
- @st.cache_data
27
- def load_data():
28
- data = pd.read_csv("train prompt.csv", sep=',', quoting=3, encoding='ISO-8859-1', on_bad_lines='skip', engine='python')
29
- data['label'] = data['label'].replace({'valid': 0, 'malicious': 1})
30
- return data
31
-
32
- data = load_data()
33
- tokenizer.fit_on_texts(data['input'].values)
34
-
35
- # Preprocessing functions
36
- def preprocess_prompt(prompt, tokenizer, max_length):
37
- sequence = tokenizer.texts_to_sequences([prompt])
38
- padded_sequence = pad_sequences(sequence, maxlen=max_length)
39
- return padded_sequence
40
-
41
- def detect_prompt(prompt, model, tokenizer, max_length):
42
- processed_prompt = preprocess_prompt(prompt, tokenizer, max_length)
43
- prediction = model.predict(processed_prompt)[0][0]
44
- class_label = 'Malicious' if prediction >= 0.5 else 'Valid'
45
- confidence_score = prediction * 100 if prediction >= 0.5 else (1 - prediction) * 100
46
- return class_label, confidence_score
47
-
48
- # Load model
49
- model = load_trained_model()
50
-
51
- # Streamlit app
52
- st.title("Prompt Injection Attack Detection")
53
- st.write("This application detects malicious prompts to prevent injection attacks.")
54
-
55
- prompt = st.text_input("Enter a prompt to analyze:")
56
-
57
- if prompt:
58
- class_label, confidence = detect_prompt(prompt, model, tokenizer, max_length)
59
- st.write(f"### Prediction: {class_label}")
60
- st.write(f"Confidence: {confidence:.2f}%")
61
-
62
- # LIME explanation
63
- st.write("Generating LIME Explanation...")
64
- explainer = LimeTextExplainer(class_names=["Valid", "Malicious"])
65
-
66
- def predict_fn(prompts):
67
- sequences = tokenizer.texts_to_sequences(prompts)
68
- padded_sequences = pad_sequences(sequences, maxlen=max_length)
69
- predictions = model.predict(padded_sequences)
70
- return np.hstack([1 - predictions, predictions])
71
-
72
- explanation = explainer.explain_instance(prompt, predict_fn, num_features=10)
73
- fig = explanation.as_pyplot_figure()
74
- st.pyplot(fig)
75
-