Spaces:
Paused
Paused
fix deps and add better logging
Browse files- README.md +1 -1
- app.py +82 -3
- requirements.txt +1 -1
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 🔍
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
|
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: "6.0.0"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
app.py
CHANGED
|
@@ -3,7 +3,10 @@ Cordon - Semantic Anomaly Detection for Log Files
|
|
| 3 |
HuggingFace Space Demo (ZeroGPU)
|
| 4 |
"""
|
| 5 |
|
|
|
|
|
|
|
| 6 |
import tempfile
|
|
|
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
import gradio as gr
|
|
@@ -12,6 +15,14 @@ import torch
|
|
| 12 |
|
| 13 |
from cordon import AnalysisConfig, SemanticLogAnalyzer
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Load sample log file
|
| 16 |
SAMPLE_LOG_PATH = Path(__file__).parent / "sample_apache.log"
|
| 17 |
FALLBACK_LOG_PATH = Path(__file__).parent.parent / "examples" / "apache_sample.log"
|
|
@@ -48,9 +59,16 @@ def analyze_logs(
|
|
| 48 |
The GPU is allocated when this function is called.
|
| 49 |
"""
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
if not log_content.strip():
|
|
|
|
| 52 |
return "", "Anomalous Sections"
|
| 53 |
|
|
|
|
|
|
|
| 54 |
progress(0.05, desc="Preparing log file...")
|
| 55 |
|
| 56 |
# Write content to temp file
|
|
@@ -58,6 +76,8 @@ def analyze_logs(
|
|
| 58 |
f.write(log_content)
|
| 59 |
temp_path = Path(f.name)
|
| 60 |
|
|
|
|
|
|
|
| 61 |
try:
|
| 62 |
progress(0.1, desc="Configuring analyzer...")
|
| 63 |
|
|
@@ -66,6 +86,9 @@ def analyze_logs(
|
|
| 66 |
# Use larger batch size on GPU for better throughput
|
| 67 |
batch_size = 128 if device == "cuda" else 32
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
if mode == "Percentile":
|
| 70 |
config = AnalysisConfig(
|
| 71 |
window_size=window_size,
|
|
@@ -74,6 +97,7 @@ def analyze_logs(
|
|
| 74 |
device=device,
|
| 75 |
batch_size=batch_size,
|
| 76 |
)
|
|
|
|
| 77 |
else:
|
| 78 |
config = AnalysisConfig(
|
| 79 |
window_size=window_size,
|
|
@@ -83,12 +107,19 @@ def analyze_logs(
|
|
| 83 |
device=device,
|
| 84 |
batch_size=batch_size,
|
| 85 |
)
|
|
|
|
| 86 |
|
| 87 |
progress(0.15, desc="Loading embedding model...")
|
|
|
|
|
|
|
| 88 |
analyzer = SemanticLogAnalyzer(config)
|
|
|
|
| 89 |
|
| 90 |
progress(0.4, desc="Analyzing patterns...")
|
|
|
|
|
|
|
| 91 |
result = analyzer.analyze_file_detailed(temp_path)
|
|
|
|
| 92 |
|
| 93 |
progress(0.9, desc="Formatting output...")
|
| 94 |
progress(1.0, desc="Complete!")
|
|
@@ -100,10 +131,20 @@ def analyze_logs(
|
|
| 100 |
|
| 101 |
label = f"Anomalous Sections — {input_tokens:,} → {output_tokens:,} tokens ({reduction:.0f}% reduction)"
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
return result.output, label
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
finally:
|
| 106 |
temp_path.unlink(missing_ok=True)
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
CUSTOM_CSS = """
|
|
@@ -143,7 +184,7 @@ THEME = gr.themes.Base(
|
|
| 143 |
def create_interface():
|
| 144 |
"""Create the Gradio interface."""
|
| 145 |
|
| 146 |
-
with gr.Blocks(title="Cordon"
|
| 147 |
|
| 148 |
# Header
|
| 149 |
gr.HTML("""
|
|
@@ -296,6 +337,44 @@ result = analyzer.analyze_file(Path("system.log"))
|
|
| 296 |
return demo
|
| 297 |
|
| 298 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
# Create and launch the demo
|
| 300 |
-
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
HuggingFace Space Demo (ZeroGPU)
|
| 4 |
"""
|
| 5 |
|
| 6 |
+
import logging
|
| 7 |
+
import sys
|
| 8 |
import tempfile
|
| 9 |
+
import time
|
| 10 |
from pathlib import Path
|
| 11 |
|
| 12 |
import gradio as gr
|
|
|
|
| 15 |
|
| 16 |
from cordon import AnalysisConfig, SemanticLogAnalyzer
|
| 17 |
|
| 18 |
+
# Configure logging for HuggingFace Spaces visibility
|
| 19 |
+
logging.basicConfig(
|
| 20 |
+
level=logging.INFO,
|
| 21 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 22 |
+
handlers=[logging.StreamHandler(sys.stdout)]
|
| 23 |
+
)
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
|
| 26 |
# Load sample log file
|
| 27 |
SAMPLE_LOG_PATH = Path(__file__).parent / "sample_apache.log"
|
| 28 |
FALLBACK_LOG_PATH = Path(__file__).parent.parent / "examples" / "apache_sample.log"
|
|
|
|
| 59 |
The GPU is allocated when this function is called.
|
| 60 |
"""
|
| 61 |
|
| 62 |
+
start_time = time.time()
|
| 63 |
+
logger.info("=" * 60)
|
| 64 |
+
logger.info("Analysis started - GPU allocated by ZeroGPU")
|
| 65 |
+
|
| 66 |
if not log_content.strip():
|
| 67 |
+
logger.warning("Empty log content provided")
|
| 68 |
return "", "Anomalous Sections"
|
| 69 |
|
| 70 |
+
logger.info(f"Log content size: {len(log_content)} characters ({len(log_content.splitlines())} lines)")
|
| 71 |
+
|
| 72 |
progress(0.05, desc="Preparing log file...")
|
| 73 |
|
| 74 |
# Write content to temp file
|
|
|
|
| 76 |
f.write(log_content)
|
| 77 |
temp_path = Path(f.name)
|
| 78 |
|
| 79 |
+
logger.info(f"Temporary log file created: {temp_path}")
|
| 80 |
+
|
| 81 |
try:
|
| 82 |
progress(0.1, desc="Configuring analyzer...")
|
| 83 |
|
|
|
|
| 86 |
# Use larger batch size on GPU for better throughput
|
| 87 |
batch_size = 128 if device == "cuda" else 32
|
| 88 |
|
| 89 |
+
logger.info(f"Device: {device}")
|
| 90 |
+
logger.info(f"Batch size: {batch_size}")
|
| 91 |
+
|
| 92 |
if mode == "Percentile":
|
| 93 |
config = AnalysisConfig(
|
| 94 |
window_size=window_size,
|
|
|
|
| 97 |
device=device,
|
| 98 |
batch_size=batch_size,
|
| 99 |
)
|
| 100 |
+
logger.info(f"Configuration: mode=Percentile, window_size={window_size}, k_neighbors={k_neighbors}, percentile={anomaly_percentile}")
|
| 101 |
else:
|
| 102 |
config = AnalysisConfig(
|
| 103 |
window_size=window_size,
|
|
|
|
| 107 |
device=device,
|
| 108 |
batch_size=batch_size,
|
| 109 |
)
|
| 110 |
+
logger.info(f"Configuration: mode=Range, window_size={window_size}, k_neighbors={k_neighbors}, range=[{range_min}, {range_max}]")
|
| 111 |
|
| 112 |
progress(0.15, desc="Loading embedding model...")
|
| 113 |
+
logger.info("Loading embedding model...")
|
| 114 |
+
model_load_start = time.time()
|
| 115 |
analyzer = SemanticLogAnalyzer(config)
|
| 116 |
+
logger.info(f"Embedding model loaded in {time.time() - model_load_start:.2f}s")
|
| 117 |
|
| 118 |
progress(0.4, desc="Analyzing patterns...")
|
| 119 |
+
logger.info("Starting log analysis...")
|
| 120 |
+
analysis_start = time.time()
|
| 121 |
result = analyzer.analyze_file_detailed(temp_path)
|
| 122 |
+
logger.info(f"Analysis completed in {time.time() - analysis_start:.2f}s")
|
| 123 |
|
| 124 |
progress(0.9, desc="Formatting output...")
|
| 125 |
progress(1.0, desc="Complete!")
|
|
|
|
| 131 |
|
| 132 |
label = f"Anomalous Sections — {input_tokens:,} → {output_tokens:,} tokens ({reduction:.0f}% reduction)"
|
| 133 |
|
| 134 |
+
total_time = time.time() - start_time
|
| 135 |
+
logger.info(f"Total analysis time: {total_time:.2f}s")
|
| 136 |
+
logger.info(f"Token reduction: {input_tokens:,} → {output_tokens:,} ({reduction:.0f}%)")
|
| 137 |
+
logger.info("=" * 60)
|
| 138 |
+
|
| 139 |
return result.output, label
|
| 140 |
|
| 141 |
+
except Exception as e:
|
| 142 |
+
logger.error(f"Error during analysis: {str(e)}", exc_info=True)
|
| 143 |
+
error_msg = f"Analysis failed: {str(e)}"
|
| 144 |
+
return error_msg, "Error"
|
| 145 |
finally:
|
| 146 |
temp_path.unlink(missing_ok=True)
|
| 147 |
+
logger.info("Temporary file cleaned up")
|
| 148 |
|
| 149 |
|
| 150 |
CUSTOM_CSS = """
|
|
|
|
| 184 |
def create_interface():
|
| 185 |
"""Create the Gradio interface."""
|
| 186 |
|
| 187 |
+
with gr.Blocks(title="Cordon") as demo:
|
| 188 |
|
| 189 |
# Header
|
| 190 |
gr.HTML("""
|
|
|
|
| 337 |
return demo
|
| 338 |
|
| 339 |
|
| 340 |
+
# Startup logging
|
| 341 |
+
logger.info("=" * 60)
|
| 342 |
+
logger.info("Cordon Space starting...")
|
| 343 |
+
logger.info(f"Python version: {sys.version}")
|
| 344 |
+
logger.info(f"Gradio version: {gr.__version__}")
|
| 345 |
+
logger.info(f"PyTorch version: {torch.__version__}")
|
| 346 |
+
logger.info(f"CUDA available: {torch.cuda.is_available()}")
|
| 347 |
+
if torch.cuda.is_available():
|
| 348 |
+
logger.info(f"CUDA device: {torch.cuda.get_device_name(0)}")
|
| 349 |
+
logger.info("=" * 60)
|
| 350 |
+
|
| 351 |
# Create and launch the demo
|
| 352 |
+
try:
|
| 353 |
+
logger.info("Creating Gradio interface...")
|
| 354 |
+
demo = create_interface()
|
| 355 |
+
logger.info("Gradio interface created successfully")
|
| 356 |
+
|
| 357 |
+
# Detect if running on HuggingFace Spaces
|
| 358 |
+
import os
|
| 359 |
+
is_hf_space = os.getenv("SPACE_ID") is not None
|
| 360 |
+
|
| 361 |
+
if is_hf_space:
|
| 362 |
+
# On HuggingFace: use strict port binding for health checks
|
| 363 |
+
logger.info("Running on HuggingFace Spaces - using port 7860")
|
| 364 |
+
demo.launch(
|
| 365 |
+
server_name="0.0.0.0",
|
| 366 |
+
server_port=7860,
|
| 367 |
+
theme=THEME,
|
| 368 |
+
css=CUSTOM_CSS
|
| 369 |
+
)
|
| 370 |
+
else:
|
| 371 |
+
# Local development: let Gradio find an available port
|
| 372 |
+
logger.info("Running locally - auto-detecting available port")
|
| 373 |
+
demo.launch(
|
| 374 |
+
theme=THEME,
|
| 375 |
+
css=CUSTOM_CSS
|
| 376 |
+
)
|
| 377 |
+
logger.info("Demo launched successfully")
|
| 378 |
+
except Exception as e:
|
| 379 |
+
logger.error(f"Failed to launch demo: {str(e)}", exc_info=True)
|
| 380 |
+
raise
|
requirements.txt
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
cordon>=0.3.1
|
| 3 |
|
| 4 |
# UI dependencies
|
| 5 |
-
gradio>=
|
| 6 |
|
| 7 |
# HuggingFace Spaces (ZeroGPU support)
|
| 8 |
spaces
|
|
|
|
| 2 |
cordon>=0.3.1
|
| 3 |
|
| 4 |
# UI dependencies
|
| 5 |
+
gradio>=6.0.0,<7.0.0
|
| 6 |
|
| 7 |
# HuggingFace Spaces (ZeroGPU support)
|
| 8 |
spaces
|