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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import io
|
| 5 |
+
import os
|
| 6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 7 |
+
from peft import PeftModel
|
| 8 |
+
|
| 9 |
+
# ----------------------------------------------------------------------
|
| 10 |
+
# 1. MODEL SETUP (Load only once)
|
| 11 |
+
# ----------------------------------------------------------------------
|
| 12 |
+
|
| 13 |
+
BASE_MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 14 |
+
LORA_ADAPTER_ID = "RootSystem2101/ZeroCyber-SLM-LoRA-Adapter"
|
| 15 |
+
|
| 16 |
+
def load_zerocyber_model():
|
| 17 |
+
print("Loading Tokenizer...")
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(LORA_ADAPTER_ID)
|
| 19 |
+
|
| 20 |
+
print("Loading Base Model in 4-bit...")
|
| 21 |
+
# التحذيرات حول load_in_4bit طبيعية وسيتم تجاهلها
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
BASE_MODEL_ID,
|
| 24 |
+
load_in_4bit=True,
|
| 25 |
+
torch_dtype=torch.float16,
|
| 26 |
+
device_map="auto"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
print("Merging LoRA Adapter...")
|
| 30 |
+
model = PeftModel.from_pretrained(model, LORA_ADAPTER_ID)
|
| 31 |
+
# ملاحظة: التحذير حول Merge لـ 4-bit طبيعي
|
| 32 |
+
model = model.merge_and_unload()
|
| 33 |
+
model.eval()
|
| 34 |
+
|
| 35 |
+
return tokenizer, model
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
ZEROCYBER_TOKENIZER, ZEROCYBER_MODEL = load_zerocyber_model()
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"FATAL ERROR during model loading: {e}")
|
| 41 |
+
ZEROCYBER_TOKENIZER = None
|
| 42 |
+
ZEROCYBER_MODEL = None
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# ----------------------------------------------------------------------
|
| 46 |
+
# 2. CORE INFERENCE FUNCTIONS (FASTEST GENERATION MODE)
|
| 47 |
+
# ----------------------------------------------------------------------
|
| 48 |
+
|
| 49 |
+
def generate_response(prompt_text: str):
|
| 50 |
+
"""وظيفة توليد الاستجابة المُسرَّعة القصوى (Greedy Search)."""
|
| 51 |
+
if ZEROCYBER_MODEL is None:
|
| 52 |
+
return "❌ Model loading failed. Please check the command line for errors."
|
| 53 |
+
|
| 54 |
+
formatted_prompt = f"<s>[INST] {prompt_text} [/INST]"
|
| 55 |
+
inputs = ZEROCYBER_TOKENIZER(formatted_prompt, return_tensors="pt").to(ZEROCYBER_MODEL.device)
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
with torch.no_grad():
|
| 59 |
+
outputs = ZEROCYBER_MODEL.generate(
|
| 60 |
+
**inputs,
|
| 61 |
+
max_new_tokens=1024, # تقليل الكلمات لضمان سرعة عالية جداً
|
| 62 |
+
do_sample=False, # إيقاف أخذ العينات العشوائية (أسرع طريقة)
|
| 63 |
+
pad_token_id=ZEROCYBER_TOKENIZER.eos_token_id
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
response = ZEROCYBER_TOKENIZER.decode(outputs[0], skip_special_tokens=True)
|
| 67 |
+
return response.split("[/INST]")[1].strip()
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return f"❌ Internal Error during Inference: {e}"
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def analyze_log_file(file_path: str):
|
| 74 |
+
"""وظيفة تحليل ملف Log/CSV بأمان ضد مشاكل الترميز."""
|
| 75 |
+
|
| 76 |
+
# 1. Safely read file content using common encodings
|
| 77 |
+
try:
|
| 78 |
+
with open(file_path, 'r', encoding='utf-8', errors='strict') as f:
|
| 79 |
+
log_content = f.read()
|
| 80 |
+
except UnicodeDecodeError:
|
| 81 |
+
try:
|
| 82 |
+
with open(file_path, 'r', encoding='latin-1', errors='strict') as f:
|
| 83 |
+
log_content = f.read()
|
| 84 |
+
except Exception as e:
|
| 85 |
+
return f"❌ File Reading Error: {e}\nCould not read the file using common text encodings."
|
| 86 |
+
|
| 87 |
+
if not log_content.strip():
|
| 88 |
+
return "⚠️ Uploaded file is empty or does not contain readable text content."
|
| 89 |
+
|
| 90 |
+
# 2. Prompt Engineering for Cybersecurity Report (Arabic language enforced)
|
| 91 |
+
|
| 92 |
+
truncated_content = log_content[:5000]
|
| 93 |
+
|
| 94 |
+
prompt = f"""
|
| 95 |
+
You are a specialized cybersecurity analyst. Analyze the following log file content.
|
| 96 |
+
|
| 97 |
+
Your task is to:
|
| 98 |
+
1. Identify the most critical security events or errors.
|
| 99 |
+
2. Pinpoint suspicious patterns or explicit attack attempts.
|
| 100 |
+
3. **Generate a structured report in ARABIC (اللغة العربية)** including a clear summary and recommendations.
|
| 101 |
+
4. Provide immediate, actionable steps for defenders (Defenders) in a bulleted list.
|
| 102 |
+
|
| 103 |
+
Log Content (Truncated):
|
| 104 |
+
---
|
| 105 |
+
{truncated_content}
|
| 106 |
+
---
|
| 107 |
+
"""
|
| 108 |
+
|
| 109 |
+
print(f"Analyzing log content from file: {os.path.basename(file_path)}")
|
| 110 |
+
return generate_response(prompt)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# ----------------------------------------------------------------------
|
| 114 |
+
# 3. UNIFIED GRADIO INTERFACE LOGIC
|
| 115 |
+
# ----------------------------------------------------------------------
|
| 116 |
+
|
| 117 |
+
def unified_interface(question: str, log_file):
|
| 118 |
+
"""Handles either text input or file upload."""
|
| 119 |
+
|
| 120 |
+
if log_file is not None:
|
| 121 |
+
return analyze_log_file(log_file.name)
|
| 122 |
+
|
| 123 |
+
elif question.strip():
|
| 124 |
+
print(f"Received question: {question}")
|
| 125 |
+
|
| 126 |
+
# Language steering
|
| 127 |
+
if any(c in question for c in 'ءآأبتثجحخدذرزسشصضطظعغفقكلمنهويى'):
|
| 128 |
+
prompt_with_lang = f"أجب باللغة العربية. السؤال هو: {question}"
|
| 129 |
+
else:
|
| 130 |
+
prompt_with_lang = f"Answer in English. The question is: {question}"
|
| 131 |
+
|
| 132 |
+
return generate_response(prompt_with_lang)
|
| 133 |
+
|
| 134 |
+
else:
|
| 135 |
+
return "Please submit a question or upload a file for analysis."
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
# ----------------------------------------------------------------------
|
| 139 |
+
# 4. GRADIO INTERFACE BUILD (Professional English Titles)
|
| 140 |
+
# ----------------------------------------------------------------------
|
| 141 |
+
|
| 142 |
+
if __name__ == "__main__":
|
| 143 |
+
|
| 144 |
+
input_components = [
|
| 145 |
+
gr.Textbox(label="1. Ask your Cybersecurity Inquiry:", placeholder="Example: What are the steps to secure a web server?"),
|
| 146 |
+
gr.File(label="2. Or Upload any Log/Text File for Analysis:", file_types=None)
|
| 147 |
+
]
|
| 148 |
+
|
| 149 |
+
output_component = gr.Markdown(label="ZeroCyber-SLM Report / Response")
|
| 150 |
+
|
| 151 |
+
interface = gr.Interface(
|
| 152 |
+
fn=unified_interface,
|
| 153 |
+
inputs=input_components,
|
| 154 |
+
outputs=output_component,
|
| 155 |
+
# العناوين المطلوبة باللغة الإنجليزية
|
| 156 |
+
title="ZeroCyber-SLM: Security analysis and response platform",
|
| 157 |
+
description="A specialized application for responding to security inquiries and analyzing Log/CSV files to identify incidents and provide actionable recommendations for defenders.",
|
| 158 |
+
allow_flagging="never"
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
if ZEROCYBER_MODEL is not None:
|
| 162 |
+
interface.launch(share=True)
|
| 163 |
+
else:
|
| 164 |
+
print("\n❌ Interface failed to start due to model loading failure.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
pandas
|
| 4 |
+
transformers
|
| 5 |
+
peft
|
| 6 |
+
bitsandbytes
|
| 7 |
+
accelerate
|