Skip to content

Commit

Permalink
Merge branch 'master' into org-support
Browse files Browse the repository at this point in the history
  • Loading branch information
nikochiko committed Aug 7, 2024
2 parents 9871594 + f6539f8 commit 73cb176
Show file tree
Hide file tree
Showing 121 changed files with 5,844 additions and 5,699 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ COPY . .
ENV FORWARDED_ALLOW_IPS='*'
ENV PYTHONUNBUFFERED=1

ARG CAPROVER_GIT_COMMIT_SHA=${CAPROVER_GIT_COMMIT_SHA}
ENV CAPROVER_GIT_COMMIT_SHA=${CAPROVER_GIT_COMMIT_SHA}

EXPOSE 8000
EXPOSE 8501

Expand Down
34 changes: 30 additions & 4 deletions app_users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from app_users import models
from bots.admin_links import open_in_new_tab, list_related_html_url
from bots.models import SavedRun
from bots.models import SavedRun, PublishedRun
from embeddings.models import EmbeddedFile
from usage_costs.models import UsageCost


Expand All @@ -31,7 +32,12 @@ class AppUserAdmin(admin.ModelAdmin):
"is_paying",
"disable_safety_checker",
"disable_rate_limits",
("user_runs", "view_transactions"),
(
"view_saved_runs",
"view_published_runs",
"view_embedded_files",
"view_transactions",
),
"created_at",
"upgraded_from_anonymous_at",
("open_in_firebase", "open_in_stripe"),
Expand Down Expand Up @@ -86,7 +92,9 @@ class AppUserAdmin(admin.ModelAdmin):
"total_usage_cost",
"created_at",
"upgraded_from_anonymous_at",
"user_runs",
"view_saved_runs",
"view_published_runs",
"view_embedded_files",
"view_transactions",
"open_in_firebase",
"open_in_stripe",
Expand All @@ -95,14 +103,32 @@ class AppUserAdmin(admin.ModelAdmin):
autocomplete_fields = ["handle", "subscription"]

@admin.display(description="User Runs")
def user_runs(self, user: models.AppUser):
def view_saved_runs(self, user: models.AppUser):
return list_related_html_url(
SavedRun.objects.filter(uid=user.uid),
query_param="uid",
instance_id=user.uid,
show_add=False,
)

@admin.display(description="Published Runs")
def view_published_runs(self, user: models.AppUser):
return list_related_html_url(
PublishedRun.objects.filter(created_by=user),
query_param="created_by",
instance_id=user.id,
show_add=False,
)

@admin.display(description="Embedded Files")
def view_embedded_files(self, user: models.AppUser):
return list_related_html_url(
EmbeddedFile.objects.filter(created_by=user),
query_param="created_by",
instance_id=user.id,
show_add=False,
)

@admin.display(description="Total Payments")
def total_payments(self, user: models.AppUser):
return "$" + str(
Expand Down
2 changes: 0 additions & 2 deletions bots/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@
"twilio_username",
"twilio_password",
"twilio_use_missed_call",
"twilio_tts_voice",
"twilio_asr_language",
"twilio_initial_text",
"twilio_initial_audio_url",
"twilio_waiting_text",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.7 on 2024-07-22 22:52

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('bots', '0078_alter_botintegration_unique_together_and_more'),
]

operations = [
migrations.RemoveField(
model_name='botintegration',
name='twilio_asr_language',
),
migrations.RemoveField(
model_name='botintegration',
name='twilio_tts_voice',
),
]
17 changes: 3 additions & 14 deletions bots/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,16 +694,6 @@ class BotIntegration(models.Model):
blank=True,
help_text="The audio url to play to the user while waiting for a response if using voice",
)
twilio_tts_voice = models.TextField(
default="",
blank=True,
help_text="The voice to use for Twilio TTS ('man', 'woman', or Amazon Polly/Google Voices: https://www.twilio.com/docs/voice/twiml/say/text-speech#available-voices-and-languages)",
)
twilio_asr_language = models.TextField(
default="",
blank=True,
help_text="The language to use for Twilio ASR (https://www.twilio.com/docs/voice/twiml/gather#languagetags)",
)

streaming_enabled = models.BooleanField(
default=False,
Expand Down Expand Up @@ -1239,10 +1229,9 @@ def to_df_format(
metadata__mime_type__startswith="image/"
).values_list("url", flat=True)
),
"Audio Input": ", ".join(
message.attachments.filter(
metadata__mime_type__startswith="audio/"
).values_list("url", flat=True)
"Audio Input": (
(message.saved_run and message.saved_run.state.get("input_audio"))
or ""
),
}
rows.append(row)
Expand Down
2 changes: 2 additions & 0 deletions bots/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def send_broadcast_msgs_chunked(
buttons: list[ReplyButton] = None,
convo_qs: QuerySet[Conversation],
bi: BotIntegration,
medium: str = "Voice Call",
):
convo_ids = list(convo_qs.values_list("id", flat=True))
for i in range(0, len(convo_ids), 100):
Expand All @@ -161,6 +162,7 @@ def send_broadcast_msgs_chunked(
documents=documents,
bi_id=bi.id,
convo_ids=convo_ids[i : i + 100],
medium=medium,
)


Expand Down
21 changes: 11 additions & 10 deletions celeryapp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from time import time
from types import SimpleNamespace

import gooey_gui as gui
import requests
import sentry_sdk
from django.db.models import Sum
from django.utils import timezone
from fastapi import HTTPException
from loguru import logger

