Skip to content

Commit

Permalink
Merge pull request #44 from banodoco/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
piyushK52 authored Nov 23, 2023
2 parents 0d9fca7 + 37aabab commit c105750
Show file tree
Hide file tree
Showing 17 changed files with 405 additions and 230 deletions.
8 changes: 6 additions & 2 deletions backend/db_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ def update_app_setting(self, **kwargs):
return InternalResponse({}, 'app_setting updated successfully', True)


def get_app_secrets_from_user_uuid(self, user_uuid):
def get_app_secrets_from_user_uuid(self, user_uuid, secret_access=None):
if user_uuid:
user: User = User.objects.filter(uuid=user_uuid, is_disabled=False).first()
if not user:
Expand Down Expand Up @@ -1412,7 +1412,11 @@ def release_lock(self, key):

# shot
def get_shot_from_number(self, project_uuid, shot_number=0):
shot: Shot = Shot.objects.filter(project_id=project_uuid, shot_idx=shot_number, is_disabled=False).first()
project = Project.objects.filter(uuid=project_uuid, is_disabled=False).first()
if not project:
return InternalResponse({}, 'invalid project uuid', False)

shot: Shot = Shot.objects.filter(project_id=project.id, shot_idx=shot_number, is_disabled=False).first()
if not shot:
return InternalResponse({}, 'invalid shot number', False)

Expand Down
22 changes: 13 additions & 9 deletions banodoco_settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
import copy
import uuid
import streamlit as st

