Skip to content

Commit

Permalink
Merge pull request #225 from GooeyAI/published-runs
Browse files Browse the repository at this point in the history
Add published runs
  • Loading branch information
devxpy authored Dec 26, 2023
2 parents a10b5f0 + 0a15d9f commit 479239e
Show file tree
Hide file tree
Showing 18 changed files with 1,862 additions and 292 deletions.
45 changes: 44 additions & 1 deletion bots/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
FeedbackComment,
CHATML_ROLE_ASSISSTANT,
SavedRun,
PublishedRun,
PublishedRunVersion,
Message,
Platform,
Feedback,
Expand Down Expand Up @@ -94,13 +96,14 @@ class BotIntegrationAdmin(admin.ModelAdmin):
"updated_at",
"billing_account_uid",
"saved_run",
"published_run",
"analysis_run",
]
list_filter = ["platform"]

form = BotIntegrationAdminForm

autocomplete_fields = ["saved_run", "analysis_run"]
autocomplete_fields = ["saved_run", "published_run", "analysis_run"]

readonly_fields = [
"fb_page_access_token",
Expand All @@ -120,6 +123,7 @@ class BotIntegrationAdmin(admin.ModelAdmin):
"fields": [
"name",
"saved_run",
"published_run",
"billing_account_uid",
"user_language",
],
Expand Down Expand Up @@ -206,6 +210,38 @@ def view_analysis_results(self, bi: BotIntegration):
return html


@admin.register(PublishedRun)
class PublishedRunAdmin(admin.ModelAdmin):
list_display = [
"__str__",
"published_run_id",
"view_user",
"view_saved_run",
"created_at",
"updated_at",
]
list_filter = ["workflow"]
search_fields = ["workflow", "published_run_id"]
autocomplete_fields = ["saved_run", "created_by", "last_edited_by"]
readonly_fields = [
"open_in_gooey",
"created_at",
"updated_at",
]

def view_user(self, published_run: PublishedRun):
if published_run.created_by is None:
return None
return change_obj_url(published_run.created_by)

view_user.short_description = "View User"

def view_saved_run(self, published_run: PublishedRun):
return change_obj_url(published_run.saved_run)

view_saved_run.short_description = "View Saved Run"


@admin.register(SavedRun)
class SavedRunAdmin(admin.ModelAdmin):
list_display = [
Expand All @@ -221,6 +257,7 @@ class SavedRunAdmin(admin.ModelAdmin):
]
list_filter = ["workflow"]
search_fields = ["workflow", "example_id", "run_id", "uid"]
autocomplete_fields = ["parent_version"]

readonly_fields = [
"open_in_gooey",
Expand Down Expand Up @@ -257,6 +294,12 @@ def preview_input(self, saved_run: SavedRun):
return truncate_text_words(BasePage.preview_input(saved_run.state) or "", 100)


@admin.register(PublishedRunVersion)
class PublishedRunVersionAdmin(admin.ModelAdmin):
search_fields = ["id", "version_id", "published_run__published_run_id"]
autocomplete_fields = ["published_run", "saved_run", "changed_by"]


class LastActiveDeltaFilter(admin.SimpleListFilter):
title = Conversation.last_active_delta.short_description
parameter_name = Conversation.last_active_delta.__name__
Expand Down
265 changes: 265 additions & 0 deletions bots/migrations/0049_publishedrun_publishedrunversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
# Generated by Django 4.2.7 on 2023-12-05 13:39

from django.db import migrations, models
import django.db.models.deletion

from bots.models import PublishedRunVisibility
from daras_ai_v2.crypto import get_random_doc_id


def set_field_attribute(instance, field_name, **attrs):
for field in instance._meta.local_fields:
if field.name == field_name:
for attr, value in attrs.items():
setattr(field, attr, value)


def create_published_run_from_example(
*,
published_run_model,
published_run_version_model,
saved_run,
user,
published_run_id,
):
published_run = published_run_model(
workflow=saved_run.workflow,
published_run_id=published_run_id,
created_by=user,
last_edited_by=user,
saved_run=saved_run,
title=saved_run.page_title,
notes=saved_run.page_notes,
visibility=PublishedRunVisibility.PUBLIC,
is_approved_example=not saved_run.hidden,
)
set_field_attribute(published_run, "created_at", auto_now_add=False)
set_field_attribute(published_run, "updated_at", auto_now=False)
published_run.created_at = saved_run.created_at
published_run.updated_at = saved_run.updated_at
published_run.save()
set_field_attribute(published_run, "created_at", auto_now_add=True)
set_field_attribute(published_run, "updated_at", auto_now=True)

version = published_run_version_model(
published_run=published_run,
version_id=get_random_doc_id(),
saved_run=saved_run,
changed_by=user,
title=saved_run.page_title,
notes=saved_run.page_notes,
visibility=PublishedRunVisibility.PUBLIC,
)
set_field_attribute(published_run, "created_at", auto_now_add=False)
version.created_at = saved_run.updated_at
version.save()
set_field_attribute(published_run, "created_at", auto_now_add=True)

return published_run


def forwards_func(apps, schema_editor):
# if example_id is not null, create published run with
# is_approved_example to True and visibility to Public
saved_run_model = apps.get_model("bots", "SavedRun")
published_run_model = apps.get_model("bots", "PublishedRun")
published_run_version_model = apps.get_model("bots", "PublishedRunVersion")
db_alias = schema_editor.connection.alias

# all examples
for saved_run in saved_run_model.objects.using(db_alias).filter(
example_id__isnull=False,
):
create_published_run_from_example(
published_run_model=published_run_model,
published_run_version_model=published_run_version_model,
saved_run=saved_run,
user=None, # TODO: use gooey-support user instead?
published_run_id=saved_run.example_id,
)

# recipe root examples
for saved_run in saved_run_model.objects.using(db_alias).filter(
example_id__isnull=True,
run_id__isnull=True,
uid__isnull=True,
):
create_published_run_from_example(
published_run_model=published_run_model,
published_run_version_model=published_run_version_model,
saved_run=saved_run,
user=None,
published_run_id="",
)


def backwards_func(apps, schema_editor):
pass


class Migration(migrations.Migration):
dependencies = [
("app_users", "0010_alter_appuser_balance_alter_appuser_created_at_and_more"),
("bots", "0048_alter_messageattachment_url"),
]

operations = [
migrations.CreateModel(
name="PublishedRun",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("published_run_id", models.CharField(blank=True, max_length=128)),
(
"workflow",
models.IntegerField(
choices=[
(1, "Doc Search"),
(2, "Doc Summary"),
(3, "Google GPT"),
(4, "Copilot"),
(5, "Lipysnc + TTS"),
(6, "Text to Speech"),
(7, "Speech Recognition"),
(8, "Lipsync"),
(9, "Deforum Animation"),
(10, "Compare Text2Img"),
(11, "Text2Audio"),
(12, "Img2Img"),
(13, "Face Inpainting"),
(14, "Google Image Gen"),
(15, "Compare AI Upscalers"),
(16, "SEO Summary"),
(17, "Email Face Inpainting"),
(18, "Social Lookup Email"),
(19, "Object Inpainting"),
(20, "Image Segmentation"),
(21, "Compare LLM"),
(22, "Chyron Plant"),
(23, "Letter Writer"),
(24, "Smart GPT"),
(25, "AI QR Code"),
(26, "Doc Extract"),
(27, "Related QnA Maker"),
(28, "Related QnA Maker Doc"),
(29, "Embeddings"),
(30, "Bulk Runner"),
]
),
),
("title", models.TextField(blank=True, default="")),
("notes", models.TextField(blank=True, default="")),
(
"visibility",
models.IntegerField(
choices=[(1, "Unlisted"), (2, "Public")], default=1
),
),
("is_approved_example", models.BooleanField(default=False)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"created_by",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="published_runs",
to="app_users.appuser",
),
),
(
"last_edited_by",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="app_users.appuser",
),
),
(
"saved_run",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name="published_runs",
to="bots.savedrun",
),
),
],
options={
"ordering": ["-updated_at"],
"unique_together": {("workflow", "published_run_id")},
},
),
migrations.CreateModel(
name="PublishedRunVersion",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("version_id", models.CharField(max_length=128, unique=True)),
("title", models.TextField(blank=True, default="")),
("notes", models.TextField(blank=True, default="")),
(
"visibility",
models.IntegerField(
choices=[(1, "Unlisted"), (2, "Public")], default=1
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
(
"changed_by",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="app_users.appuser",
),
),
(
"published_run",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="versions",
to="bots.publishedrun",
),
),
(
"saved_run",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name="published_run_versions",
to="bots.savedrun",
),
),
],
options={
"ordering": ["-created_at"],
"get_latest_by": "created_at",
"indexes": [
models.Index(
fields=["published_run", "-created_at"],
name="bots_publis_publish_9cd246_idx",
),
models.Index(
fields=["version_id"], name="bots_publis_version_c121d4_idx"
),
],
},
),
migrations.RunPython(
forwards_func,
backwards_func,
),
]
26 changes: 26 additions & 0 deletions bots/migrations/0050_botintegration_published_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.7 on 2023-12-05 13:39

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
("bots", "0049_publishedrun_publishedrunversion"),
]

operations = [
migrations.AddField(
model_name="botintegration",
name="published_run",
field=models.ForeignKey(
blank=True,
default=None,
help_text="The saved run that the bot is based on",
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="botintegrations",
to="bots.publishedrun",
),
),
]
Loading

0 comments on commit 479239e

Please sign in to comment.