Spaces:
Sleeping
Sleeping
saifeddinemk
commited on
Commit
•
66279bf
1
Parent(s):
f9436bc
Fixed app v2
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
from llama_cpp import Llama
|
4 |
|
@@ -14,48 +14,39 @@ try:
|
|
14 |
except Exception as e:
|
15 |
raise RuntimeError(f"Failed to load model: {e}")
|
16 |
|
|
|
|
|
|
|
|
|
17 |
# Define response model
|
18 |
class AnalysisResponse(BaseModel):
|
19 |
analysis: str
|
20 |
|
21 |
-
# Define the route for security log analysis
|
22 |
@app.post("/analyze_security_logs", response_model=AnalysisResponse)
|
23 |
-
async def analyze_security_logs(
|
24 |
try:
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
)
|
43 |
-
|
44 |
-
# Generate response from the model
|
45 |
-
response = llm.create_chat_completion(
|
46 |
-
messages=[
|
47 |
-
{
|
48 |
-
"role": "user",
|
49 |
-
"content": prompt
|
50 |
-
}
|
51 |
-
]
|
52 |
-
)
|
53 |
-
|
54 |
-
# Extract and accumulate analysis text
|
55 |
-
analysis_text = response["choices"][0]["message"]["content"]
|
56 |
-
full_analysis += analysis_text + "\n"
|
57 |
|
58 |
-
return
|
|
|
|
|
59 |
except Exception as e:
|
60 |
raise HTTPException(status_code=500, detail=str(e))
|
61 |
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
from llama_cpp import Llama
|
4 |
|
|
|
14 |
except Exception as e:
|
15 |
raise RuntimeError(f"Failed to load model: {e}")
|
16 |
|
17 |
+
# Define request model for log data
|
18 |
+
class LogRequest(BaseModel):
|
19 |
+
log_data: str
|
20 |
+
|
21 |
# Define response model
|
22 |
class AnalysisResponse(BaseModel):
|
23 |
analysis: str
|
24 |
|
25 |
+
# Define the route for security log analysis
|
26 |
@app.post("/analyze_security_logs", response_model=AnalysisResponse)
|
27 |
+
async def analyze_security_logs(request: LogRequest):
|
28 |
try:
|
29 |
+
# Security-focused prompt
|
30 |
+
prompt = (
|
31 |
+
"Analyze the following network log data for any indicators of malicious activity, "
|
32 |
+
"such as unusual IP addresses, unauthorized access attempts, data exfiltration, or anomalies. "
|
33 |
+
"Provide details on potential threats, IPs involved, and suggest actions if any threats are detected.\n\n"
|
34 |
+
f"{request.log_data}"
|
35 |
+
)
|
36 |
|
37 |
+
# Generate response from the model
|
38 |
+
response = llm.create_chat_completion(
|
39 |
+
messages=[
|
40 |
+
{
|
41 |
+
"role": "user",
|
42 |
+
"content": prompt
|
43 |
+
}
|
44 |
+
]
|
45 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
# Extract and return the analysis text
|
48 |
+
analysis_text = response["choices"][0]["message"]["content"]
|
49 |
+
return AnalysisResponse(analysis=analysis_text)
|
50 |
except Exception as e:
|
51 |
raise HTTPException(status_code=500, detail=str(e))
|
52 |
|