|
import streamlit as st |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForSeq2SeqLM |
|
import torch |
|
|
|
|
|
st.sidebar.header("Model Configuration") |
|
model_choice = st.sidebar.selectbox("Select a model", [ |
|
"CyberAttackDetection", |
|
"text2shellcommands", |
|
"pentest_ai" |
|
]) |
|
|
|
|
|
model_mapping = { |
|
"CyberAttackDetection": "Canstralian/CyberAttackDetection", |
|
"text2shellcommands": "Canstralian/text2shellcommands", |
|
"pentest_ai": "Canstralian/pentest_ai" |
|
} |
|
|
|
model_name = model_mapping.get(model_choice, "Canstralian/CyberAttackDetection") |
|
|
|
|
|
@st.cache_resource |
|
def load_model(model_name): |
|
try: |
|
|
|
if model_name == "Canstralian/text2shellcommands": |
|
model_name = "t5-small" |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
if "seq2seq" in model_name.lower(): |
|
model = AutoModelForSeq2SeqLM.from_pretrained(model_name) |
|
else: |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
|
|
return tokenizer, model |
|
except Exception as e: |
|
st.error(f"Error loading model: {e}") |
|
return None, None |
|
|
|
|
|
tokenizer, model = load_model(model_name) |
|
|
|
|
|
st.title(f"{model_choice} Model") |
|
user_input = st.text_area("Enter text:") |
|
|
|
|
|
if user_input and model and tokenizer: |
|
if model_choice == "text2shellcommands": |
|
|
|
inputs = tokenizer(user_input, return_tensors="pt", padding=True, truncation=True) |
|
with torch.no_grad(): |
|
outputs = model.generate(**inputs) |
|
generated_command = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
st.write(f"Generated Shell Command: {generated_command}") |
|
|
|
else: |
|
|
|
inputs = tokenizer(user_input, return_tensors="pt", padding=True, truncation=True) |
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
predicted_class = torch.argmax(logits, dim=-1).item() |
|
st.write(f"Predicted Class: {predicted_class}") |
|
st.write(f"Logits: {logits}") |
|
|
|
else: |
|
st.info("Please enter some text for prediction.") |
|
|