mirror of
https://github.com/th30d4y/OpenLearnX.git
synced 2026-05-26 19:26:33 +00:00
feat: unify real activity tracking, admin monitoring, and error UX
This commit is contained in:
@@ -1,14 +1,22 @@
|
||||
import tensorflow as tf
|
||||
import pickle
|
||||
import json
|
||||
import numpy as np
|
||||
import random
|
||||
import os
|
||||
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
||||
from datetime import datetime
|
||||
from bson import ObjectId
|
||||
import uuid
|
||||
|
||||
# Optional TensorFlow import with fallback
|
||||
try:
|
||||
import tensorflow as tf
|
||||
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
||||
TENSORFLOW_AVAILABLE = True
|
||||
except ImportError:
|
||||
tf = None
|
||||
pad_sequences = None
|
||||
TENSORFLOW_AVAILABLE = False
|
||||
|
||||
class AdaptiveQuizMasterLLM:
|
||||
def __init__(self, models_path="./models/"):
|
||||
"""
|
||||
@@ -18,13 +26,13 @@ class AdaptiveQuizMasterLLM:
|
||||
self.model_available = False
|
||||
|
||||
try:
|
||||
# Try to load model files
|
||||
# Try to load model files only if TensorFlow is available
|
||||
model_file = f'{models_path}improved_cnn_model.h5'
|
||||
tokenizer_file = f'{models_path}tokenizer.pickle'
|
||||
label_encoder_file = f'{models_path}label_encoder.pickle'
|
||||
data_file = f'{models_path}processed_commonsenseqa_data.json'
|
||||
|
||||
if all(os.path.exists(f) for f in [model_file, tokenizer_file, label_encoder_file, data_file]):
|
||||
if TENSORFLOW_AVAILABLE and all(os.path.exists(f) for f in [model_file, tokenizer_file, label_encoder_file, data_file]):
|
||||
try:
|
||||
self.model = tf.keras.models.load_model(model_file)
|
||||
print("✅ CNN Model loaded successfully")
|
||||
|
||||
@@ -13,10 +13,11 @@ import signal
|
||||
|
||||
class RealCompilerService:
|
||||
def __init__(self):
|
||||
self.client = docker.from_env()
|
||||
self.client = None # Lazy initialization
|
||||
self.execution_queue = queue.Queue()
|
||||
self.active_executions = {}
|
||||
self.max_concurrent_executions = 5
|
||||
self.docker_available = False
|
||||
|
||||
# Enhanced language configurations with real execution
|
||||
self.language_configs = {
|
||||
@@ -97,6 +98,18 @@ class RealCompilerService:
|
||||
# Start execution worker
|
||||
self.start_execution_worker()
|
||||
|
||||
def _get_docker_client(self):
|
||||
"""Lazily initialize Docker client"""
|
||||
if self.client is None:
|
||||
try:
|
||||
self.client = docker.from_env()
|
||||
self.docker_available = True
|
||||
except Exception as e:
|
||||
print(f"⚠️ Docker initialization failed: {e}")
|
||||
self.docker_available = False
|
||||
self.client = None
|
||||
return self.client
|
||||
|
||||
def start_execution_worker(self):
|
||||
"""Start background worker for code execution"""
|
||||
def worker():
|
||||
@@ -176,6 +189,17 @@ class RealCompilerService:
|
||||
input_data = context['input_data']
|
||||
config = context['config']
|
||||
|
||||
# Check Docker availability
|
||||
docker_client = self._get_docker_client()
|
||||
if docker_client is None or not self.docker_available:
|
||||
return {
|
||||
"output": "",
|
||||
"error": "Docker service is not available. Compiler service cannot execute code.",
|
||||
"exit_code": -1,
|
||||
"execution_time": 0,
|
||||
"memory_used": 0
|
||||
}
|
||||
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
# Prepare code file
|
||||
filename = f"code{config['file_ext']}" if language != 'java' else "Main.java"
|
||||
@@ -193,7 +217,7 @@ class RealCompilerService:
|
||||
start_time = time.time()
|
||||
|
||||
# Create and run container
|
||||
container = self.client.containers.run(
|
||||
container = docker_client.containers.run(
|
||||
config['image'],
|
||||
command=self._build_execution_command(config, filename),
|
||||
volumes={temp_dir: {'bind': '/app', 'mode': 'rw'}},
|
||||
@@ -302,4 +326,8 @@ class RealCompilerService:
|
||||
return False
|
||||
|
||||
# Create global instance
|
||||
real_compiler_service = RealCompilerService()
|
||||
try:
|
||||
real_compiler_service = RealCompilerService()
|
||||
except Exception as e:
|
||||
print(f"⚠️ Failed to initialize RealCompilerService: {e}")
|
||||
real_compiler_service = RealCompilerService() # Still create instance for graceful fallback
|
||||
|
||||
Reference in New Issue
Block a user