File size: 19,505 Bytes
94ecb74 |
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 |
"""
π Real-time Progress Tracker for CourseCrafter AI
Advanced progress tracking with visual feedback and status updates.
"""
import gradio as gr
import asyncio
import time
from typing import Dict, List, Any, Optional, Callable, Generator
from dataclasses import dataclass
from enum import Enum
import logging
import threading
from datetime import datetime
logger = logging.getLogger(__name__)
class ProgressStatus(Enum):
"""Progress status enumeration"""
PENDING = "pending"
ACTIVE = "active"
COMPLETE = "complete"
ERROR = "error"
SKIPPED = "skipped"
@dataclass
class ProgressStep:
"""Individual progress step"""
id: str
name: str
description: str
emoji: str
status: ProgressStatus = ProgressStatus.PENDING
progress: float = 0.0
start_time: Optional[datetime] = None
end_time: Optional[datetime] = None
error_message: Optional[str] = None
substeps: List['ProgressStep'] = None
def __post_init__(self):
if self.substeps is None:
self.substeps = []
class RealTimeProgressTracker:
"""Real-time progress tracking with visual updates"""
def __init__(self):
self.steps: List[ProgressStep] = []
self.current_step_index = 0
self.overall_progress = 0.0
self.is_running = False
self.start_time: Optional[datetime] = None
self.end_time: Optional[datetime] = None
self.log_entries: List[str] = []
self.update_callbacks: List[Callable] = []
# Initialize default steps
self._initialize_default_steps()
def _initialize_default_steps(self):
"""Initialize the default course generation steps"""
self.steps = [
ProgressStep(
id="research",
name="Research & Analysis",
description="Gathering information and analyzing the topic",
emoji="π",
substeps=[
ProgressStep("research_web", "Web Search", "Searching for relevant information", "π"),
ProgressStep("research_academic", "Academic Sources", "Finding scholarly content", "π"),
ProgressStep("research_analysis", "Content Analysis", "Analyzing gathered information", "π§ "),
]
),
ProgressStep(
id="planning",
name="Course Planning",
description="Creating the course structure and outline",
emoji="π",
substeps=[
ProgressStep("plan_structure", "Course Structure", "Designing lesson flow", "ποΈ"),
ProgressStep("plan_objectives", "Learning Objectives", "Defining learning goals", "π―"),
ProgressStep("plan_assessment", "Assessment Strategy", "Planning quizzes and activities", "π"),
]
),
ProgressStep(
id="content",
name="Content Generation",
description="Creating engaging lesson content",
emoji="βοΈ",
substeps=[
ProgressStep("content_lessons", "Lesson Content", "Writing lesson materials", "π"),
ProgressStep("content_examples", "Examples & Exercises", "Creating practical examples", "π‘"),
ProgressStep("content_review", "Content Review", "Reviewing and refining content", "π"),
]
),
ProgressStep(
id="assessments",
name="Assessment Creation",
description="Generating quizzes and flashcards",
emoji="π―",
substeps=[
ProgressStep("assess_flashcards", "Flashcards", "Creating study flashcards", "π"),
ProgressStep("assess_quizzes", "Quizzes", "Generating quiz questions", "β"),
ProgressStep("assess_validation", "Validation", "Validating assessment quality", "β
"),
]
),
ProgressStep(
id="images",
name="Visual Content",
description="Generating images and diagrams",
emoji="π¨",
substeps=[
ProgressStep("images_cover", "Course Cover", "Creating course cover image", "πΌοΈ"),
ProgressStep("images_lessons", "Lesson Images", "Generating lesson illustrations", "π"),
ProgressStep("images_diagrams", "Diagrams", "Creating concept diagrams", "π"),
]
),
ProgressStep(
id="finalize",
name="Finalization",
description="Packaging and finalizing the course",
emoji="π¦",
substeps=[
ProgressStep("final_package", "Course Package", "Assembling course materials", "π"),
ProgressStep("final_metadata", "Metadata", "Adding course metadata", "π·οΈ"),
ProgressStep("final_validation", "Final Validation", "Final quality check", "π"),
]
)
]
def start_generation(self) -> None:
"""Start the course generation process"""
self.is_running = True
self.start_time = datetime.now()
self.current_step_index = 0
self.overall_progress = 0.0
self.log_entries = []
self.add_log_entry("π Starting course generation...")
self.add_log_entry(f"β° Started at {self.start_time.strftime('%H:%M:%S')}")
def update_step_progress(self, step_id: str, progress: float, message: str = "") -> None:
"""Update progress for a specific step"""
step = self._find_step_by_id(step_id)
if not step:
logger.warning(f"Step {step_id} not found")
return
step.progress = min(100.0, max(0.0, progress))
if step.status == ProgressStatus.PENDING and progress > 0:
step.status = ProgressStatus.ACTIVE
step.start_time = datetime.now()
self.add_log_entry(f"{step.emoji} Started: {step.name}")
if progress >= 100.0:
step.status = ProgressStatus.COMPLETE
step.end_time = datetime.now()
duration = (step.end_time - step.start_time).total_seconds() if step.start_time else 0
self.add_log_entry(f"β
Completed: {step.name} ({duration:.1f}s)")
if message:
self.add_log_entry(f" {message}")
self._update_overall_progress()
self._notify_callbacks()
def update_substep_progress(self, step_id: str, substep_id: str, progress: float, message: str = "") -> None:
"""Update progress for a substep"""
step = self._find_step_by_id(step_id)
if not step:
return
substep = self._find_substep_by_id(step, substep_id)
if not substep:
return
substep.progress = min(100.0, max(0.0, progress))
if substep.status == ProgressStatus.PENDING and progress > 0:
substep.status = ProgressStatus.ACTIVE
substep.start_time = datetime.now()
self.add_log_entry(f" {substep.emoji} {substep.name}...")
if progress >= 100.0:
substep.status = ProgressStatus.COMPLETE
substep.end_time = datetime.now()
if message:
self.add_log_entry(f" {message}")
# Update parent step progress based on substeps
self._update_step_from_substeps(step)
self._notify_callbacks()
def mark_step_error(self, step_id: str, error_message: str) -> None:
"""Mark a step as having an error"""
step = self._find_step_by_id(step_id)
if not step:
return
step.status = ProgressStatus.ERROR
step.error_message = error_message
step.end_time = datetime.now()
self.add_log_entry(f"β Error in {step.name}: {error_message}")
self._notify_callbacks()
def skip_step(self, step_id: str, reason: str = "") -> None:
"""Skip a step"""
step = self._find_step_by_id(step_id)
if not step:
return
step.status = ProgressStatus.SKIPPED
step.progress = 100.0
step.end_time = datetime.now()
skip_msg = f"βοΈ Skipped: {step.name}"
if reason:
skip_msg += f" ({reason})"
self.add_log_entry(skip_msg)
self._update_overall_progress()
self._notify_callbacks()
def complete_generation(self, success: bool = True) -> None:
"""Complete the course generation process"""
self.is_running = False
self.end_time = datetime.now()
if success:
self.overall_progress = 100.0
total_time = (self.end_time - self.start_time).total_seconds() if self.start_time else 0
self.add_log_entry(f"π Course generation completed successfully!")
self.add_log_entry(f"β±οΈ Total time: {total_time:.1f} seconds")
else:
self.add_log_entry("β Course generation failed")
self._notify_callbacks()
def add_log_entry(self, message: str) -> None:
"""Add an entry to the progress log"""
timestamp = datetime.now().strftime("%H:%M:%S")
log_entry = f"[{timestamp}] {message}"
self.log_entries.append(log_entry)
# Keep only last 100 entries
if len(self.log_entries) > 100:
self.log_entries = self.log_entries[-100:]
def get_progress_display(self) -> Dict[str, Any]:
"""Get current progress display data"""
return {
"overall_progress": self.overall_progress,
"current_step": self.get_current_step_display(),
"step_indicators": self.get_step_indicators(),
"progress_log": "\n".join(self.log_entries[-20:]), # Last 20 entries
"is_running": self.is_running,
"elapsed_time": self.get_elapsed_time()
}
def get_current_step_display(self) -> str:
"""Get current step display HTML"""
if not self.is_running:
if self.overall_progress >= 100:
return """
<div class='step-indicator complete animate-bounce'>
<div class='step-icon'>π</div>
<div class='step-text'>Course Generation Complete!</div>
<div class='step-message'>Your course is ready to explore</div>
</div>
"""
else:
return """
<div class='step-indicator pending'>
<div class='step-icon'>π</div>
<div class='step-text'>Ready to Generate Course</div>
<div class='step-message'>Click the generate button to begin</div>
</div>
"""
current_step = self._get_current_active_step()
if not current_step:
return "<div class='step-indicator'>Processing...</div>"
status_class = current_step.status.value
return f"""
<div class='step-indicator {status_class} animate-pulse'>
<div class='step-icon'>{current_step.emoji}</div>
<div class='step-text'>{current_step.name}</div>
<div class='step-message'>{current_step.description}</div>
<div class='step-progress'>
<div class='progress-bar'>
<div class='progress-fill' style='width: {current_step.progress}%'></div>
</div>
<div class='progress-text'>{current_step.progress:.0f}%</div>
</div>
</div>
"""
def get_step_indicators(self) -> str:
"""Get step indicators HTML"""
indicators_html = "<div class='status-grid'>"
for step in self.steps:
status_class = step.status.value
indicators_html += f"""
<div class='status-item {status_class}' title='{step.description}'>
<div class='status-icon'>{step.emoji}</div>
<div class='status-name'>{step.name}</div>
<div class='status-progress'>{step.progress:.0f}%</div>
</div>
"""
indicators_html += "</div>"
return indicators_html
def get_elapsed_time(self) -> str:
"""Get elapsed time string"""
if not self.start_time:
return "00:00"
end_time = self.end_time or datetime.now()
elapsed = (end_time - self.start_time).total_seconds()
minutes = int(elapsed // 60)
seconds = int(elapsed % 60)
return f"{minutes:02d}:{seconds:02d}"
def add_update_callback(self, callback: Callable) -> None:
"""Add a callback for progress updates"""
self.update_callbacks.append(callback)
def _find_step_by_id(self, step_id: str) -> Optional[ProgressStep]:
"""Find a step by ID"""
for step in self.steps:
if step.id == step_id:
return step
return None
def _find_substep_by_id(self, step: ProgressStep, substep_id: str) -> Optional[ProgressStep]:
"""Find a substep by ID"""
for substep in step.substeps:
if substep.id == substep_id:
return substep
return None
def _update_step_from_substeps(self, step: ProgressStep) -> None:
"""Update step progress based on substeps"""
if not step.substeps:
return
total_progress = sum(substep.progress for substep in step.substeps)
step.progress = total_progress / len(step.substeps)
# Update step status based on substeps
if all(substep.status == ProgressStatus.COMPLETE for substep in step.substeps):
step.status = ProgressStatus.COMPLETE
if not step.end_time:
step.end_time = datetime.now()
elif any(substep.status == ProgressStatus.ACTIVE for substep in step.substeps):
if step.status == ProgressStatus.PENDING:
step.status = ProgressStatus.ACTIVE
step.start_time = datetime.now()
elif any(substep.status == ProgressStatus.ERROR for substep in step.substeps):
step.status = ProgressStatus.ERROR
def _update_overall_progress(self) -> None:
"""Update overall progress based on all steps"""
total_progress = sum(step.progress for step in self.steps)
self.overall_progress = total_progress / len(self.steps) if self.steps else 0.0
def _get_current_active_step(self) -> Optional[ProgressStep]:
"""Get the currently active step"""
for step in self.steps:
if step.status == ProgressStatus.ACTIVE:
return step
# If no active step, return the first pending step
for step in self.steps:
if step.status == ProgressStatus.PENDING:
return step
return None
def _notify_callbacks(self) -> None:
"""Notify all registered callbacks"""
for callback in self.update_callbacks:
try:
callback()
except Exception as e:
logger.error(f"Error in progress callback: {e}")
class ProgressSimulator:
"""Simulates realistic progress for demonstration"""
def __init__(self, tracker: RealTimeProgressTracker):
self.tracker = tracker
self.is_running = False
async def simulate_course_generation(self) -> None:
"""Simulate a realistic course generation process"""
self.is_running = True
self.tracker.start_generation()
try:
# Research phase
await self._simulate_step("research", [
("research_web", "Searching web for relevant content...", 3.0),
("research_academic", "Finding academic sources...", 2.0),
("research_analysis", "Analyzing gathered information...", 2.5),
])
# Planning phase
await self._simulate_step("planning", [
("plan_structure", "Designing course structure...", 2.0),
("plan_objectives", "Defining learning objectives...", 1.5),
("plan_assessment", "Planning assessments...", 1.0),
])
# Content generation phase
await self._simulate_step("content", [
("content_lessons", "Generating lesson content...", 4.0),
("content_examples", "Creating examples and exercises...", 3.0),
("content_review", "Reviewing content quality...", 1.5),
])
# Assessment creation phase
await self._simulate_step("assessments", [
("assess_flashcards", "Creating flashcards...", 2.0),
("assess_quizzes", "Generating quiz questions...", 2.5),
("assess_validation", "Validating assessments...", 1.0),
])
# Image generation phase
await self._simulate_step("images", [
("images_cover", "Creating course cover image...", 3.0),
("images_lessons", "Generating lesson illustrations...", 4.0),
("images_diagrams", "Creating concept diagrams...", 2.0),
])
# Finalization phase
await self._simulate_step("finalize", [
("final_package", "Packaging course materials...", 1.5),
("final_metadata", "Adding metadata...", 0.5),
("final_validation", "Final quality check...", 1.0),
])
self.tracker.complete_generation(success=True)
except Exception as e:
self.tracker.add_log_entry(f"β Simulation error: {str(e)}")
self.tracker.complete_generation(success=False)
finally:
self.is_running = False
async def _simulate_step(self, step_id: str, substeps: List[tuple]) -> None:
"""Simulate a step with substeps"""
for substep_id, message, duration in substeps:
if not self.is_running:
break
self.tracker.add_log_entry(f"Starting {message}")
# Simulate gradual progress
steps = 20
for i in range(steps + 1):
if not self.is_running:
break
progress = (i / steps) * 100
self.tracker.update_substep_progress(step_id, substep_id, progress)
await asyncio.sleep(duration / steps)
# Add some realistic variation
await asyncio.sleep(0.2)
def stop_simulation(self) -> None:
"""Stop the simulation"""
self.is_running = False |