File size: 14,557 Bytes
27f6252 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | """
Gradio UI for the CVE RAG System
"""
import gradio as gr
import asyncio
import aiohttp
import json
import logging
from typing import Tuple, List, Dict, Any
from datetime import datetime
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class RAGInterface:
"""Gradio interface for the RAG system"""
def __init__(self, api_url: str = "http://localhost:8000"):
self.api_url = api_url
self.session = None
async def _get_session(self):
"""Get or create aiohttp session"""
if self.session is None or self.session.closed:
self.session = aiohttp.ClientSession()
return self.session
async def query_api(self, query: str, use_large_model: bool, max_results: int) -> Tuple[str, List[Dict]]:
"""Query the RAG API"""
try:
session = await self._get_session()
payload = {
"query": query,
"top_k": max_results,
"max_context_docs": 5,
"use_large_model": use_large_model
}
async with session.post(f"{self.api_url}/api/v1/query", json=payload) as response:
if response.status == 200:
data = await response.json()
return data['response'], data['search_results']
else:
error_text = await response.text()
return f"Error: {response.status} - {error_text}", []
except Exception as e:
logger.error(f"Error querying API: {e}")
return f"Error: {str(e)}", []
async def search_api(self, query: str, max_results: int) -> List[Dict]:
"""Search the API without LLM generation"""
try:
session = await self._get_session()
payload = {
"query": query,
"top_k": max_results
}
async with session.post(f"{self.api_url}/api/v1/search", json=payload) as response:
if response.status == 200:
return await response.json()
else:
error_text = await response.text()
logger.error(f"Search API error: {response.status} - {error_text}")
return []
except Exception as e:
logger.error(f"Error in search API: {e}")
return []
async def summary_api(self, query: str, max_results: int) -> Dict:
"""Get summary from API"""
try:
session = await self._get_session()
payload = {
"query": query,
"max_results": max_results
}
async with session.post(f"{self.api_url}/api/v1/summary", json=payload) as response:
if response.status == 200:
return await response.json()
else:
error_text = await response.text()
logger.error(f"Summary API error: {response.status} - {error_text}")
return {"error": f"API Error: {response.status}"}
except Exception as e:
logger.error(f"Error in summary API: {e}")
return {"error": str(e)}
async def health_check(self) -> Dict:
"""Check API health"""
try:
session = await self._get_session()
async with session.get(f"{self.api_url}/api/v1/health") as response:
if response.status == 200:
return await response.json()
else:
return {"status": "unhealthy", "error": f"HTTP {response.status}"}
except Exception as e:
return {"status": "unhealthy", "error": str(e)}
def format_search_results(self, results: List[Dict]) -> str:
"""Format search results for display"""
if not results:
return "No results found."
formatted = []
for i, result in enumerate(results, 1):
metadata = result.get('metadata', {})
cve_id = metadata.get('cve_id', 'Unknown')
severity = metadata.get('severity', 'Unknown')
score = result.get('score', 0)
# Try all possible keys for products, vendors, cwe
products = metadata.get('products') or metadata.get('affected_products') or 'N/A'
vendors = metadata.get('vendors') or 'N/A'
cwe = metadata.get('cwe_refs') or metadata.get('weaknesses') or 'N/A'
formatted.append(f"**{i}. {cve_id}** ({severity}) - Score: {score:.3f}")
formatted.append(f" Products: {products}")
formatted.append(f" Vendors: {vendors}")
formatted.append(f" CWE: {cwe}")
formatted.append("")
return "\n".join(formatted)
def format_summary(self, summary: Dict) -> str:
"""Format summary for display"""
if "error" in summary:
return f"Error: {summary['error']}"
formatted = []
formatted.append(f"**Summary for: {summary['query']}**")
formatted.append(f"Total results: {summary['total_results']}")
formatted.append("")
# Severity distribution
formatted.append("**Severity Distribution:**")
for severity, count in summary['severity_distribution'].items():
formatted.append(f" {severity}: {count}")
formatted.append("")
# Top vendors
if summary['top_vendors']:
formatted.append("**Top Vendors:**")
for vendor in summary['top_vendors'][:5]:
formatted.append(f" • {vendor}")
formatted.append("")
# Top products
if summary['top_products']:
formatted.append("**Top Products:**")
for product in summary['top_products'][:5]:
formatted.append(f" • {product}")
formatted.append("")
# Common weaknesses
if summary['common_weaknesses']:
formatted.append("**Common Weaknesses:**")
for weakness in summary['common_weaknesses'][:5]:
formatted.append(f" • {weakness}")
return "\n".join(formatted)
def create_interface(self):
"""Create the Gradio interface"""
with gr.Blocks(title="CVE RAG System", theme=gr.themes.Soft()) as interface:
gr.Markdown("# 🔍 CVE RAG System")
gr.Markdown("Retrieval-Augmented Generation for Cybersecurity Vulnerability Analysis")
# Health status
with gr.Row():
status_box = gr.Textbox(label="System Status", interactive=False)
refresh_btn = gr.Button("🔄 Refresh Status", size="sm")
# Main query interface
with gr.Tab("🔍 Query"):
with gr.Row():
with gr.Column(scale=1):
query_input = gr.Textbox(
label="Query",
placeholder="e.g., Tell me about CVE-2024-12345 or What are the latest Microsoft vulnerabilities?",
lines=3
)
with gr.Row():
model_choice = gr.Radio(
["Fast (8B)", "Accurate (70B)"],
label="Model",
value="Fast (8B)"
)
max_results = gr.Slider(
minimum=5,
maximum=20,
value=10,
step=5,
label="Max Results"
)
submit_btn = gr.Button("🔍 Search", variant="primary")
with gr.Column(scale=2):
response_output = gr.Markdown(label="Response")
sources_output = gr.JSON(label="Sources")
# Example queries
gr.Examples(
examples=[
"What is CVE-2021-44228 (Log4Shell)?",
"Show me recent Microsoft Exchange vulnerabilities",
"Find vulnerabilities similar to CVE-2021-44228",
"What are the most critical web application vulnerabilities from 2024?",
"Explain SQL injection attacks and related CVEs",
"Summarize all 5G related vulnerabilities",
"What are the latest Apache vulnerabilities?",
"Find critical vulnerabilities affecting industrial control systems",
"Show me vulnerabilities in OpenSSL",
"What are the most exploited vulnerabilities in 2024?"
],
inputs=query_input
)
# Search only tab
with gr.Tab("📋 Search Only"):
with gr.Row():
search_input = gr.Textbox(
label="Search Query",
placeholder="e.g., SQL injection vulnerabilities",
lines=2
)
search_results_count = gr.Slider(
minimum=5,
maximum=50,
value=10,
step=5,
label="Results Count"
)
search_btn = gr.Button("🔍 Search", variant="secondary")
search_output = gr.Markdown(label="Search Results")
# Summary tab
with gr.Tab("📊 Summary"):
with gr.Row():
summary_input = gr.Textbox(
label="Summary Query",
placeholder="e.g., Microsoft vulnerabilities",
lines=2
)
summary_results_count = gr.Slider(
minimum=10,
maximum=100,
value=50,
step=10,
label="Analysis Count"
)
summary_btn = gr.Button("📊 Generate Summary", variant="secondary")
summary_output = gr.Markdown(label="Summary")
# Similar CVEs tab
with gr.Tab("🔗 Similar CVEs"):
with gr.Row():
cve_input = gr.Textbox(
label="CVE ID",
placeholder="e.g., CVE-2024-12345",
lines=1
)
similar_count = gr.Slider(
minimum=3,
maximum=10,
value=5,
step=1,
label="Similar Count"
)
similar_btn = gr.Button("🔗 Find Similar", variant="secondary")
similar_output = gr.Markdown(label="Similar CVEs")
# Event handlers
async def process_query(query, model_choice, max_results):
use_large_model = "70B" in model_choice
response, sources = await self.query_api(query, use_large_model, max_results)
return response, sources
async def process_search(query, max_results):
results = await self.search_api(query, max_results)
return self.format_search_results(results)
async def process_summary(query, max_results):
summary = await self.summary_api(query, max_results)
return self.format_summary(summary)
async def process_similar(cve_id, count):
results = await self.search_api(f"similar to {cve_id}", count)
return self.format_search_results(results)
async def check_health():
health = await self.health_check()
if health.get('status') == 'healthy':
status_text = f"✅ System Healthy | GPU: {health.get('gpu_name', 'N/A')} | Documents: {health.get('vector_db_documents', 0)}"
else:
status_text = f"❌ System Unhealthy | Error: {health.get('error', 'Unknown')}"
return status_text
# Wire up the interface
submit_btn.click(
fn=process_query,
inputs=[query_input, model_choice, max_results],
outputs=[response_output, sources_output]
)
search_btn.click(
fn=process_search,
inputs=[search_input, search_results_count],
outputs=[search_output]
)
summary_btn.click(
fn=process_summary,
inputs=[summary_input, summary_results_count],
outputs=[summary_output]
)
similar_btn.click(
fn=process_similar,
inputs=[cve_input, similar_count],
outputs=[similar_output]
)
refresh_btn.click(
fn=check_health,
outputs=[status_box]
)
# Initial health check
interface.load(check_health, outputs=[status_box])
return interface
def main():
"""Main function to launch the Gradio interface"""
interface = RAGInterface()
app = interface.create_interface()
print("🚀 Starting CVE RAG System UI...")
print("📊 API: http://localhost:8000")
print("🌐 UI: http://localhost:7860")
print("📚 Docs: http://localhost:8000/docs")
app.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
show_error=True
)
if __name__ == "__main__":
main() |