camawowo commited on
Commit
4ff3d5f
·
1 Parent(s): 12fa2c2

convert to GGUF optimized CPU bug bounty agent

Browse files
Files changed (1) hide show
  1. app.py +155 -0
app.py CHANGED
@@ -215,7 +215,162 @@ def chat(message, history):
215
 
216
  # =========================
217
  # UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
  # =========================
 
 
 
 
 
 
 
 
 
 
219
 
220
  demo = gr.ChatInterface(
221
  fn=chat,
 
215
 
216
  # =========================
217
  # UI
218
+ # =================import gradio as gr
219
+ import sqlite3
220
+ from ctransformers import AutoModelForCausalLM
221
+
222
+ # =========================
223
+ # MEMORY SYSTEM (LIGHT)
224
+ # =========================
225
+
226
+ conn = sqlite3.connect("memory.db", check_same_thread=False)
227
+ cursor = conn.cursor()
228
+
229
+ cursor.execute("""
230
+ CREATE TABLE IF NOT EXISTS memory (
231
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
232
+ role TEXT,
233
+ content TEXT
234
+ )
235
+ """)
236
+ conn.commit()
237
+
238
+
239
+ def save(role, content):
240
+ cursor.execute(
241
+ "INSERT INTO memory (role, content) VALUES (?, ?)",
242
+ (role, content)
243
+ )
244
+ conn.commit()
245
+
246
+
247
+ def load(limit=2): # VERY IMPORTANT FOR RAM
248
+ cursor.execute("""
249
+ SELECT role, content FROM memory
250
+ ORDER BY id DESC LIMIT ?
251
+ """, (limit,))
252
+ rows = cursor.fetchall()
253
+ rows.reverse()
254
+ return rows
255
+
256
+
257
+ # =========================
258
+ # LOAD GGUF MODEL
259
+ # =========================
260
+
261
+ model = AutoModelForCausalLM.from_pretrained(
262
+ "TheBloke/Qwen2.5-Coder-1.5B-Instruct-GGUF",
263
+ model_file="q4_k_m.gguf", # IMPORTANT: quantized file
264
+ model_type="qwen",
265
+ gpu_layers=0
266
+ )
267
+
268
+ # =========================
269
+ # SYSTEM PROMPT
270
+ # =========================
271
+
272
+ SYSTEM_PROMPT = """
273
+ You are Symbolic Alpha Bug Bounty AI Agent.
274
+
275
+ Rules:
276
+ - Be accurate and technical
277
+ - Do not invent fake exploits or tools
278
+ - Focus on cybersecurity education
279
+ - Keep answers concise
280
+ """
281
+
282
+
283
+ # =========================
284
+ # PROMPT BUILDER
285
+ # =========================
286
+
287
+ def build_prompt(user_input):
288
+
289
+ prompt = SYSTEM_PROMPT + "\n\n"
290
+
291
+ memory = load()
292
+
293
+ for role, content in memory:
294
+ prompt += f"{role}: {content}\n"
295
+
296
+ prompt += f"user: {user_input}\nassistant:"
297
+
298
+ return prompt
299
+
300
+
301
+ # =========================
302
+ # SIMPLE SECURITY SCANNER TOOL
303
+ # =========================
304
+
305
+ def scanner(text):
306
+
307
+ findings = []
308
+ t = text.lower()
309
+
310
+ if "'" in t and "select" in t:
311
+ findings.append("SQL Injection pattern detected")
312
+
313
+ if "script" in t or "innerhtml" in t:
314
+ findings.append("XSS risk detected")
315
+
316
+ if "eval(" in t:
317
+ findings.append("RCE risk detected")
318
+
319
+ if not findings:
320
+ findings.append("No obvious issues found")
321
+
322
+ return findings
323
+
324
+
325
+ # =========================
326
+ # CHAT ENGINE
327
+ # =========================
328
+
329
+ def chat(message, history):
330
+
331
+ try:
332
+ save("user", message)
333
+
334
+ # TOOL MODE
335
+ if message.startswith("scan:"):
336
+ target = message.replace("scan:", "")
337
+ result = scanner(target)
338
+
339
+ output = "BUG BOUNTY SCAN RESULTS:\n"
340
+ for r in result:
341
+ output += "- " + r + "\n"
342
+
343
+ save("assistant", output)
344
+ return output
345
+
346
+ # NORMAL MODE
347
+ prompt = build_prompt(message)
348
+
349
+ response = model(
350
+ prompt,
351
+ max_new_tokens=80, # VERY IMPORTANT FOR CPU
352
+ temperature=0.2,
353
+ stop=["user:"]
354
+ )
355
+
356
+ save("assistant", response)
357
+ return response
358
+
359
+ except Exception as e:
360
+ return f"Error: {str(e)}"
361
+
362
+
363
  # =========================
364
+ # UI
365
+ # =========================
366
+
367
+ demo = gr.ChatInterface(
368
+ fn=chat,
369
+ title="Symbolic Alpha GGUF Bug Bounty Agent",
370
+ description="Lightweight CPU-optimized AI security assistant"
371
+ )
372
+
373
+ demo.launch()========
374
 
375
  demo = gr.ChatInterface(
376
  fn=chat,