Delete inference
Browse files- inference/__pycache__/analyze.cpython-312.pyc +0 -0
- inference/analyze.py +0 -59
- inference/test.py +0 -51
inference/__pycache__/analyze.cpython-312.pyc
DELETED
|
Binary file (2.07 kB)
|
|
|
inference/analyze.py
DELETED
|
@@ -1,59 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
-
from rag.search import search_context
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
| 7 |
-
|
| 8 |
-
MODEL_PATH = os.path.join(BASE_DIR, "model", "final")
|
| 9 |
-
|
| 10 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 11 |
-
MODEL_PATH,
|
| 12 |
-
local_files_only=True,
|
| 13 |
-
trust_remote_code=True,
|
| 14 |
-
fix_mistral_regex=True
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
-
MODEL_PATH,
|
| 19 |
-
local_files_only=True,
|
| 20 |
-
trust_remote_code=True,
|
| 21 |
-
device_map="auto",
|
| 22 |
-
dtype=torch.float16
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def analyze(user_input: str):
|
| 27 |
-
context = search_context(user_input)
|
| 28 |
-
|
| 29 |
-
prompt = f"""
|
| 30 |
-
You are a cybersecurity malware analysis assistant.
|
| 31 |
-
Respond ONLY in valid JSON.
|
| 32 |
-
Use these fields exactly once:
|
| 33 |
-
- reasoning (array of strings)
|
| 34 |
-
- indicators (array)
|
| 35 |
-
- confidence (float 0-1)
|
| 36 |
-
- recommendation (string)
|
| 37 |
-
- mitre_attack (array)
|
| 38 |
-
|
| 39 |
-
Context:
|
| 40 |
-
{context}
|
| 41 |
-
|
| 42 |
-
Input:
|
| 43 |
-
{user_input}
|
| 44 |
-
|
| 45 |
-
Response:
|
| 46 |
-
"""
|
| 47 |
-
|
| 48 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 49 |
-
|
| 50 |
-
with torch.no_grad():
|
| 51 |
-
output = model.generate(
|
| 52 |
-
**inputs,
|
| 53 |
-
max_new_tokens=256,
|
| 54 |
-
do_sample=True,
|
| 55 |
-
temperature=0.2,
|
| 56 |
-
top_p=0.9
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
return tokenizer.decode(output[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inference/test.py
DELETED
|
@@ -1,51 +0,0 @@
|
|
| 1 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
-
import torch
|
| 3 |
-
import os
|
| 4 |
-
|
| 5 |
-
MODEL_PATH = os.path.abspath(
|
| 6 |
-
r"C:\Users\USER\OneDrive\Desktop\work\mcma\micro-cyber-llm\model\final"
|
| 7 |
-
)
|
| 8 |
-
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 10 |
-
MODEL_PATH,
|
| 11 |
-
local_files_only=True,
|
| 12 |
-
fix_mistral_regex=True
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
-
MODEL_PATH,
|
| 17 |
-
device_map="auto",
|
| 18 |
-
dtype=torch.float16,
|
| 19 |
-
local_files_only=True
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
prompt = """### Instruction:
|
| 23 |
-
You are a cybersecurity malware analysis assistant.
|
| 24 |
-
Respond ONLY in valid JSON.
|
| 25 |
-
Use these fields exactly once:
|
| 26 |
-
- reasoning (array of strings)
|
| 27 |
-
- indicators (array)
|
| 28 |
-
- confidence (float 0-1)
|
| 29 |
-
- recommendation (string)
|
| 30 |
-
- mitre_attack (array)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
### Input:
|
| 34 |
-
APK requests READ_SMS and communicates with api.telegram.org
|
| 35 |
-
|
| 36 |
-
### Response:
|
| 37 |
-
"""
|
| 38 |
-
|
| 39 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 40 |
-
|
| 41 |
-
with torch.no_grad():
|
| 42 |
-
output = model.generate(
|
| 43 |
-
**inputs,
|
| 44 |
-
max_new_tokens=256,
|
| 45 |
-
do_sample=True,
|
| 46 |
-
temperature=0.2,
|
| 47 |
-
top_p=0.9
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
print(tokenizer.decode(output[0], skip_special_tokens=True))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|