rahimizadeh commited on
Commit
4c2540e
·
verified ·
1 Parent(s): 7795241

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -59
app.py CHANGED
@@ -1,59 +1,65 @@
1
- import gradio as gr
2
- from modules import analysis # Correct import
3
-
4
- # Create the Gradio Blocks UI
5
- with gr.Blocks(title="Log Intelligence Assistant") as demo:
6
- gr.Markdown("## 🔍 Interactive Network Log Assistant")
7
-
8
- with gr.Tab("Inputs 🔽"):
9
- with gr.Row():
10
- uploaded_files = gr.File(
11
- file_types=[".pdf", ".log", ".txt"],
12
- file_count="multiple",
13
- label="📁 Upload Log Files"
14
- )
15
- text_input = gr.Textbox(
16
- label="Paste Logs Here",
17
- lines=15,
18
- placeholder="2023-10-05 14:22:11 [Firewall] BLOCKED: 192.168.1.5..."
19
- )
20
-
21
- with gr.Row():
22
- query_input = gr.Textbox(
23
- label="Ask About Logs",
24
- placeholder="Find failed login attempts from 192.168.1.5"
25
- )
26
- quick_query = gr.Dropdown(
27
- choices=["", "Detect anomalies", "Show blocked IPs", "Generate summary"],
28
- label="Quick Actions"
29
- )
30
-
31
- with gr.Accordion("⚙️ Configuration Controls", open=False):
32
- temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="Creativity (Temperature)")
33
- start_time = gr.DateTime(label="Start Time")
34
- end_time = gr.DateTime(label="End Time")
35
-
36
- with gr.Tab("Outputs 🔼"):
37
- analysis_result = gr.Textbox(label="Analysis Results", lines=10, interactive=False)
38
- bar_plot = gr.Plot(label="Event Frequency by Hour")
39
- pie_chart = gr.Plot(label="Event Type Distribution")
40
- alert_feed = gr.HighlightedText(label="Security Alerts", color_map={
41
- "CRITICAL": "red",
42
- "WARNING": "orange",
43
- "INFO": "blue"
44
- })
45
-
46
- with gr.Tab("Export 📤"):
47
- export_csv = gr.Button("Save as CSV")
48
- export_pdf = gr.Button("Export PDF Report")
49
-
50
- run_button = gr.Button("🔎 Analyze Logs")
51
-
52
- # ✅ Fixed the typo here: analysis not analys
53
- run_button.click(
54
- fn=analysis.run_analysis,
55
- inputs=[uploaded_files, text_input, query_input, quick_query, temperature, start_time, end_time],
56
- outputs=[analysis_result, bar_plot, pie_chart, alert_feed]
57
- )
58
-
59
- demo.launch()
 
 
 
 
 
 
 
1
+ from langchain.chains import RetrievalQA
2
+ from langchain_community.llms import HuggingFacePipeline
3
+ from transformers import pipeline
4
+ from modules import parser, vectorizer
5
+ from datetime import datetime
6
+ import re
7
+
8
+ def filter_logs_by_time(logs_text, start_time, end_time):
9
+ """
10
+ Filters log lines based on timestamp range.
11
+ """
12
+ if not start_time or not end_time:
13
+ return logs_text # Skip filtering if not both are set
14
+
15
+ start = datetime.fromisoformat(str(start_time))
16
+ end = datetime.fromisoformat(str(end_time))
17
+
18
+ filtered_lines = []
19
+ for line in logs_text.splitlines():
20
+ match = re.match(r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})", line)
21
+ if match:
22
+ timestamp = datetime.strptime(match.group(1), "%Y-%m-%d %H:%M:%S")
23
+ if start <= timestamp <= end:
24
+ filtered_lines.append(line)
25
+ return "\n".join(filtered_lines)
26
+
27
+ def run_analysis(uploaded_files, text_input, query, quick_action, temperature, start_time, end_time):
28
+ logs_text = ""
29
+
30
+ # Combine uploaded + pasted logs
31
+ if uploaded_files:
32
+ logs_text += parser.parse_uploaded_files(uploaded_files)
33
+ if text_input:
34
+ logs_text += "\n" + text_input
35
+
36
+ if not logs_text.strip():
37
+ return "❌ No logs provided.", None, None, None
38
+
39
+ # Filter logs based on time range (if provided)
40
+ logs_text = filter_logs_by_time(logs_text, start_time, end_time)
41
+
42
+ # Use either typed query or quick action
43
+ query_text = query.strip() if query else ""
44
+ if not query_text and quick_action:
45
+ query_text = quick_action
46
+ if not query_text:
47
+ return "❌ No query or quick action selected.", None, None, None
48
+
49
+ # Process logs
50
+ docs = vectorizer.prepare_documents(logs_text)
51
+ vectordb = vectorizer.create_vectorstore(docs)
52
+
53
+ pipe = pipeline("text-generation", model="gpt2", max_length=512, temperature=temperature)
54
+ llm = HuggingFacePipeline(pipeline=pipe)
55
+
56
+ qa = RetrievalQA.from_chain_type(llm=llm, retriever=vectordb.as_retriever())
57
+ result = qa.run(query_text)
58
+
59
+ # Dummy charts and alerts for testing
60
+ bar_data = {"Hour": ["14:00", "15:00"], "Count": [8, 4]}
61
+ pie_data = {"Event Type": ["Blocked", "Scan"], "Count": [8, 4]}
62
+ alerts = [("CRITICAL", "8 blocked SSH attempts from 192.168.1.5"),
63
+ ("WARNING", "4 port scanning alerts from 10.0.0.8")]
64
+
65
+ return result, bar_data, pie_data, alerts