Spaces:
Runtime error
Runtime error
feat: improve image loading and anomaly detection in smart agents, enhancing error handling and mock anomaly generation
Browse files- agents/smart_agents.py +28 -17
- app.py +9 -2
agents/smart_agents.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import logging
|
| 2 |
from PIL import Image
|
| 3 |
-
|
| 4 |
logger = logging.getLogger(__name__)
|
| 5 |
|
| 6 |
class ContextualIntelligenceAgent:
|
|
@@ -23,7 +23,7 @@ class ContextualIntelligenceAgent:
|
|
| 23 |
# Mock external detection (e.g., from a simpler scene classification model or EXIF data)
|
| 24 |
# For demonstration, we'll hardcode some possible tags here.
|
| 25 |
# In a real system, you'd feed actual image features or metadata to an LLM.
|
| 26 |
-
mock_tags = ["
|
| 27 |
for tag in mock_tags:
|
| 28 |
if tag not in context_tags:
|
| 29 |
context_tags.append(tag)
|
|
@@ -37,18 +37,29 @@ class ForensicAnomalyDetectionAgent:
|
|
| 37 |
|
| 38 |
def analyze_forensic_outputs(self, forensic_output_descriptions: list[str]) -> dict:
|
| 39 |
"""Simulates an LLM analyzing descriptions of forensic images for anomalies."""
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import logging
|
| 2 |
from PIL import Image
|
| 3 |
+
import smolagents
|
| 4 |
logger = logging.getLogger(__name__)
|
| 5 |
|
| 6 |
class ContextualIntelligenceAgent:
|
|
|
|
| 23 |
# Mock external detection (e.g., from a simpler scene classification model or EXIF data)
|
| 24 |
# For demonstration, we'll hardcode some possible tags here.
|
| 25 |
# In a real system, you'd feed actual image features or metadata to an LLM.
|
| 26 |
+
mock_tags = ["foo", "bar"] # These could be returned by an actual LLM based on input
|
| 27 |
for tag in mock_tags:
|
| 28 |
if tag not in context_tags:
|
| 29 |
context_tags.append(tag)
|
|
|
|
| 37 |
|
| 38 |
def analyze_forensic_outputs(self, forensic_output_descriptions: list[str]) -> dict:
|
| 39 |
"""Simulates an LLM analyzing descriptions of forensic images for anomalies."""
|
| 40 |
+
import random
|
| 41 |
+
|
| 42 |
+
# 4 mock anomalies for demo purposes
|
| 43 |
+
mock_anomalies = [
|
| 44 |
+
{
|
| 45 |
+
"summary": "ELA analysis reveals potential image manipulation",
|
| 46 |
+
"details": ["ELA: Unusually strong compression artifacts detected", "ELA: Inconsistent noise patterns suggest compositing"]
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"summary": "Bit plane analysis shows irregular patterns",
|
| 50 |
+
"details": ["Bit Plane: Unexpected data patterns in LSB", "Bit Plane: Hidden information detected in lower planes"]
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"summary": "Gradient analysis indicates artificial boundaries",
|
| 54 |
+
"details": ["Gradient: Sharp discontinuities in color transitions", "Gradient: Unnatural edge patterns detected"]
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"summary": "Wavelet analysis reveals processing artifacts",
|
| 58 |
+
"details": ["Wavelet: Unusual frequency distribution", "Wavelet: Compression artifacts inconsistent with natural images"]
|
| 59 |
+
}
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
# Randomly select one of the mock anomalies
|
| 63 |
+
selected_anomaly = random.choice(mock_anomalies)
|
| 64 |
+
|
| 65 |
+
return selected_anomaly
|
app.py
CHANGED
|
@@ -266,7 +266,14 @@ def full_prediction(img, confidence_threshold, augment_methods, rotate_degrees,
|
|
| 266 |
# Ensure img is a PIL Image object
|
| 267 |
if img is None:
|
| 268 |
raise gr.Error("No image provided. Please upload an image to analyze.")
|
| 269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
if not isinstance(img, Image.Image):
|
| 271 |
try:
|
| 272 |
img = Image.fromarray(img)
|
|
@@ -429,7 +436,7 @@ def full_prediction(img, confidence_threshold, augment_methods, rotate_degrees,
|
|
| 429 |
detection_model_eval_playground = gr.Interface(
|
| 430 |
fn=full_prediction,
|
| 431 |
inputs=[
|
| 432 |
-
gr.Image(label="Upload Image to Analyze", sources=['upload', 'webcam'], type='
|
| 433 |
gr.Slider(0.0, 1.0, value=0.7, step=0.05, label="Confidence Threshold"),
|
| 434 |
gr.CheckboxGroup(["rotate", "add_noise", "sharpen"], label="Augmentation Methods"),
|
| 435 |
gr.Slider(0, 45, value=0, step=1, label="Rotate Degrees", visible=False),
|
|
|
|
| 266 |
# Ensure img is a PIL Image object
|
| 267 |
if img is None:
|
| 268 |
raise gr.Error("No image provided. Please upload an image to analyze.")
|
| 269 |
+
# Handle filepath conversion if needed
|
| 270 |
+
if isinstance(img, str):
|
| 271 |
+
try:
|
| 272 |
+
img = load_image(img)
|
| 273 |
+
except Exception as e:
|
| 274 |
+
logger.error(f"Error loading image from path: {e}")
|
| 275 |
+
raise gr.Error(f"Could not load image from the provided path. Error: {str(e)}")
|
| 276 |
+
|
| 277 |
if not isinstance(img, Image.Image):
|
| 278 |
try:
|
| 279 |
img = Image.fromarray(img)
|
|
|
|
| 436 |
detection_model_eval_playground = gr.Interface(
|
| 437 |
fn=full_prediction,
|
| 438 |
inputs=[
|
| 439 |
+
gr.Image(label="Upload Image to Analyze", sources=['upload', 'webcam'], type='filepath'),
|
| 440 |
gr.Slider(0.0, 1.0, value=0.7, step=0.05, label="Confidence Threshold"),
|
| 441 |
gr.CheckboxGroup(["rotate", "add_noise", "sharpen"], label="Augmentation Methods"),
|
| 442 |
gr.Slider(0, 45, value=0, step=1, label="Rotate Degrees", visible=False),
|