import gooey_ui as st
from app_users.models import AppUser, AppUserTransaction
from bots.admin_links import change_obj_url
from bots.models import SavedRun, Platform, Workflow
Expand All @@ -22,8 +23,6 @@
from daras_ai_v2.exceptions import UserError
from daras_ai_v2.send_email import send_email_via_postmark, send_low_balance_email
from daras_ai_v2.settings import templates
from gooey_ui.pubsub import realtime_push
from gooey_ui.state import set_query_params
from gooeysite.bg_db_conn import db_middleware
from payments.auto_recharge import (
should_attempt_auto_recharge,
Expand All @@ -41,6 +40,7 @@ def runner_task(
run_id: str,
uid: str,
channel: str,
unsaved_state: dict[str, typing.Any] = None,
) -> int:
start_time = time()
error_msg = None
Expand Down Expand Up @@ -68,34 +68,35 @@ def save_on_step(yield_val: str | tuple[str, dict] = None, *, done: bool = False
# extract outputs from local state
| {
k: v
for k, v in st.session_state.items()
for k, v in gui.session_state.items()
if k in page.ResponseModel.__fields__
}
# add extra outputs from the run
| extra_output
)

# send outputs to ui
realtime_push(channel, output)
gui.realtime_push(channel, output)
# save to db
page.dump_state_to_sr(st.session_state | output, sr)
page.dump_state_to_sr(gui.session_state | output, sr)

user = AppUser.objects.get(id=user_id)
page = page_cls(request=SimpleNamespace(user=user))
page.setup_sentry()
sr = page.run_doc_sr(run_id, uid)
st.set_session_state(sr.to_dict())
set_query_params(dict(run_id=run_id, uid=uid))
gui.set_session_state(sr.to_dict() | (unsaved_state or {}))
gui.set_query_params(dict(run_id=run_id, uid=uid))

try:
save_on_step()
for val in page.main(sr, st.session_state):
for val in page.main(sr, gui.session_state):
save_on_step(val)

# render errors nicely
except Exception as e:
if isinstance(e, UserError):
sentry_level = e.sentry_level
logger.warning(e)
else:
sentry_level = "error"
traceback.print_exc()
Expand All @@ -106,7 +107,7 @@ def save_on_step(yield_val: str | tuple[str, dict] = None, *, done: bool = False

# run completed successfully, deduct credits
else:
sr.transaction, sr.price = page.deduct_credits(st.session_state)
sr.transaction, sr.price = page.deduct_credits(gui.session_state)

# save everything, mark run as completed
finally:
Expand Down
2 changes: 1 addition & 1 deletion components_doc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
from functools import wraps

import gooey_ui as gui
import gooey_gui as gui

META_TITLE = "Gooey Components"
META_DESCRIPTION = "Explore the Gooey Component Library"
Expand Down
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def mock_celery_tasks():
with (
patch("celeryapp.tasks.runner_task", _mock_runner_task),
patch("celeryapp.tasks.post_runner_tasks", _mock_post_runner_tasks),
patch("daras_ai_v2.bots.realtime_subscribe", _mock_realtime_subscribe),
patch("gooey_gui.realtime_subscribe", _mock_realtime_subscribe),
):
yield

Expand Down
37 changes: 27 additions & 10 deletions daras_ai/image_input.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import math
import mimetypes
import os
import re
import typing
import uuid
from pathlib import Path

import math
import numpy as np
import requests
from PIL import Image, ImageOps
Expand All @@ -13,6 +14,9 @@
from daras_ai_v2 import settings
from daras_ai_v2.exceptions import UserError

if typing.TYPE_CHECKING:
from google.cloud.storage import Blob, Bucket


def resize_img_pad(img_bytes: bytes, size: tuple[int, int]) -> bytes:
img_cv2 = bytes_to_cv2_img(img_bytes)
Expand Down Expand Up @@ -57,25 +61,38 @@ def upload_file_from_bytes(
data: bytes,
content_type: str = None,
) -> str:
if not content_type:
content_type = mimetypes.guess_type(filename)[0]
content_type = content_type or "application/octet-stream"
blob = storage_blob_for(filename)
blob.upload_from_string(data, content_type=content_type)
blob = gcs_blob_for(filename)
upload_gcs_blob_from_bytes(blob, data, content_type)
return blob.public_url


def storage_blob_for(filename: str) -> "storage.storage.Blob":
from firebase_admin import storage

def gcs_blob_for(filename: str) -> "Blob":
filename = safe_filename(filename)
bucket = storage.bucket(settings.GS_BUCKET_NAME)
bucket = gcs_bucket()
blob = bucket.blob(
os.path.join(settings.GS_MEDIA_PATH, str(uuid.uuid1()), filename)
)
return blob


def upload_gcs_blob_from_bytes(
blob: "Blob",
data: bytes,
content_type: str = None,
) -> str:
if not content_type:
content_type = mimetypes.guess_type(blob.path)[0]
content_type = content_type or "application/octet-stream"
blob.upload_from_string(data, content_type=content_type)
return blob.public_url


def gcs_bucket() -> "Bucket":
from firebase_admin import storage

return storage.bucket(settings.GS_BUCKET_NAME)


def cv2_img_to_bytes(img: np.ndarray) -> bytes:
import cv2

Expand Down
Loading

0 comments on commit 73cb176

Please sign in to comment.