ciyidogan commited on
Commit
5713a15
·
verified ·
1 Parent(s): 1bcbd7c

Update config_provider.py

Browse files
Files changed (1) hide show
  1. config_provider.py +132 -5
config_provider.py CHANGED
@@ -285,7 +285,7 @@ class ConfigProvider:
285
  no=1,
286
  caption="Initial version",
287
  description="Auto-generated initial version",
288
- published=False,
289
  deleted=False,
290
  general_prompt="You are a helpful assistant.",
291
  welcome_prompt=None,
@@ -413,6 +413,39 @@ class ConfigProvider:
413
  user=username
414
  )
415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
416
  # ===================== Version Methods =====================
417
 
418
  @classmethod
@@ -425,12 +458,65 @@ class ConfigProvider:
425
  if not project:
426
  raise ResourceNotFoundError("project", project_id)
427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
  # Create version
429
  version = VersionConfig(
430
  no=project.version_id_counter,
 
 
431
  created_date=get_current_timestamp(),
432
  created_by=username,
433
- **version_data
434
  )
435
 
436
  # Update project
@@ -459,7 +545,7 @@ class ConfigProvider:
459
  return version
460
 
461
  @classmethod
462
- def publish_version(cls, project_id: int, version_no: int, username: str) -> VersionConfig:
463
  """Publish a version"""
464
  with cls._lock:
465
  config = cls.get()
@@ -503,7 +589,7 @@ class ConfigProvider:
503
  user=username
504
  )
505
 
506
- return version
507
 
508
  @classmethod
509
  def update_version(cls, project_id: int, version_no: int, update_data: dict, username: str, expected_last_update: Optional[str] = None) -> VersionConfig:
@@ -565,6 +651,47 @@ class ConfigProvider:
565
 
566
  return version
567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
568
  # ===================== API Methods =====================
569
 
570
  @classmethod
@@ -724,4 +851,4 @@ class ConfigProvider:
724
 
725
  # Keep only last 1000 entries
726
  if len(config.activity_log) > 1000:
727
- config.activity_log = config.activity_log[-1000:]
 
285
  no=1,
286
  caption="Initial version",
287
  description="Auto-generated initial version",
288
+ published=False, # Explicitly set to False
289
  deleted=False,
290
  general_prompt="You are a helpful assistant.",
291
  welcome_prompt=None,
 
413
  user=username
414
  )
415
 
416
+ @classmethod
417
+ def toggle_project(cls, project_id: int, username: str) -> bool:
418
+ """Toggle project enabled status"""
419
+ with cls._lock:
420
+ config = cls.get()
421
+ project = cls.get_project(project_id)
422
+
423
+ if not project:
424
+ raise ResourceNotFoundError("project", project_id)
425
+
426
+ project.enabled = not project.enabled
427
+ project.last_update_date = get_current_timestamp()
428
+ project.last_update_user = username
429
+
430
+ # Log activity
431
+ cls._add_activity(
432
+ config, username, "TOGGLE_PROJECT",
433
+ "project", project.id, project.name,
434
+ f"{'Enabled' if project.enabled else 'Disabled'}"
435
+ )
436
+
437
+ # Save
438
+ cls.save(config, username)
439
+
440
+ log_info(
441
+ "Project toggled",
442
+ project_id=project.id,
443
+ enabled=project.enabled,
444
+ user=username
445
+ )
446
+
447
+ return project.enabled
448
+
449
  # ===================== Version Methods =====================
450
 
451
  @classmethod
 
458
  if not project:
459
  raise ResourceNotFoundError("project", project_id)
460
 
