Rithvickkr
commited on
Commit
·
5e77094
1
Parent(s):
c19c237
DSATP, LlamaIndex, and Mistral-7B Modal API integration
Browse files- app.py +58 -44
- requirements.txt +4 -1
app.py
CHANGED
@@ -1,50 +1,21 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
|
4 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
5 |
import os
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# Configure LlamaIndex to use local embeddings
|
8 |
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
9 |
|
10 |
-
# DSATP log parsing (embedded for Spaces)
|
11 |
-
def dsatp_parse_log(text: str) -> dict:
|
12 |
-
"""Parse log for IoT threats."""
|
13 |
-
log = text.lower()
|
14 |
-
threats = {
|
15 |
-
"compromised": {"classification": "Threat Detected", "severity": "Critical", "mitigation": "Isolate process, run port scan"},
|
16 |
-
"unauthorized": {"classification": "Threat Detected", "severity": "High", "mitigation": "Quarantine MAC address"},
|
17 |
-
"high cpu": {"classification": "Threat Detected", "severity": "Medium", "mitigation": "Check for crypto-miner or DoS"},
|
18 |
-
"inbound traffic": {"classification": "Threat Detected", "severity": "Medium", "mitigation": "Block closed ports"},
|
19 |
-
"firmware mismatch": {"classification": "Threat Detected", "severity": "High", "mitigation": "Validate OTA or rollback"}
|
20 |
-
}
|
21 |
-
for key, value in threats.items():
|
22 |
-
if key in log:
|
23 |
-
return {**value, "confidence": 0.9}
|
24 |
-
return {"classification": "No Threat", "severity": "Safe", "mitigation": "None", "confidence": 0.5}
|
25 |
-
|
26 |
-
# DSATP YARA scanning (embedded for Spaces)
|
27 |
-
def dsatp_yara_scan(file_path: str) -> dict:
|
28 |
-
"""Scan file with YARA rules."""
|
29 |
-
try:
|
30 |
-
rules = yara.compile(filepath="dsatp/rules.yar")
|
31 |
-
matches = rules.match(file_path)
|
32 |
-
if matches:
|
33 |
-
return {
|
34 |
-
"classification": "Malware Detected",
|
35 |
-
"severity": "Critical",
|
36 |
-
"mitigation": "Quarantine file, run antivirus",
|
37 |
-
"confidence": 0.95
|
38 |
-
}
|
39 |
-
return {
|
40 |
-
"classification": "No Malware",
|
41 |
-
"severity": "Safe",
|
42 |
-
"mitigation": "None",
|
43 |
-
"confidence": 0.7
|
44 |
-
}
|
45 |
-
except Exception as e:
|
46 |
-
return {"error": str(e), "severity": "Unknown", "mitigation": "Check file format"}
|
47 |
-
|
48 |
# Initialize LlamaIndex with real corpus
|
49 |
def init_llama_index():
|
50 |
"""Load CVE/IoT corpus into LlamaIndex."""
|
@@ -56,11 +27,42 @@ def init_llama_index():
|
|
56 |
return None
|
57 |
|
58 |
index = init_llama_index()
|
59 |
-
query_engine = index.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
# Chatbot function
|
62 |
def chatbot_response(user_input, file, history):
|
63 |
-
"""Process input or file with DSATP and
|
64 |
if history is None:
|
65 |
history = []
|
66 |
input_text = user_input
|
@@ -76,12 +78,24 @@ def chatbot_response(user_input, file, history):
|
|
76 |
context_str = "No context available."
|
77 |
if query_engine:
|
78 |
try:
|
79 |
-
|
80 |
-
context_str =
|
81 |
except Exception as e:
|
82 |
context_str = f"Context error: {e}"
|
83 |
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
updated_history = history + [(user_input or "File uploaded", response)]
|
86 |
return updated_history, scan_result
|
87 |
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
|
4 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
5 |
import os
|
6 |
|
7 |
+
# Suppress Hugging Face symlink warning
|
8 |
+
os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] = "1"
|
9 |
+
|
10 |
+
# DSATP API endpoints
|
11 |
+
DSATP_API = "http://localhost:8000"
|
12 |
+
|
13 |
+
# Modal Mistral-7B API endpoint (replace with your Modal URL)
|
14 |
+
MODAL_API = "https://rithvickkumar27--mistral-7b-api-analyze.modal.run"
|
15 |
+
|
16 |
# Configure LlamaIndex to use local embeddings
|
17 |
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# Initialize LlamaIndex with real corpus
|
20 |
def init_llama_index():
|
21 |
"""Load CVE/IoT corpus into LlamaIndex."""
|
|
|
27 |
return None
|
28 |
|
29 |
index = init_llama_index()
|
30 |
+
query_engine = index.as_retriever() if index else None
|
31 |
+
|
32 |
+
# Call Modal Mistral-7B API
|
33 |
+
def call_mistral_llm(prompt):
|
34 |
+
"""Call Mistral-7B via Modal API."""
|
35 |
+
try:
|
36 |
+
response = requests.post(MODAL_API, json={"prompt": prompt}, timeout=30)
|
37 |
+
if response.status_code == 200:
|
38 |
+
print(f"LLM response: {response.json()}")
|
39 |
+
return response.json().get("response", "LLM error")
|
40 |
+
else:
|
41 |
+
return f"Modal API error: {response.status_code}"
|
42 |
+
except Exception as e:
|
43 |
+
return f"Modal error: {e}"
|
44 |
+
|
45 |
+
# Call DSATP log parsing
|
46 |
+
def dsatp_parse_log(text: str) -> dict:
|
47 |
+
"""Call DSATP log parsing API."""
|
48 |
+
try:
|
49 |
+
response = requests.post(f"{DSATP_API}/parse_log", json={"log": text})
|
50 |
+
return response.json() if response.status_code == 200 else {"error": "API error"}
|
51 |
+
except Exception as e:
|
52 |
+
return {"error": str(e), "severity": "Unknown", "mitigation": "Check API"}
|
53 |
+
|
54 |
+
# Call DSATP YARA scanning
|
55 |
+
def dsatp_yara_scan(file_path: str) -> dict:
|
56 |
+
"""Call DSATP YARA scanning API."""
|
57 |
+
try:
|
58 |
+
response = requests.post(f"{DSATP_API}/scan_file", json={"file_path": file_path})
|
59 |
+
return response.json() if response.status_code == 200 else {"error": "API error"}
|
60 |
+
except Exception as e:
|
61 |
+
return {"error": str(e), "severity": "Unknown", "mitigation": "Check API"}
|
62 |
|
63 |
# Chatbot function
|
64 |
def chatbot_response(user_input, file, history):
|
65 |
+
"""Process input or file with DSATP, LlamaIndex, and Mistral-7B."""
|
66 |
if history is None:
|
67 |
history = []
|
68 |
input_text = user_input
|
|
|
78 |
context_str = "No context available."
|
79 |
if query_engine:
|
80 |
try:
|
81 |
+
results = query_engine.retrieve(f"Mitigation for: {input_text}")
|
82 |
+
context_str = " ".join([res.text for res in results[:2]])
|
83 |
except Exception as e:
|
84 |
context_str = f"Context error: {e}"
|
85 |
|
86 |
+
# Generate response with Mistral-7B via Modal
|
87 |
+
prompt = f"""
|
88 |
+
You are a Security Analyst. Based on:
|
89 |
+
Classification: {scan_result['classification']}
|
90 |
+
Severity: {scan_result['severity']}
|
91 |
+
Mitigation: {scan_result['mitigation']}
|
92 |
+
Confidence: {scan_result['confidence']}
|
93 |
+
Context: {context_str}
|
94 |
+
Provide a concise response to the user.
|
95 |
+
"""
|
96 |
+
llm_response = call_mistral_llm(prompt)
|
97 |
+
response = f"Security Analyst: {llm_response}. Context: {context_str}"
|
98 |
+
|
99 |
updated_history = history + [(user_input or "File uploaded", response)]
|
100 |
return updated_history, scan_result
|
101 |
|
requirements.txt
CHANGED
@@ -5,5 +5,8 @@ uvicorn
|
|
5 |
yara-python
|
6 |
requests
|
7 |
llama-index-core
|
|
|
8 |
pandas
|
9 |
-
sentence-transformers
|
|
|
|
|
|
5 |
yara-python
|
6 |
requests
|
7 |
llama-index-core
|
8 |
+
llama-index-embeddings-huggingface
|
9 |
pandas
|
10 |
+
sentence-transformers
|
11 |
+
transformers
|
12 |
+
torch
|