Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,73 +2,81 @@ import streamlit as st
|
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForSeq2SeqLM
|
3 |
import torch
|
4 |
|
5 |
-
# Define the model names
|
6 |
-
|
7 |
-
"CyberAttackDetection": "Canstralian/CyberAttackDetection",
|
8 |
"text2shellcommands": "Canstralian/text2shellcommands",
|
9 |
-
"pentest_ai": "Canstralian/pentest_ai"
|
10 |
}
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
try:
|
14 |
-
#
|
15 |
if model_name == "Canstralian/text2shellcommands":
|
16 |
-
model_name = "t5-small"
|
17 |
|
18 |
-
# Load the
|
19 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
20 |
if "seq2seq" in model_name.lower():
|
21 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
22 |
else:
|
23 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
24 |
-
|
25 |
return tokenizer, model
|
26 |
except Exception as e:
|
27 |
st.error(f"Error loading model: {e}")
|
28 |
return None, None
|
29 |
|
30 |
-
def validate_input(user_input):
|
31 |
-
if not user_input:
|
32 |
-
st.error("Please enter some text for prediction.")
|
33 |
-
return False
|
34 |
-
return True
|
35 |
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
inputs = tokenizer(user_input, return_tensors="pt", padding=True, truncation=True)
|
39 |
with torch.no_grad():
|
40 |
outputs = model(**inputs)
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
45 |
|
46 |
-
def main():
|
47 |
-
st.sidebar.header("Model Configuration")
|
48 |
-
model_choice = st.sidebar.selectbox("Select a model", [
|
49 |
-
"CyberAttackDetection",
|
50 |
-
"text2shellcommands",
|
51 |
-
"pentest_ai"
|
52 |
-
])
|
53 |
|
54 |
-
|
|
|
|
|
55 |
|
56 |
-
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
user_input = st.text_area("Enter text:")
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
predicted_class = torch.argmax(logits, dim=-1).item()
|
70 |
-
st.write(f"Predicted Class: {predicted_class}")
|
71 |
-
st.write(f"Logits: {logits}")
|
72 |
|
73 |
if __name__ == "__main__":
|
74 |
-
main()
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForSeq2SeqLM
|
3 |
import torch
|
4 |
|
5 |
+
# Define the model names and mappings
|
6 |
+
MODEL_MAPPING = {
|
|
|
7 |
"text2shellcommands": "Canstralian/text2shellcommands",
|
8 |
+
"pentest_ai": "Canstralian/pentest_ai",
|
9 |
}
|
10 |
|
11 |
+
# Sidebar for model selection
|
12 |
+
def select_model():
|
13 |
+
st.sidebar.header("Model Configuration")
|
14 |
+
return st.sidebar.selectbox("Select a model", list(MODEL_MAPPING.keys()))
|
15 |
+
|
16 |
+
|
17 |
+
# Load model and tokenizer with caching
|
18 |
+
@st.cache_resource
|
19 |
+
def load_model_and_tokenizer(model_name):
|
20 |
try:
|
21 |
+
# Use a fallback model for testing
|
22 |
if model_name == "Canstralian/text2shellcommands":
|
23 |
+
model_name = "t5-small"
|
24 |
|
25 |
+
# Load the tokenizer and model
|
26 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
27 |
if "seq2seq" in model_name.lower():
|
28 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
29 |
else:
|
30 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
31 |
+
|
32 |
return tokenizer, model
|
33 |
except Exception as e:
|
34 |
st.error(f"Error loading model: {e}")
|
35 |
return None, None
|
36 |
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
# Handle predictions
|
39 |
+
def predict_with_model(user_input, model, tokenizer, model_choice):
|
40 |
+
if model_choice == "text2shellcommands":
|
41 |
+
# Generate shell commands
|
42 |
+
inputs = tokenizer(user_input, return_tensors="pt", padding=True, truncation=True)
|
43 |
+
with torch.no_grad():
|
44 |
+
outputs = model.generate(**inputs)
|
45 |
+
generated_command = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
46 |
+
return {"Generated Shell Command": generated_command}
|
47 |
+
else:
|
48 |
+
# Perform classification
|
49 |
inputs = tokenizer(user_input, return_tensors="pt", padding=True, truncation=True)
|
50 |
with torch.no_grad():
|
51 |
outputs = model(**inputs)
|
52 |
+
logits = outputs.logits
|
53 |
+
predicted_class = torch.argmax(logits, dim=-1).item()
|
54 |
+
return {
|
55 |
+
"Predicted Class": predicted_class,
|
56 |
+
"Logits": logits.tolist(),
|
57 |
+
}
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
# Main Streamlit app
|
61 |
+
def main():
|
62 |
+
st.title("AI Model Inference Dashboard")
|
63 |
|
64 |
+
# Model selection
|
65 |
+
model_choice = select_model()
|
66 |
+
model_name = MODEL_MAPPING.get(model_choice)
|
67 |
+
tokenizer, model = load_model_and_tokenizer(model_name)
|
68 |
|
69 |
+
# Input text box
|
70 |
user_input = st.text_area("Enter text:")
|
71 |
|
72 |
+
# Perform prediction if input and models are available
|
73 |
+
if user_input and model and tokenizer:
|
74 |
+
result = predict_with_model(user_input, model, tokenizer, model_choice)
|
75 |
+
for key, value in result.items():
|
76 |
+
st.write(f"{key}: {value}")
|
77 |
+
else:
|
78 |
+
st.info("Please enter some text for prediction.")
|
79 |
+
|
|
|
|
|
|
|
80 |
|
81 |
if __name__ == "__main__":
|
82 |
+
main()
|