Spaces:
Sleeping
Sleeping
File size: 1,940 Bytes
66279bf 868dd10 2620283 efadfea 4919f63 f620305 4919f63 2620283 9b9a132 e71fade 66279bf e71fade 868dd10 4919f63 66279bf e71fade 2620283 868dd10 e71fade 2620283 e71fade 2620283 e71fade f620305 4919f63 efadfea |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from llama_cpp import Llama
from functools import lru_cache
import uvicorn
# Initialize FastAPI app
app = FastAPI()
# Lazy load the Llama model
@lru_cache(maxsize=1)
def load_model():
try:
return Llama.from_pretrained(
repo_id="prithivMLmods/Llama-3.2-1B-GGUF",
filename="Llama-3.2-1B.F16.gguf"
)
except Exception as e:
raise RuntimeError(f"Failed to load model: {e}")
# Define request model for log data
class LogRequest(BaseModel):
log_data: str
# Define response model
class AnalysisResponse(BaseModel):
analysis: str
# Define the route for security log analysis
@app.post("/analyze_security_logs", response_model=AnalysisResponse)
async def analyze_security_logs(request: LogRequest):
llm = load_model()
try:
# Security-focused prompt
prompt = (
"Analyze the following network log data for any indicators of malicious activity, "
"such as unusual IP addresses, unauthorized access attempts, data exfiltration, or anomalies. "
"Provide details on potential threats, IPs involved, and suggest actions if any threats are detected.\n\n"
f"{request.log_data}"
)
# Generate response from the Llama model
response = llm.create_chat_completion(
messages=[
{
"role": "user",
"content": prompt
}
]
)
# Extract and return the analysis text
analysis_text = response["choices"][0]["message"]["content"]
return AnalysisResponse(analysis=analysis_text)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Run the FastAPI app using uvicorn
if __name__ == "__main__":
uvicorn.run("app:app", host="0.0.0.0", port=8000, workers=4, reload=True)
|