Spaces:
Runtime error
Runtime error
File size: 1,966 Bytes
fa80d08 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
from utils.code_analysis import analyze_code
from utils.log_analyzer import analyze_log
from utils.ctf_helper import explain_ctf
# Load Mistral-7B or similar free model
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct")
model = AutoModelForCausalLM.from_pretrained(
"mistralai/Mistral-7B-Instruct",
torch_dtype=torch.float16,
device_map="auto"
)
def tutor_chat(message):
prompt = f"[INST] {message} [/INST]"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=300)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
def code_analysis(file):
code = file.read().decode()
return analyze_code(code)
def log_analysis(file):
log_data = file.read().decode()
return analyze_log(log_data)
def ctf_help(question):
return explain_ctf(question)
# UI
with gr.Blocks() as demo:
gr.Markdown("# π‘οΈ Ethical Hacking AI Assistant")
with gr.Tab("π¬ Tutor Chat"):
inp = gr.Textbox(label="Ask about hacking tools or techniques")
out = gr.Textbox(label="AI Response")
inp.submit(tutor_chat, inp, out)
with gr.Tab("π Code Analyzer"):
code_file = gr.File(label="Upload source code file")
result = gr.Textbox(label="Security Report")
code_file.change(fn=code_analysis, inputs=code_file, outputs=result)
with gr.Tab("π Log Analyzer"):
log_file = gr.File(label="Upload log file")
log_result = gr.Textbox(label="Suspicious Patterns")
log_file.change(fn=log_analysis, inputs=log_file, outputs=log_result)
with gr.Tab("π― CTF Challenge Helper"):
ctf_input = gr.Textbox(label="Describe your CTF challenge")
ctf_output = gr.Textbox(label="Explanation")
ctf_input.submit(ctf_help, ctf_input, ctf_output)
demo.launch()
|