saifeddinemk commited on
Commit
f620305
1 Parent(s): 4919f63

Fixed app v2

Browse files
Files changed (2) hide show
  1. app.py +43 -46
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,51 +1,48 @@
1
- import openai
 
2
 
3
- # Set up OpenAI API key
4
- openai.api_key = "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"
5
 
6
- # Function to read and process log files
7
- def read_log_file(file_path):
8
- with open(file_path, 'r') as file:
9
- log_data = file.read()
10
- return log_data
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
- # Extract response text
31
- analysis = response.choices[0].text.strip()
32
- return analysis
 
 
 
 
 
 
33
 
34
- # Main function to execute log analysis
35
- def main():
36
- # Path to your network log file
37
- log_file_path = "log.txt"
38
-
39
- # Read log data
40
- log_data = read_log_file(log_file_path)
41
-
42
- # Analyze log data
43
- analysis = analyze_logs_for_malicious_activity(log_data)
44
-
45
- # Print or save analysis result
46
- print("Analysis of Network Logs for Malicious Activity:\n")
47
- print(analysis)
 
 
 
 
 
 
 
 
 
48
 
49
- # Run the main function
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