Expand Down Expand Up @@ -89,7 +89,7 @@ def create_new_project(user: InternalUserObject, project_name: str, width=512, h
shot_data = {
"project_uuid": project.uuid,
"desc": "",
"duration": 2
"duration": 10
}

shot = data_repo.create_shot(**shot_data)
Expand All @@ -99,7 +99,10 @@ def create_new_project(user: InternalUserObject, project_name: str, width=512, h
sample_file_location = "sample_assets/sample_images/v.jpeg"
img = Image.open(sample_file_location)
img = img.resize((width, height))
hosted_url = save_or_host_file(img, sample_file_location, mime_type='image/png', dim=(width, height))

unique_file_name = f"{str(uuid.uuid4())}.png"
file_location = f"videos/{project.uuid}/resources/prompt_images/{unique_file_name}"
hosted_url = save_or_host_file(img, file_location, mime_type='image/png', dim=(width, height))
file_data = {
"name": str(uuid.uuid4()),
"type": InternalFileType.IMAGE.value,
Expand All @@ -110,7 +113,7 @@ def create_new_project(user: InternalUserObject, project_name: str, width=512, h
if hosted_url:
file_data.update({'hosted_url': hosted_url})
else:
file_data.update({'local_path': sample_file_location})
file_data.update({'local_path': file_location})

source_image = data_repo.create_file(**file_data)

Expand Down Expand Up @@ -148,11 +151,12 @@ def create_predefined_models(user):

# create predefined models
data = []
for model in ML_MODEL_LIST:
if model['enabled']:
del model['enabled']
model['user_id'] = user.uuid
data.append(model)
predefined_model_list = copy.deepcopy(ML_MODEL_LIST)
for m in predefined_model_list:
if 'enabled' in m and m['enabled']:
del m['enabled']
m['user_id'] = user.uuid
data.append(m)

# only creating pre-defined models for the first time
available_models = data_repo.get_all_ai_model_list(\
Expand Down
Binary file modified sample_assets/sample_images/v.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion shared/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,12 @@ class SortOrder(ExtendedEnum):
ENCRYPTION_KEY = os.getenv('ENCRYPTION_KEY', 'J2684nBgNUYa_K0a6oBr5H8MpSRW0EJ52Qmq7jExE-w=')

QUEUE_INFERENCE_QUERIES = True
HOSTED_BACKGROUND_RUNNER_MODE = os.getenv('HOSTED_BACKGROUND_RUNNER_MODE', False)
HOSTED_BACKGROUND_RUNNER_MODE = os.getenv('HOSTED_BACKGROUND_RUNNER_MODE', False)

if OFFLINE_MODE:
SECRET_ACCESS_TOKEN = os.getenv('SECRET_ACCESS_TOKEN', None)
else:
import boto3
ssm = boto3.client("ssm", region_name="ap-south-1")

SECRET_ACCESS_TOKEN = ssm.get_parameter(Name='/backend/banodoco/secret-access-token')['Parameter']['Value']
1 change: 0 additions & 1 deletion ui_components/components/app_settings_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

def app_settings_page():
data_repo = DataRepo()
app_secrets = data_repo.get_app_secrets_from_user_uuid()

if SERVER == ServerType.DEVELOPMENT.value:
st.subheader("Purchase Credits")
Expand Down
4 changes: 2 additions & 2 deletions ui_components/components/new_project_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def new_project_page():
new_project_name = new_project_name.replace(" ", "_")
current_user = data_repo.get_first_active_user()

new_project, shot = create_new_project(current_user, new_project_name, width, height, "Images", "Interpolation")
new_project, shot = create_new_project(current_user, new_project_name, width, height)
new_timing = create_frame_inside_shot(shot.uuid, 0)

if starting_image:
Expand All @@ -100,7 +100,7 @@ def new_project_page():

# remvoing the initial frame which moved to the 1st position
# (since creating new project also creates a frame)
shot = data_repo.get_shot_from_number(new_project.uuid, 0)
shot = data_repo.get_shot_from_number(new_project.uuid, 1)
initial_frame = data_repo.get_timing_from_frame_number(shot.uuid, 0)
data_repo.delete_timing_from_uuid(initial_frame.uuid)

Expand Down
30 changes: 20 additions & 10 deletions ui_components/components/project_settings_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import time
from ui_components.widgets.attach_audio_element import attach_audio_element
from PIL import Image

from utils.data_repo.data_repo import DataRepo

Expand All @@ -13,14 +14,23 @@ def project_settings_page(project_uuid):
project_settings = data_repo.get_project_setting(project_uuid)
attach_audio_element(project_uuid, True)

frame_sizes = ["512x512", "768x512", "512x768"]
current_size = f"{project_settings.width}x{project_settings.height}"
current_index = frame_sizes.index(current_size) if current_size in frame_sizes else 0

with st.expander("Frame Size", expanded=True):
st.write("Current Size = ",
project_settings.width, "x", project_settings.height)
width = st.selectbox("Select video width", options=[
"512", "683", "704", "1024"], key="video_width")
height = st.selectbox("Select video height", options=[
"512", "704", "1024"], key="video_height")
if st.button("Save"):
data_repo.update_project_setting(project_uuid, width=width)
data_repo.update_project_setting(project_uuid, height=height)
st.rerun()
v1, v2, v3 = st.columns([4, 4, 2])
with v1:
st.write("Current Size = ", project_settings.width, "x", project_settings.height)

frame_size = st.radio("Select frame size:", options=frame_sizes, index=current_index, key="frame_size", horizontal=True)
width, height = map(int, frame_size.split('x'))


img = Image.new('RGB', (width, height), color = (73, 109, 137))
st.image(img, width=70)

if st.button("Save"):
data_repo.update_project_setting(project_uuid, width=width)
data_repo.update_project_setting(project_uuid, height=height)
st.experimental_rerun()
7 changes: 4 additions & 3 deletions ui_components/methods/common_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from io import BytesIO
import numpy as np
import urllib3
from shared.constants import SERVER, InferenceType, InternalFileTag, InternalFileType, ProjectMetaData, ServerType
from shared.constants import OFFLINE_MODE, SERVER, InferenceType, InternalFileTag, InternalFileType, ProjectMetaData, ServerType
from pydub import AudioSegment
from backend.models import InternalFileObject
from shared.logging.constants import LoggingType
Expand Down Expand Up @@ -734,6 +734,7 @@ def process_inference_output(**kwargs):

inference_time = 0.0
inference_type = kwargs.get('inference_type')
log_uuid = None
# ------------------- FRAME TIMING IMAGE INFERENCE -------------------
if inference_type == InferenceType.FRAME_TIMING_IMAGE_INFERENCE.value:
output = kwargs.get('output')
Expand Down Expand Up @@ -882,7 +883,7 @@ def process_inference_output(**kwargs):

if inference_time:
credits_used = round(inference_time * 0.004, 3) # make this more granular for different models
data_repo.update_usage_credits(-credits_used)
data_repo.update_usage_credits(-credits_used, log_uuid)

return True

Expand Down Expand Up @@ -929,7 +930,7 @@ def update_app_setting_keys():
data_repo = DataRepo()
app_logger = AppLogger()

if True or SERVER == ServerType.DEVELOPMENT.value:
if OFFLINE_MODE:
key = os.getenv('REPLICATE_KEY', None)
else:
import boto3
Expand Down
45 changes: 36 additions & 9 deletions ui_components/methods/video_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import uuid
import ffmpeg
import streamlit as st
from moviepy.editor import concatenate_videoclips, VideoFileClip, AudioFileClip
from moviepy.editor import concatenate_videoclips, concatenate_audioclips, VideoFileClip, AudioFileClip, CompositeVideoClip
from pydub import AudioSegment

from shared.constants import InferenceType, InternalFileTag
from shared.file_upload.s3 import is_s3_image_url
Expand Down Expand Up @@ -123,6 +124,9 @@ def add_audio_to_video_slice(video_file, audio_bytes):
os.rename("output_with_audio.mp4", video_location)

def sync_audio_and_duration(video_file: InternalFileObject, shot_uuid, audio_sync_required=False):
'''
audio_sync_required: this ensures that entire video clip is filled with proper audio
'''
from ui_components.methods.file_methods import convert_bytes_to_file, generate_temp_file

data_repo = DataRepo()
Expand Down Expand Up @@ -165,11 +169,32 @@ def sync_audio_and_duration(video_file: InternalFileObject, shot_uuid, audio_syn
start_timestamp += round(shot_list[i - 1].duration, 2)

trimmed_audio_clip = None
if audio_clip.duration >= video_clip.duration and start_timestamp + video_clip.duration <= audio_clip.duration:
trimmed_audio_clip = audio_clip.subclip(start_timestamp, start_timestamp + video_clip.duration)
video_clip = video_clip.set_audio(trimmed_audio_clip)
audio_len_overlap = 0 # length of audio that can be added to the video clip
if audio_clip.duration >= start_timestamp:
audio_len_overlap = round(min(video_clip.duration, audio_clip.duration - start_timestamp), 2)


# audio doesn't fit the video clip
if audio_len_overlap < video_clip.duration and audio_sync_required:
return None

if audio_len_overlap:
trimmed_audio_clip = audio_clip.subclip(start_timestamp, start_timestamp + audio_len_overlap)
trimmed_audio_clip_duration = round(trimmed_audio_clip.duration, 2)
if trimmed_audio_clip_duration < video_clip.duration:
video_with_sound = video_clip.subclip(0, trimmed_audio_clip_duration)
video_with_sound = video_with_sound.copy()
video_without_sound = video_clip.subclip(trimmed_audio_clip_duration)
video_without_sound = video_without_sound.copy()
video_with_sound = video_with_sound.set_audio(trimmed_audio_clip)
video_clip = concatenate_videoclips([video_with_sound, video_without_sound])
else:
video_clip = video_clip.set_audio(trimmed_audio_clip)
else:
return None if audio_sync_required else output_video
for file in temp_file_list:
os.remove(file.name)

return output_video

# writing the video to the temp file
output_temp_video_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
Expand Down Expand Up @@ -197,6 +222,8 @@ def sync_audio_and_duration(video_file: InternalFileObject, shot_uuid, audio_syn
for file in temp_file_list:
os.remove(file.name)

output_video = data_repo.get_file_from_uuid(output_video.uuid)
_ = data_repo.get_shot_list(shot.project.uuid, invalidate_cache=True)
return output_video


Expand Down Expand Up @@ -226,7 +253,7 @@ def render_video(final_video_name, project_uuid, file_tag=InternalFileTag.GENERA
time.sleep(0.3)
return False

shot_video = sync_audio_and_duration(shot.main_clip, shot.uuid, audio_sync_required=True)
shot_video = sync_audio_and_duration(shot.main_clip, shot.uuid, audio_sync_required=False)
if not shot_video:
st.error("Audio sync failed. Length mismatch")
time.sleep(0.7)
Expand All @@ -236,11 +263,11 @@ def render_video(final_video_name, project_uuid, file_tag=InternalFileTag.GENERA
data_repo.add_interpolated_clip(shot.uuid, interpolated_clip_id=shot_video.uuid)

temp_video_file = None
if shot.main_clip.hosted_url:
temp_video_file = generate_temp_file(shot.main_clip.hosted_url, '.mp4')
if shot_video.hosted_url:
temp_video_file = generate_temp_file(shot_video.hosted_url, '.mp4')
temp_file_list.append(temp_video_file)

file_path = temp_video_file.name if temp_video_file else shot.main_clip.local_path
file_path = temp_video_file.name if temp_video_file else shot_video.local_path
video_list.append(file_path)

finalclip = concatenate_videoclips([VideoFileClip(v) for v in video_list])
Expand Down
2 changes: 1 addition & 1 deletion ui_components/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def setup_app_ui():

elif st.session_state["main_view_type"] == "Tools & Settings":
with st.sidebar:
tool_pages = ["Query Logger", "Custom Models", "Project Settings"]
tool_pages = ["Query Logger", "Project Settings"]

if st.session_state["page"] not in tool_pages:
st.session_state["page"] = tool_pages[0]
Expand Down
Loading

0 comments on commit c105750

Please sign in to comment.