-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.py
More file actions
164 lines (142 loc) · 5.61 KB
/
models.py
File metadata and controls
164 lines (142 loc) · 5.61 KB
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
from django.db import models
import uuid
import json
from .validators import (
validate_canvas_state,
validate_block_config,
validate_group_internal_structure,
validate_shape_data,
)
class Project(models.Model):
"""Represents a model building project"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(
'authentication.User',
on_delete=models.CASCADE,
related_name='projects',
null=True, # Allow null for existing projects during migration
blank=True,
db_index=True, # Index for faster queries
help_text='User who owns this project'
)
name = models.CharField(max_length=255)
description = models.TextField(blank=True, default='')
framework = models.CharField(
max_length=20,
choices=[('pytorch', 'PyTorch'), ('tensorflow', 'TensorFlow')],
default='pytorch'
)
share_token = models.UUIDField(
default=None,
null=True,
blank=True,
unique=True,
db_index=True,
help_text='Unique token for public sharing; generated on first share'
)
is_shared = models.BooleanField(
default=False,
help_text='Whether this project is publicly accessible via share link'
)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['-updated_at']
def __str__(self):
return self.name
class ModelArchitecture(models.Model):
"""Stores the architecture graph for a project"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
project = models.OneToOneField(
Project,
on_delete=models.CASCADE,
related_name='architecture'
)
canvas_state = models.JSONField(default=dict, blank=True, validators=[validate_canvas_state])
is_valid = models.BooleanField(default=False)
validation_errors = models.JSONField(default=list, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f"Architecture for {self.project.name}"
class GroupBlockDefinition(models.Model):
"""Project-specific block template for group blocks"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
project = models.ForeignKey(
Project,
on_delete=models.CASCADE,
related_name='group_definitions'
)
name = models.CharField(max_length=255)
description = models.TextField(blank=True, default='')
category = models.CharField(max_length=50, default='utility')
color = models.CharField(max_length=50, default='#9333ea')
# Serialized structure: {nodes, edges, portMappings}
internal_structure = models.JSONField(default=dict, blank=True, validators=[validate_group_internal_structure])
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['-updated_at']
unique_together = ['project', 'name']
def __str__(self):
return f"{self.name} ({self.project.name})"
class Block(models.Model):
"""Represents a single block/layer in the architecture"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
architecture = models.ForeignKey(
ModelArchitecture,
on_delete=models.CASCADE,
related_name='blocks'
)
# Store the frontend node ID for reference
node_id = models.CharField(max_length=255)
block_type = models.CharField(max_length=50)
position_x = models.FloatField(default=0)
position_y = models.FloatField(default=0)
config = models.JSONField(default=dict, blank=True, validators=[validate_block_config])
input_shape = models.JSONField(null=True, blank=True, validators=[validate_shape_data])
output_shape = models.JSONField(null=True, blank=True, validators=[validate_shape_data])
# Group block fields
group_definition = models.ForeignKey(
GroupBlockDefinition,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name='instances'
)
is_expanded = models.BooleanField(default=False)
repetition_metadata = models.JSONField(null=True, blank=True)
instance_config_overrides = models.JSONField(null=True, blank=True, default=dict)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['created_at']
def __str__(self):
return f"{self.block_type} ({self.node_id})"
class Connection(models.Model):
"""Represents a connection/edge between blocks"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
architecture = models.ForeignKey(
ModelArchitecture,
on_delete=models.CASCADE,
related_name='connections'
)
# Store the frontend edge ID for reference
edge_id = models.CharField(max_length=255)
source_block = models.ForeignKey(
Block,
on_delete=models.CASCADE,
related_name='outgoing_connections'
)
target_block = models.ForeignKey(
Block,
on_delete=models.CASCADE,
related_name='incoming_connections'
)
source_handle = models.CharField(max_length=50, blank=True, default='')
target_handle = models.CharField(max_length=50, blank=True, default='')
is_valid = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['created_at']
def __str__(self):
return f"{self.source_block.node_id} -> {self.target_block.node_id}"