rahimizadeh commited on
Commit
ef14d9b
·
verified ·
1 Parent(s): f9649c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -55
app.py CHANGED
@@ -1,67 +1,67 @@
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
66
 
 
67
  demo.launch()
 
1
+ import gradio as gr
2
+ from modules import analysis # Make sure this matches your folder structure
 
 
 
 
3
 
4
+ # Create the Gradio interface using Blocks
5
+ with gr.Blocks(title="Log Intelligence Assistant") as demo:
6
+ gr.Markdown("## 🔍 Interactive Network Log Assistant")
 
 
 
7
 
8
+ # ---------- Inputs ----------
9
+ with gr.Tab("Inputs 🔽"):
10
+ with gr.Row():
11
+ uploaded_files = gr.File(
12
+ file_types=[".pdf", ".log", ".txt"],
13
+ file_count="multiple",
14
+ label="📁 Upload Log Files"
15
+ )
16
+ text_input = gr.Textbox(
17
+ label="Paste Logs Here",
18
+ lines=15,
19
+ placeholder="2023-10-05 14:22:11 [Firewall] BLOCKED: 192.168.1.5..."
20
+ )
21
 
22
+ with gr.Row():
23
+ query_input = gr.Textbox(
24
+ label="Ask About Logs",
25
+ placeholder="Find failed login attempts from 192.168.1.5"
26
+ )
27
+ quick_query = gr.Dropdown(
28
+ choices=["", "Detect anomalies", "Show blocked IPs", "Generate summary"],
29
+ label="Quick Actions"
30
+ )
31
 
32
+ with gr.Accordion("⚙️ Configuration Controls", open=False):
33
+ temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="Creativity (Temperature)")
34
+ start_time = gr.DateTime(label="Start Time")
35
+ end_time = gr.DateTime(label="End Time")
36
 
37
+ # ---------- Outputs ----------
38
+ with gr.Tab("Outputs 🔼"):
39
+ analysis_result = gr.Textbox(label="Analysis Results", lines=10, interactive=False)
 
 
40
 
41
+ # Use gr.Plot for matplotlib/plotly compatibility
42
+ bar_plot = gr.Plot(label="Event Frequency by Hour")
43
+ pie_chart = gr.Plot(label="Event Type Distribution")
44
 
45
+ alert_feed = gr.HighlightedText(label="Security Alerts", color_map={
46
+ "CRITICAL": "red",
47
+ "WARNING": "orange",
48
+ "INFO": "blue"
49
+ })
50
 
51
+ # ---------- Export ----------
52
+ with gr.Tab("Export 📤"):
53
+ export_csv = gr.Button("Save as CSV")
54
+ export_pdf = gr.Button("Export PDF Report")
 
 
55
 
56
+ # ---------- Action Button ----------
57
+ run_button = gr.Button("🔎 Analyze Logs")
 
58
 
59
+ # Bind the Analyze button to your backend function
60
+ run_button.click(
61
+ fn=analysis.run_analysis,
62
+ inputs=[uploaded_files, text_input, query_input, quick_query, temperature, start_time, end_time],
63
+ outputs=[analysis_result, bar_plot, pie_chart, alert_feed]
64
+ )
 
 
 
 
 
 
 
65
 
66
+ # ✅ Make sure this line exists — it launches the app!
67
  demo.launch()