Spaces:
Sleeping
Sleeping
saifeddinemk
commited on
Commit
•
f620305
1
Parent(s):
4919f63
Fixed app v2
Browse files- app.py +43 -46
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,51 +1,48 @@
|
|
1 |
-
import
|
|
|
2 |
|
3 |
-
#
|
4 |
-
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
# Function to analyze log data for malicious activity using OpenAI
|
13 |
-
def analyze_logs_for_malicious_activity(log_data):
|
14 |
-
# Instruction prompt to guide the model
|
15 |
-
prompt = (
|
16 |
-
"Analyze the following network log data for any indicators of malicious activity, "
|
17 |
-
"such as unusual IP addresses, unauthorized access attempts, data exfiltration, or anomalies. "
|
18 |
-
"Provide details on potential threats, IPs involved, and suggest actions if any threats are detected.\n\n"
|
19 |
-
f"{log_data}"
|
20 |
-
)
|
21 |
-
|
22 |
-
# Send request to OpenAI API
|
23 |
-
response = openai.Completion.create(
|
24 |
-
engine="gpt-3.5-turbo", # Ensure to use a suitable model for instructions
|
25 |
-
prompt=prompt,
|
26 |
-
max_tokens=500,
|
27 |
-
temperature=0.5
|
28 |
)
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
#
|
50 |
-
if __name__ == "__main__":
|
51 |
-
main()
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, UploadFile, File
|
2 |
+
from llama_cpp import Llama
|
3 |
|
4 |
+
# Initialize FastAPI app
|
5 |
+
app = FastAPI()
|
6 |
|
7 |
+
# Load the Llama model
|
8 |
+
try:
|
9 |
+
llm = Llama.from_pretrained(
|
10 |
+
repo_id="QuantFactory/Lily-Cybersecurity-7B-v0.2-GGUF",
|
11 |
+
filename="Lily-Cybersecurity-7B-v0.2.Q3_K_S.gguf",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
)
|
13 |
+
except Exception as e:
|
14 |
+
raise RuntimeError(f"Failed to load model: {e}")
|
15 |
+
|
16 |
+
# Define the route for security log analysis with file upload
|
17 |
+
@app.post("/analyze_security_logs")
|
18 |
+
async def analyze_security_logs(file: UploadFile = File(...)):
|
19 |
+
try:
|
20 |
+
# Read the content of the uploaded file
|
21 |
+
log_data = await file.read()
|
22 |
+
log_data = log_data.decode("utf-8")
|
23 |
|
24 |
+
# Security-focused prompt
|
25 |
+
prompt = (
|
26 |
+
"Analyze the following network log data for any indicators of malicious activity, "
|
27 |
+
"such as unusual IP addresses, unauthorized access attempts, data exfiltration, or anomalies. "
|
28 |
+
"Provide details on potential threats, IPs involved, and suggest actions if any threats are detected.\n\n"
|
29 |
+
f"{log_data}"
|
30 |
+
)
|
31 |
+
|
32 |
+
# Generate response from the model
|
33 |
+
response = llm.create_chat_completion(
|
34 |
+
messages=[
|
35 |
+
{
|
36 |
+
"role": "user",
|
37 |
+
"content": prompt
|
38 |
+
}
|
39 |
+
]
|
40 |
+
)
|
41 |
+
|
42 |
+
# Extract and return the analysis text
|
43 |
+
analysis_text = response["choices"][0]["message"]["content"]
|
44 |
+
return {"analysis": analysis_text}
|
45 |
+
except Exception as e:
|
46 |
+
raise HTTPException(status_code=500, detail=str(e))
|
47 |
|
48 |
+
# To run the app, use: uvicorn app:app --reload
|
|
|
|
requirements.txt
CHANGED
@@ -7,4 +7,5 @@ torchvision
|
|
7 |
pydantic
|
8 |
sentencepiece
|
9 |
accelerate>=0.26.0
|
10 |
-
gradio
|
|
|
|
7 |
pydantic
|
8 |
sentencepiece
|
9 |
accelerate>=0.26.0
|
10 |
+
gradio
|
11 |
+
git+https://github.com/abetlen/llama-cpp-python.git
|