Rootsystem2101 commited on
Commit
e1de573
·
verified ·
1 Parent(s): 1e85488

Update app.py

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