Spaces:
Sleeping
Sleeping
File size: 23,796 Bytes
18c9405 c31b74d 8ea29e3 f6249c8 18c9405 c31b74d f6249c8 18c9405 c31b74d 18c9405 8ea29e3 18c9405 c31b74d 18c9405 c31b74d 18c9405 8ea29e3 18c9405 8ea29e3 18c9405 c31b74d 18c9405 c31b74d 18c9405 c31b74d 087b518 c31b74d 73e99ed 087b518 c31b74d 87aa5b7 c31b74d 087b518 87aa5b7 c31b74d 087b518 c31b74d 87aa5b7 c31b74d 87aa5b7 c31b74d 87aa5b7 c31b74d 87aa5b7 c31b74d 87aa5b7 087b518 c31b74d 87aa5b7 c31b74d 87aa5b7 c31b74d 87aa5b7 c31b74d 87aa5b7 c31b74d 87aa5b7 c31b74d 18c9405 c31b74d 5fdda3f c31b74d 5fdda3f c31b74d d62475b 087b518 c31b74d 5fdda3f d62475b c31b74d d62475b c31b74d d62475b 5fdda3f 8ea29e3 5fdda3f 8ea29e3 5fdda3f 8ea29e3 5fdda3f 8ea29e3 5fdda3f 8ea29e3 5fdda3f 8ea29e3 5fdda3f baa6fc3 c31b74d 5fdda3f c31b74d 5fdda3f c31b74d 5fdda3f c31b74d 5fdda3f c31b74d d62475b c31b74d d62475b c31b74d 5fdda3f c31b74d 5fdda3f c31b74d d62475b 5fdda3f c31b74d d62475b 5fdda3f c31b74d 5fdda3f d62475b 5fdda3f c31b74d d62475b 5fdda3f 95f7879 5fdda3f d62475b 5fdda3f c31b74d d62475b 5fdda3f d62475b c31b74d 5fdda3f c31b74d 5fdda3f baa6fc3 5fdda3f c31b74d 5fdda3f baa6fc3 c31b74d d62475b c31b74d 5fdda3f | 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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 | from flask import jsonify, request
import logging
import os
import json
from datetime import datetime, date, time
import uuid
import pytesseract
from PIL import Image
import pdf2image
import io
import tempfile
import openai
from models.log import Log
from models.user import User
from models.department import Department
from models.workflow import Workflow
from models.incident import Incident
from utils.pdf_utils import pdf_to_text, extract_activities
from db import get_gridfs
from bson.objectid import ObjectId
# Configure logging
logger = logging.getLogger(__name__)
def upload_log(current_user):
"""Upload a new log file, extract text using OCR, and save only the text"""
if 'file' not in request.files:
return jsonify({'message': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'message': 'No selected file'}), 400
# Validate file is PDF
if not file.filename.lower().endswith('.pdf'):
return jsonify({'message': 'Only PDF files are allowed'}), 400
# Get log date from form data
log_date_str = request.form.get('log_date')
if not log_date_str:
return jsonify({'message': 'Log date is required'}), 400
try:
# Parse the date string and convert to datetime at midnight
parsed_date = datetime.strptime(log_date_str, '%Y-%m-%d').date()
log_datetime = datetime.combine(parsed_date, time.min) # Use time.min for midnight
# Read the file content
file_content = file.read()
# Extract text from PDF using OCR
logger.info(f"Extracting text from PDF using OCR")
extracted_text = pdf_to_text(file_content, is_bytes=True)
# Create new log entry with datetime object
log = Log(
user_id=current_user._id,
department_id=current_user.department_id,
log_date=log_datetime, # Pass datetime object
log_text=extracted_text
)
if log.save():
# Process log synchronously
result = process_log_sync(str(log._id))
return jsonify({
'message': 'Log uploaded and processed successfully',
'log': log.to_dict(),
'incidents_created': result.get('incidents_created', 0)
}), 201
else:
return jsonify({'message': 'Failed to save log entry'}), 500
except ValueError:
return jsonify({'message': 'Invalid date format. Please use YYYY-MM-DD'}), 400
except Exception as e:
logger.error(f"Error uploading log: {str(e)}")
return jsonify({'message': f'Error uploading log: {str(e)}'}), 500
def process_log_sync(log_id):
"""Process a log document synchronously"""
try:
# Check if OpenAI API key is set
api_key = os.environ.get('OPENAI_API_KEY')
if not api_key:
logger.error("OPENAI_API_KEY environment variable is not set")
return {"status": "error", "message": "OpenAI API key not configured"}
# Create OpenAI client with correct parameters for the current version
client = openai.OpenAI(api_key=api_key)
# Retrieve the log
log = Log.find_by_id(log_id)
if not log:
logger.error(f"Log not found: {log_id}")
return {"status": "error", "message": "Log not found"}
# Use the stored text directly instead of extracting from PDF
logger.info(f"Using stored text for log {log_id}")
extracted_text = log.log_text
# 2. Extract activities using LLM
logger.info(f"Extracting activities for log {log_id}")
activities_json = extract_activities(extracted_text)
# Parse the activities JSON
activities_data = json.loads(activities_json)
activities = activities_data.get('activities', [])
# 3. Classify each activity and create incidents
logger.info(f"Classifying activities and creating incidents for log {log_id}")
# Get all workflows for this department
workflows = Workflow.find_by_department(log.department_id)
# Skip if no workflows defined
if not workflows:
logger.warning(f"No workflows defined for department {log.department_id}")
return {"status": "completed", "message": "No workflows to process", "incidents_created": 0}
# Prepare workflow information for classification
workflow_info = []
for workflow in workflows:
workflow_info.append({
"id": str(workflow._id),
"title": workflow.title,
"description": workflow.description
})
# Classify each activity against workflows
classified_activities = []
created_incidents = 0
for activity in activities:
# Classify activity against workflows
workflow_id = classify_activity(activity, workflow_info)
# If classified as a workflow, create an incident
if workflow_id:
logger.info(f"Creating incident for activity: {activity['activity']}")
# Create incident
incident = Incident(
department_id=log.department_id,
user_id=log.user_id,
workflow_id=ObjectId(workflow_id),
description=activity['activity'],
date=log.log_date,
activity_text=activity['text'],
log_id=log._id,
status="completed" # Mark as completed since we're processing synchronously
)
if incident.save():
# Add incident to log
log.add_incident(incident._id)
created_incidents += 1
# Add to classified activities
classified_activities.append({
"activity": activity,
"workflow_id": workflow_id,
"incident_id": str(incident._id)
})
return {
"status": "completed",
"message": "Log processing completed",
"incidents_created": created_incidents,
"classified_activities": classified_activities
}
except Exception as e:
logger.error(f"Error processing log {log_id}: {str(e)}")
return {"status": "error", "message": str(e)}
def classify_activity(activity, workflow_info):
"""
Classify an activity against available workflows using an LLM.
Returns workflow_id if matched, None otherwise.
Includes enhanced logging and asks for justification from the LLM.
"""
try:
api_key = os.environ.get('OPENAI_API_KEY')
if not api_key:
logger.error("OPENAI_API_KEY not found for classify_activity.")
return None
client = openai.OpenAI(api_key=api_key)
workflows_text = "\n".join([
f"Workflow ID: {w['id']} | Title: {w['title']} | Description: {w['description']}"
for w in workflow_info
])
prompt = f"""
Analyze the following law enforcement activity and decide if it matches one of the provided workflows or if it is a mundane activity.
Available Workflows:
{workflows_text}
Activity Details:
Activity Description: {activity.get('activity', 'N/A')}
Full Text: {activity.get('text', 'N/A')}
Time: {activity.get('time', 'Not specified')}
Location: {activity.get('location', 'Not specified')}
Your Task:
1. Determine if the activity clearly matches one of the workflow descriptions.
2. If it matches, provide the corresponding Workflow ID.
3. If it does not match any workflow, classify it as "mundane".
4. Provide a brief justification for your decision (1-2 sentences).
Output Format:
Return a JSON object with two keys: "decision" and "justification".
- "decision": Should be the matching Workflow ID (string) or the string "mundane".
- "justification": Should be a brief string explaining your reasoning.
Example Match Response:
{{"decision": "60d21b4967d0d8992e610c87", "justification": "The activity describes a traffic stop, which matches the Traffic Violation workflow."}}
Example Mundane Response:
{{"decision": "mundane", "justification": "The activity describes routine patrol or administrative tasks not covered by any workflow."}}
"""
# Log the prompt being sent (use debug level for potentially sensitive info)
logger.debug(f"Sending classification prompt to OpenAI: \n{prompt}")
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are an AI assistant helping classify law enforcement activities into predefined workflows. Respond ONLY in the requested JSON format."},
{"role": "user", "content": prompt}
],
response_format={"type": "json_object"} # Ensure JSON output
)
# Parse the JSON response
try:
content = response.choices[0].message.content
logger.debug(f"Received OpenAI classification response content: {content}")
result_json = json.loads(content)
decision = result_json.get("decision")
justification = result_json.get("justification", "No justification provided.")
logger.info(f"LLM Classification - Decision: {decision}, Justification: {justification}")
if decision == "mundane":
return None
# Check if the decision is a valid ObjectId and matches a known workflow ID
valid_workflow_ids = {w['id'] for w in workflow_info}
if decision in valid_workflow_ids:
try:
# Validate it's a proper ObjectId format, though it's already a string match
ObjectId(decision)
return decision # Return the matched workflow ID string
except Exception:
logger.warning(f"LLM returned a decision '{decision}' matching a workflow ID, but it's not a valid ObjectId format. Treating as unclassified.")
return None
else:
logger.warning(f"LLM returned a decision '{decision}' which is not 'mundane' and does not match any known workflow ID. Treating as unclassified.")
return None
except json.JSONDecodeError:
logger.error(f"Failed to decode JSON response from OpenAI: {content}")
return None
except Exception as parse_err:
logger.error(f"Error parsing OpenAI classification response: {parse_err}")
return None
except Exception as e:
logger.error(f"Error in classify_activity function: {str(e)}")
import traceback
logger.error(traceback.format_exc())
return None
def get_log(current_user, log_id):
"""Get log by ID"""
log = Log.find_by_id(log_id)
if not log:
return jsonify({'message': 'Log not found'}), 404
# Check if user has access to this log
if str(log.department_id) != str(current_user.department_id):
return jsonify({'message': 'Access denied to logs from other departments'}), 403
return jsonify({'log': log.to_dict()}), 200
def delete_log(current_user, log_id):
"""Delete a log"""
log = Log.find_by_id(log_id)
if not log:
return jsonify({'message': 'Log not found'}), 404
# Check if user has access to this log
if str(log.department_id) != str(current_user.department_id):
return jsonify({'message': 'Access denied to logs from other departments'}), 403
# Additional check: only log owner or admin can delete
if str(log.user_id) != str(current_user._id) and current_user.permissions != 'Admin':
return jsonify({'message': 'Only the log owner or department admin can delete logs'}), 403
# Delete associated incidents if they exist
for incident_id in log.incidents:
incident = Incident.find_by_id(incident_id)
if incident:
incident.delete()
# Delete the log
if log.delete():
return jsonify({'message': 'Log and associated incidents deleted successfully'}), 200
else:
return jsonify({'message': 'Failed to delete log'}), 500
def get_user_logs(current_user):
"""Get all logs for the current user"""
logs = Log.find_by_user(current_user._id)
return jsonify({'logs': [log.to_dict() for log in logs]}), 200
def get_department_logs(current_user):
"""Get all logs for the user's department"""
# Check if user has admin permissions
if current_user.permissions != 'Admin':
return jsonify({'message': 'Admin permissions required'}), 403
logs = Log.find_by_department(current_user.department_id)
return jsonify({'logs': [log.to_dict() for log in logs]}), 200
def get_logs_by_date_range(current_user):
"""Get logs by date range"""
data = request.get_json()
# Check if required fields are present
if 'start_date' not in data or 'end_date' not in data:
return jsonify({'message': 'Start date and end date are required'}), 400
try:
# Parse date strings
start_date = datetime.strptime(data['start_date'], '%Y-%m-%d').date()
end_date = datetime.strptime(data['end_date'], '%Y-%m-%d').date()
# Get logs by date range
logs = Log.find_by_date_range(current_user.department_id, start_date, end_date)
return jsonify({'logs': [log.to_dict() for log in logs]}), 200
except ValueError:
return jsonify({'message': 'Invalid date format. Please use YYYY-MM-DD'}), 400
except Exception as e:
logger.error(f"Error fetching logs by date range: {str(e)}")
return jsonify({'message': f'Error fetching logs: {str(e)}'}), 500
def classify_log_activities(current_user):
"""
Uploads log, extracts text, creates Log object, extracts activities,
classifies activities, creates Incident objects for classified activities,
and returns results.
"""
logger.info(f"Entering classify_log_activities (now creates Log/Incidents) for user {current_user.email}")
if 'file' not in request.files:
logger.error("No file part in the request")
return jsonify({'message': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
logger.error("No selected file")
return jsonify({'message': 'No selected file'}), 400
if not file.filename.lower().endswith('.pdf'):
logger.error(f"Invalid file type: {file.filename}")
return jsonify({'message': 'Only PDF files are allowed'}), 400
try:
logger.info("Checking for OpenAI API key...")
api_key = os.environ.get('OPENAI_API_KEY')
if not api_key:
logger.error("OPENAI_API_KEY environment variable is not set")
return jsonify({'message': 'OpenAI API key not configured'}), 500
logger.info("Reading file content...")
file_content = file.read()
logger.info(f"Read {len(file_content)} bytes from file {file.filename}")
logger.info(f"Starting OCR...")
extracted_text = pdf_to_text(file_content, is_bytes=True)
logger.info(f"OCR finished. Extracted {len(extracted_text)} characters.")
# Get log date from form data and convert to datetime at midnight
log_date_str = request.form.get('log_date', datetime.now().strftime('%Y-%m-%d'))
try:
parsed_date = datetime.strptime(log_date_str, '%Y-%m-%d').date()
log_datetime = datetime.combine(parsed_date, time.min) # Use time.min
except ValueError:
logger.warning(f"Invalid log_date format '{log_date_str}', using today's date.")
parsed_date = datetime.now().date()
log_datetime = datetime.combine(parsed_date, time.min)
logger.info(f"Creating Log object for user {current_user._id} on {log_datetime.date()}")
new_log = Log(
user_id=current_user._id,
department_id=current_user.department_id,
log_date=log_datetime, # Pass datetime object
log_text=extracted_text,
incidents=[]
)
if not new_log.save():
logger.error("Failed to save initial Log object.")
return jsonify({'message': 'Failed to create log entry'}), 500
log_id = new_log._id
logger.info(f"Log object created and saved successfully with ID: {log_id}")
logger.info(f"Extracting activities with LLM for log {log_id}...")
activities_json = extract_activities(extracted_text)
logger.info(f"Activity extraction finished for log {log_id}. JSON length: {len(activities_json)}.")
logger.info(f"Parsing activities JSON for log {log_id}...")
activities_data = json.loads(activities_json)
activities = activities_data.get('activities', [])
logger.info(f"Parsed activities JSON for log {log_id}. Found {len(activities)} activities.")
logger.info(f"Fetching workflows for department {current_user.department_id}...")
workflows = Workflow.find_by_department(current_user.department_id)
logger.info(f"Fetched {len(workflows)} workflows.")
# Prepare workflow info only if workflows exist
workflow_info = []
if workflows:
for workflow in workflows:
workflow_info.append({
"id": str(workflow._id),
"title": workflow.title,
"description": workflow.description
})
logger.info(f"Prepared workflow info for classification: {workflow_info}")
else:
logger.warning(f"No workflows found for department {current_user.department_id}. No classification will occur.")
classified_activities_output = []
incident_ids_created = [] # Keep track of incidents created for this log
logger.info(f"Starting classification & incident creation loop for {len(activities)} activities...")
for index, activity in enumerate(activities):
logger.info(f"Processing activity {index + 1}/{len(activities)}: '{activity.get('activity', 'N/A')}'")
workflow_id = None
incident_id = None
workflow_title = None
classified = False
if workflow_info:
workflow_id = classify_activity(activity, workflow_info)
activity_text_for_incident = activity.get('text', '')
activity_description = activity.get('activity', 'No description')
if workflow_id:
workflow = next((w for w in workflow_info if w["id"] == workflow_id), None)
if workflow:
classified = True
workflow_title = workflow["title"]
logger.info(f"Activity {index + 1} classified as Workflow: {workflow_title} ({workflow_id}). Creating Incident...")
# Create Incident Object
try:
new_incident = Incident(
department_id=current_user.department_id,
user_id=current_user._id,
workflow_id=ObjectId(workflow_id),
description=activity_description,
date=log_datetime, # Pass the full datetime object
activity_text=activity_text_for_incident,
log_id=log_id,
status="classified" # New initial status
)
if new_incident.save():
incident_id = new_incident._id
incident_ids_created.append(incident_id)
logger.info(f"Incident created successfully with ID: {incident_id} for activity {index + 1}")
else:
logger.error(f"Failed to save Incident object for activity {index + 1}")
except Exception as incident_exc:
logger.error(f"Error creating Incident for activity {index + 1}: {incident_exc}")
else:
logger.warning(f"Activity {index + 1} returned workflow ID {workflow_id} but no matching workflow found. Treating as unclassified.")
else:
logger.info(f"Activity {index + 1} classified as mundane.")
# Prepare the result for this activity to send back to frontend
activity_result = {
"activity": activity,
"classified": classified,
"workflow_id": workflow_id,
"workflow_title": workflow_title,
"incident_id": str(incident_id) if incident_id else None # Include incident ID if created
}
classified_activities_output.append(activity_result)
logger.info(f"Classification & incident creation loop finished. Created {len(incident_ids_created)} incidents.")
# Update Log object with incident IDs
if incident_ids_created:
logger.info(f"Updating Log {log_id} with {len(incident_ids_created)} incident IDs.")
log_to_update = Log.find_by_id(log_id)
if log_to_update:
log_to_update.incidents = incident_ids_created
if not log_to_update.save():
logger.error(f"Failed to update Log {log_id} with incident IDs.")
else:
logger.info(f"Successfully updated Log {log_id} with incident IDs.")
else:
logger.error(f"Could not find Log {log_id} to update with incident IDs.")
# --- Log right before return ---
try:
log_dict_to_return = new_log.to_dict()
logger.info(f"Preparing to return Log object: {log_dict_to_return}")
except Exception as to_dict_err:
logger.error(f"Error calling new_log.to_dict() before return: {to_dict_err}")
# Optionally return an error here if to_dict fails
return jsonify({'message': 'Internal error preparing log data for response.'}), 500
# --- End Log right before return ---
logger.info("Successfully processed upload & classification request. Returning 200 OK.")
return jsonify({
'message': 'Log created, activities extracted and classified, incidents created.',
'log': log_dict_to_return, # Use the logged dictionary
'classified_activities': classified_activities_output,
'extracted_text': extracted_text
}), 200
except Exception as e:
logger.error(f"!!! Unhandled exception in classify_log_activities: {str(e)}")
import traceback
logger.error(traceback.format_exc())
return jsonify({'message': 'An internal server error occurred during log processing.'}), 500 |