Spaces:
Build error
Build error
Commit
·
2620283
1
Parent(s):
e71fade
Fixed app v2
Browse files
app.py
CHANGED
|
@@ -1,16 +1,22 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
from
|
|
|
|
| 4 |
import uvicorn
|
| 5 |
|
| 6 |
# Initialize FastAPI app
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Define request model for log data
|
| 16 |
class LogRequest(BaseModel):
|
|
@@ -23,6 +29,7 @@ class AnalysisResponse(BaseModel):
|
|
| 23 |
# Define the route for security log analysis
|
| 24 |
@app.post("/analyze_security_logs", response_model=AnalysisResponse)
|
| 25 |
async def analyze_security_logs(request: LogRequest):
|
|
|
|
| 26 |
try:
|
| 27 |
# Security-focused prompt
|
| 28 |
prompt = (
|
|
@@ -32,11 +39,18 @@ async def analyze_security_logs(request: LogRequest):
|
|
| 32 |
f"{request.log_data}"
|
| 33 |
)
|
| 34 |
|
| 35 |
-
# Generate response from the
|
| 36 |
-
response =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Extract and return the analysis text
|
| 39 |
-
analysis_text = response[0]["
|
| 40 |
return AnalysisResponse(analysis=analysis_text)
|
| 41 |
except Exception as e:
|
| 42 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
from functools import lru_cache
|
| 5 |
import uvicorn
|
| 6 |
|
| 7 |
# Initialize FastAPI app
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# Lazy load the Llama model
|
| 11 |
+
@lru_cache(maxsize=1)
|
| 12 |
+
def load_model():
|
| 13 |
+
try:
|
| 14 |
+
return Llama.from_pretrained(
|
| 15 |
+
repo_id="prithivMLmods/Llama-3.2-1B-GGUF",
|
| 16 |
+
filename="Llama-3.2-1B.F16.gguf"
|
| 17 |
+
)
|
| 18 |
+
except Exception as e:
|
| 19 |
+
raise RuntimeError(f"Failed to load model: {e}")
|
| 20 |
|
| 21 |
# Define request model for log data
|
| 22 |
class LogRequest(BaseModel):
|
|
|
|
| 29 |
# Define the route for security log analysis
|
| 30 |
@app.post("/analyze_security_logs", response_model=AnalysisResponse)
|
| 31 |
async def analyze_security_logs(request: LogRequest):
|
| 32 |
+
llm = load_model()
|
| 33 |
try:
|
| 34 |
# Security-focused prompt
|
| 35 |
prompt = (
|
|
|
|
| 39 |
f"{request.log_data}"
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Generate response from the Llama model
|
| 43 |
+
response = llm.create_chat_completion(
|
| 44 |
+
messages=[
|
| 45 |
+
{
|
| 46 |
+
"role": "user",
|
| 47 |
+
"content": prompt
|
| 48 |
+
}
|
| 49 |
+
]
|
| 50 |
+
)
|
| 51 |
|
| 52 |
# Extract and return the analysis text
|
| 53 |
+
analysis_text = response["choices"][0]["message"]["content"]
|
| 54 |
return AnalysisResponse(analysis=analysis_text)
|
| 55 |
except Exception as e:
|
| 56 |
raise HTTPException(status_code=500, detail=str(e))
|