Text Generation
Transformers
English
AI
NLP
Cybersecurity
Ethical Hacking
Pentesting
Inference Endpoints
Canstralian commited on
Commit
f08ed33
·
verified ·
1 Parent(s): b8dba5e

Update pentest_ai_streamlit.py

Browse files
Files changed (1) hide show
  1. pentest_ai_streamlit.py +33 -46
pentest_ai_streamlit.py CHANGED
@@ -1,49 +1,36 @@
1
  import streamlit as st
2
- from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
 
3
 
4
- # Load the model and tokenizer from Hugging Face
5
- MODEL_NAME = "Canstralian/pentest_ai"
6
-
7
- @st.cache_resource
8
  def load_model():
9
- # Load the tokenizer and model
10
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
- model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
12
- return pipeline("text-generation", model=model, tokenizer=tokenizer)
13
-
14
- # Load the pentest_ai model
15
- pentest_ai = load_model()
16
-
17
- # Streamlit interface setup
18
- st.title("Pentest AI Assistant")
19
- st.write("This tool allows you to ask penetration testing and cybersecurity-related queries, and it will generate AI-powered suggestions or commands.")
20
-
21
- # Text input for user's question
22
- user_input = st.text_area("Enter your question or command:")
23
-
24
- # Button to trigger generation
25
- if st.button("Generate Response"):
26
- if user_input.strip() == "":
27
- st.error("Please enter a valid input.")
28
- else:
29
- # Generate response using the model
30
- with st.spinner("Generating response..."):
31
- response = pentest_ai(user_input, max_length=150, num_return_sequences=1)[0]['generated_text']
32
-
33
- # Display the model's response
34
- st.subheader("AI Response:")
35
- st.write(response)
36
-
37
- # Add an example button to help users see a sample
38
- if st.button("Show Example"):
39
- example_query = "How do I scan a network for open ports?"
40
- with st.spinner("Generating response for example query..."):
41
- example_response = pentest_ai(example_query, max_length=150, num_return_sequences=1)[0]['generated_text']
42
-
43
- st.subheader("Example Query:")
44
- st.write(example_query)
45
- st.subheader("AI Response:")
46
- st.write(example_response)
47
-
48
- # Instructions for the user
49
- st.info("Note: This AI model provides general advice. Always ensure you're testing on systems you have permission to, and follow legal and ethical guidelines.")
 
1
  import streamlit as st
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
5
+ @st.cache(allow_output_mutation=True)
 
 
 
6
  def load_model():
7
+ model_path = "Canstralian/pentest_ai"
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_path,
10
+ torch_dtype=torch.float16,
11
+ device_map="auto",
12
+ load_in_4bit=False,
13
+ load_in_8bit=True,
14
+ trust_remote_code=True,
15
+ )
16
+ tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
17
+ return model, tokenizer
18
+
19
+ def generate_text(model, tokenizer, instruction):
20
+ tokens = tokenizer.encode(instruction, return_tensors='pt').to('cuda')
21
+ generated_tokens = model.generate(
22
+ tokens,
23
+ max_length=1024,
24
+ top_p=1.0,
25
+ temperature=0.5,
26
+ top_k=50
27
+ )
28
+ return tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
29
+
30
+ model, tokenizer = load_model()
31
+
32
+ st.title("Penetration Testing AI Assistant")
33
+ instruction = st.text_area("Enter your question:")
34
+ if st.button("Generate"):
35
+ response = generate_text(model, tokenizer, instruction)
36
+ st.write(response)