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

Fixed app v2

Browse files
Files changed (1) hide show
  1. app.py +51 -39
app.py CHANGED
@@ -1,39 +1,51 @@
1
- from fastapi import FastAPI, HTTPException
2
- from pydantic import BaseModel
3
- from transformers import pipeline
4
- from typing import List
5
-
6
- # Initialize the FastAPI app
7
- app = FastAPI()
8
-
9
- # Initialize the text generation pipeline with the specified model
10
- pipe = pipeline("text-generation", model="jcordon5/Mistral-7B-cybersecurity-rules")
11
-
12
- # Define the input schema for FastAPI
13
- class Message(BaseModel):
14
- role: str
15
- content: str
16
-
17
- class MessagesInput(BaseModel):
18
- messages: List[Message]
19
-
20
- @app.post("/generate/")
21
- async def generate_response(messages_input: MessagesInput):
22
- try:
23
- # Convert the messages to the expected format for the pipeline
24
- messages = [{"role": msg.role, "content": msg.content} for msg in messages_input.messages]
25
-
26
- # Generate response using the pipeline
27
- response = pipe(messages)
28
-
29
- # Extract generated text from the response
30
- generated_text = response[0]["generated_text"]
31
-
32
- return {
33
- "response": generated_text
34
- }
35
- except Exception as e:
36
- raise HTTPException(status_code=500, detail=str(e))
37
-
38
- # Run the app
39
- # To start the server, use the command: uvicorn filename:app --host 0.0.0.0 --port 8000
 
 
 
 
 
 
 
 
 
 
 
 
 
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()