461
+ # Handle source version copy
462
+ if 'source_version_no' in version_data and version_data['source_version_no']:
463
+ source_version = next((v for v in project.versions if v.no == version_data['source_version_no']), None)
464
+ if source_version:
465
+ # Copy from source version
466
+ version_dict = source_version.model_dump()
467
+ # Remove fields that shouldn't be copied
468
+ for field in ['no', 'created_date', 'created_by', 'published', 'publish_date',
469
+ 'published_by', 'last_update_date', 'last_update_user']:
470
+ version_dict.pop(field, None)
471
+ # Override with provided data
472
+ version_dict['caption'] = version_data.get('caption', f"Copy of {source_version.caption}")
473
+ else:
474
+ # Source not found, create blank
475
+ version_dict = {
476
+ 'caption': version_data.get('caption', 'New Version'),
477
+ 'general_prompt': '',
478
+ 'welcome_prompt': None,
479
+ 'llm': {
480
+ 'repo_id': '',
481
+ 'generation_config': {
482
+ 'max_new_tokens': 512,
483
+ 'temperature': 0.7,
484
+ 'top_p': 0.95,
485
+ 'repetition_penalty': 1.1
486
+ },
487
+ 'use_fine_tune': False,
488
+ 'fine_tune_zip': ''
489
+ },
490
+ 'intents': []
491
+ }
492
+ else:
493
+ # Create blank version
494
+ version_dict = {
495
+ 'caption': version_data.get('caption', 'New Version'),
496
+ 'general_prompt': '',
497
+ 'welcome_prompt': None,
498
+ 'llm': {
499
+ 'repo_id': '',
500
+ 'generation_config': {
501
+ 'max_new_tokens': 512,
502
+ 'temperature': 0.7,
503
+ 'top_p': 0.95,
504
+ 'repetition_penalty': 1.1
505
+ },
506
+ 'use_fine_tune': False,
507
+ 'fine_tune_zip': ''
508
+ },
509
+ 'intents': []
510
+ }
511
+
512
  # Create version
513
  version = VersionConfig(
514
  no=project.version_id_counter,
515
+ published=False, # New versions are always unpublished
516
+ deleted=False,
517
  created_date=get_current_timestamp(),
518
  created_by=username,
519
+ **version_dict
520
  )
521
 
522
  # Update project
 
545
  return version
546
 
547
  @classmethod
548
+ def publish_version(cls, project_id: int, version_no: int, username: str) -> tuple[ProjectConfig, VersionConfig]:
549
  """Publish a version"""
550
  with cls._lock:
551
  config = cls.get()
 
589
  user=username
590
  )
591
 
592
+ return project, version
593
 
594
  @classmethod
595
  def update_version(cls, project_id: int, version_no: int, update_data: dict, username: str, expected_last_update: Optional[str] = None) -> VersionConfig:
 
651
 
652
  return version
653
 
654
+ @classmethod
655
+ def delete_version(cls, project_id: int, version_no: int, username: str) -> None:
656
+ """Soft delete version"""
657
+ with cls._lock:
658
+ config = cls.get()
659
+ project = cls.get_project(project_id)
660
+
661
+ if not project:
662
+ raise ResourceNotFoundError("project", project_id)
663
+
664
+ version = next((v for v in project.versions if v.no == version_no), None)
665
+ if not version:
666
+ raise ResourceNotFoundError("version", version_no)
667
+
668
+ if version.published:
669
+ raise ValidationError("Cannot delete published version")
670
+
671
+ version.deleted = True
672
+ version.last_update_date = get_current_timestamp()
673
+ version.last_update_user = username
674
+
675
+ # Update project
676
+ project.last_update_date = get_current_timestamp()
677
+ project.last_update_user = username
678
+
679
+ # Log activity
680
+ cls._add_activity(
681
+ config, username, "DELETE_VERSION",
682
+ "version", f"{project.id}:{version.no}", f"{project.name} v{version.no}"
683
+ )
684
+
685
+ # Save
686
+ cls.save(config, username)
687
+
688
+ log_info(
689
+ "Version deleted",
690
+ project_id=project.id,
691
+ version_no=version.no,
692
+ user=username
693
+ )
694
+
695
  # ===================== API Methods =====================
696
 
697
  @classmethod
 
851
 
852
  # Keep only last 1000 entries
853
  if len(config.activity_log) > 1000:
854
+ config.activity_log = config.activity_log[-1000:]