ali3133 commited on
Commit
04cf251
·
verified ·
1 Parent(s): 573c70a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -29
app.py CHANGED
@@ -1,14 +1,7 @@
1
- """
2
- CyberSec Web Vulnerabilities Assistant
3
- Gradio app for Hugging Face Spaces - v1.2
4
- """
5
-
6
  import json
7
  import gradio as gr
8
 
9
- # ─────────────────────────────────────────
10
- # Load dataset
11
- # ─────────────────────────────────────────
12
  with open("dataset.json", "r", encoding="utf-8") as f:
13
  DATA = json.load(f)
14
 
@@ -57,7 +50,6 @@ ALIASES = {
57
  "tools": "Additional Tools",
58
  }
59
 
60
-
61
  def find_category(query: str):
62
  q = query.strip().lower()
63
  for alias, cat_name in ALIASES.items():
@@ -73,7 +65,6 @@ def find_category(query: str):
73
  + "\n".join(f"• {e['category']}" for e in DATA)
74
  )
75
 
76
-
77
  def wants_commands(query: str) -> bool:
78
  keywords = [
79
  "command", "commands", "أوامر", "امر", "أمر",
@@ -82,7 +73,6 @@ def wants_commands(query: str) -> bool:
82
  ]
83
  return any(k in query.lower() for k in keywords)
84
 
85
-
86
  def format_description_only(entry) -> str:
87
  return (
88
  f"## 🛡️ {entry['category']}\n\n"
@@ -91,7 +81,6 @@ def format_description_only(entry) -> str:
91
  f"💡 اكتب **`{entry['category']} commands`** لعرض الأوامر"
92
  )
93
 
94
-
95
  def format_with_commands(entry) -> str:
96
  lines = [
97
  f"## 🛡️ {entry['category']}\n\n{entry['description']}\n\n---\n\n## ⚙️ الأوامر\n"
@@ -101,26 +90,23 @@ def format_with_commands(entry) -> str:
101
  lines.append(f"```bash\n{cmd['command']}\n```\n")
102
  return "\n".join(lines)
103
 
104
-
105
- # tuples format - works with all Gradio versions
106
- def respond(query: str, history: list):
107
- if not query.strip():
108
  return history, ""
109
 
110
- entry, err = find_category(query)
111
  answer = err if err else (
112
- format_with_commands(entry) if wants_commands(query)
113
  else format_description_only(entry)
114
  )
115
 
 
116
  history = history or []
117
- history.append((query, answer))
 
118
  return history, ""
119
 
120
-
121
- # ─────────────────────────────────────────
122
- # UI
123
- # ─────────────────────────────────────────
124
  CATEGORIES_MD = "\n".join(f"- {e['category']}" for e in DATA)
125
 
126
  with gr.Blocks(title="CyberSec Assistant") as demo:
@@ -133,7 +119,7 @@ with gr.Blocks(title="CyberSec Assistant") as demo:
133
 
134
  with gr.Row():
135
  with gr.Column(scale=3):
136
- chatbot = gr.Chatbot(label="المحادثة", height=500, type="tuples")
137
  with gr.Row():
138
  txt = gr.Textbox(
139
  placeholder="اكتب اسم الثغرة مثل: XSS أو LFI أو SSRF ...",
@@ -154,11 +140,10 @@ with gr.Blocks(title="CyberSec Assistant") as demo:
154
  """
155
  )
156
 
157
- state = gr.State([])
158
-
159
- send_btn.click(respond, [txt, state], [chatbot, txt])
160
- txt.submit(respond, [txt, state], [chatbot, txt])
161
 
162
  gr.Markdown("---\n*تصنيف: هجومي | Web Vulnerabilities | v1.2*")
163
 
164
- demo.launch()
 
 
 
 
 
 
1
  import json
2
  import gradio as gr
3
 
4
+ # تحميل البيانات
 
 
5
  with open("dataset.json", "r", encoding="utf-8") as f:
6
  DATA = json.load(f)
7
 
 
50
  "tools": "Additional Tools",
51
  }
52
 
 
53
  def find_category(query: str):
54
  q = query.strip().lower()
55
  for alias, cat_name in ALIASES.items():
 
65
  + "\n".join(f"• {e['category']}" for e in DATA)
66
  )
67
 
 
68
  def wants_commands(query: str) -> bool:
69
  keywords = [
70
  "command", "commands", "أوامر", "امر", "أمر",
 
73
  ]
74
  return any(k in query.lower() for k in keywords)
75
 
 
76
  def format_description_only(entry) -> str:
77
  return (
78
  f"## 🛡️ {entry['category']}\n\n"
 
81
  f"💡 اكتب **`{entry['category']} commands`** لعرض الأوامر"
82
  )
83
 
 
84
  def format_with_commands(entry) -> str:
85
  lines = [
86
  f"## 🛡️ {entry['category']}\n\n{entry['description']}\n\n---\n\n## ⚙️ الأوامر\n"
 
90
  lines.append(f"```bash\n{cmd['command']}\n```\n")
91
  return "\n".join(lines)
92
 
93
+ def respond(message: str, history: list):
94
+ if not message.strip():
 
 
95
  return history, ""
96
 
97
+ entry, err = find_category(message)
98
  answer = err if err else (
99
+ format_with_commands(entry) if wants_commands(message)
100
  else format_description_only(entry)
101
  )
102
 
103
+ # بناء التاريخ بالصيغة الجديدة
104
  history = history or []
105
+ history.append({"role": "user", "content": message})
106
+ history.append({"role": "assistant", "content": answer})
107
  return history, ""
108
 
109
+ # واجهة Gradio
 
 
 
110
  CATEGORIES_MD = "\n".join(f"- {e['category']}" for e in DATA)
111
 
112
  with gr.Blocks(title="CyberSec Assistant") as demo:
 
119
 
120
  with gr.Row():
121
  with gr.Column(scale=3):
122
+ chatbot = gr.Chatbot(label="المحادثة", height=500)
123
  with gr.Row():
124
  txt = gr.Textbox(
125
  placeholder="اكتب اسم الثغرة مثل: XSS أو LFI أو SSRF ...",
 
140
  """
141
  )
142
 
143
+ # ربط الأحداث
144
+ send_btn.click(respond, [txt, chatbot], [chatbot, txt])
145
+ txt.submit(respond, [txt, chatbot], [chatbot, txt])
 
146
 
147
  gr.Markdown("---\n*تصنيف: هجومي | Web Vulnerabilities | v1.2*")
148
 
149
+ demo.launch()