quotexhlp's picture
Create app.py
fa80d08 verified
Raw History Blame Contribute Delete
1.97 kB
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()