Skip to content

Commit

Permalink
add typing
Browse files Browse the repository at this point in the history
  • Loading branch information
kostas2370 committed Mar 31, 2024
1 parent 8dde490 commit 03fa008
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 53 deletions.
12 changes: 7 additions & 5 deletions src/videomanagement/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations
from django.db import models
from django.contrib.auth import get_user_model
from random import randint
from typing import Union

TEMPLATE_CHOICES = (("EDUCATIONAL", "Educational"), ("GAMING", "Gaming"), ("ADVERTISEMENT", "Advertisement"),
("STORY", "Story"), ("OTHER", "Other"))
Expand Down Expand Up @@ -32,7 +34,7 @@ def __str__(self):
return self.title

@staticmethod
def get_template(template_select: str):
def get_template(template_select: str) -> Union[TemplatePrompts, None]:
if template_select.isnumeric():
template = TemplatePrompts.objects.filter(id = template_select)

Expand All @@ -55,7 +57,7 @@ def __str__(self):
return self.name

@staticmethod
def select_music(category=None):
def select_music(category: str = None) -> Union[Music, None]:
if category is None:
music = Music.objects.all()

Expand Down Expand Up @@ -107,7 +109,7 @@ def __str__(self):
return self.name

@staticmethod
def select_voice():
def select_voice() -> VoiceModels:
voice = VoiceModels.objects.all()
return voice[randint(0, voice.count()-1)]

Expand All @@ -123,7 +125,7 @@ def __str__(self):
return self.name

@staticmethod
def select_avatar(selected='random', voice_model=None):
def select_avatar(selected: str = 'random', voice_model: VoiceModels = None) -> Union[Avatars, None]:
if selected == 'random':
if voice_model is None:
avatars = Avatars.objects.all()
Expand Down Expand Up @@ -156,7 +158,7 @@ def __str__(self):
return self.name

@staticmethod
def select_background(category=None):
def select_background(category: str = None) -> Backgrounds:
if category is not None:
back = Backgrounds.objects.filter(category = category)
else:
Expand Down
2 changes: 1 addition & 1 deletion src/videomanagement/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
router = routers.DefaultRouter()


router.register('templates', TemplatePromptView)
# router.register('templates', TemplatePromptView)
router.register('generate', GenerateView)
router.register('video', VideoView)
router.register('avatars', AvatarView)
Expand Down
8 changes: 3 additions & 5 deletions src/videomanagement/utils/audio_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import uuid


def make_scenes_speech(video):
def make_scenes_speech(video: Videos) -> None:
dir_name = video.dir_name
voice_model = video.voice_model
syn = voice_model.path
Expand Down Expand Up @@ -32,10 +32,10 @@ def make_scenes_speech(video):
sound = save(syn, j['dialogue'], save_path = f'{dir_name}/dialogues/{filename}.wav')
Scene.objects.create(file = sound, prompt = video.prompt, text = j['dialogue'].strip())

return True
return


def update_scene(scene):
def update_scene(scene: Scene) -> None:
video = Videos.objects.get(prompt__id=scene.prompt.id)
dir_name = video.dir_name
voice_model = video.voice_model
Expand All @@ -51,5 +51,3 @@ def update_scene(scene):
sound = save(syn, scene.text, save_path = f'{dir_name}/dialogues/{filename}.wav')
scene.file = sound
scene.save()

return True
28 changes: 14 additions & 14 deletions src/videomanagement/utils/download_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pytube import Playlist
from ..models import Music, Scene, SceneImage
from ..models import Music, Scene, SceneImage, Videos
import uuid
from .bing_image_downloader import downloader
import os
Expand All @@ -13,7 +13,7 @@
from .video_utils import split_video_and_mp3, add_text_to_video


def download_playlist(url, category):
def download_playlist(url: str, category: str) -> None:
playlist = Playlist(url)
for music in playlist.videos:
stream = music.streams.filter(only_audio = True).first()
Expand All @@ -32,27 +32,25 @@ def download_playlist(url, category):
except FileNotDownloadedError:
pass

return True


def download_image(query, path, amount=1):
def download_image(query: str, path: str, amount: int = 1) -> list[str]:
return downloader.download(query = f'{query}', limit = amount, output_dir = path,
adult_filter_off = True,
force_replace = False, timeout = 60, filter = 'photo')


def download_image_from_google(q, path, amt=1):
def download_image_from_google(q: str, path: str, amt: int = 1) -> str:
return google_downloader.download(q = q, path = path, amt = amt)


def check_which_file_exists(images):
def check_which_file_exists(images: list) -> str:
for i in images:
if os.path.exists(i):
return i
return None


def generate_from_dalle(prompt, dir_name, style, title=""):
def generate_from_dalle(prompt: str, dir_name: str, style: str, title: str = "") -> str:

client = OpenAI(api_key=settings.OPEN_API_KEY)

Expand All @@ -73,7 +71,9 @@ def generate_from_dalle(prompt, dir_name, style, title=""):
return rf"{dir_name}/images/{x}.png"


def create_image_scene(prompt, image, text, dir_name, mode="WEB", provider="bing", style="", title=""):
def create_image_scene(prompt: str, image: str, text: str, dir_name: str, mode: str = "WEB", provider: str = "bing",
style: str = "", title: str = "") -> None:

scene = Scene.objects.get(prompt = prompt, text = text.strip())

if mode == "DALL-E":
Expand All @@ -99,7 +99,7 @@ def create_image_scene(prompt, image, text, dir_name, mode="WEB", provider="bing
SceneImage.objects.create(scene = scene, file = downloaded_image, prompt = image)


def create_image_scenes(video, mode="WEB", style="natural"):
def create_image_scenes(video: Videos, mode: str = "WEB", style: str = "natural") -> None:
is_sentenced = True if video.prompt.template is None else video.prompt.template.is_sentenced
dir_name = video.dir_name
search_field = "scene" if "scene" in video.gpt_answer["scenes"][0] and \
Expand Down Expand Up @@ -129,14 +129,14 @@ def create_image_scenes(video, mode="WEB", style="natural"):
title = video.title)


def download_video(url, dir_name):
def download_video(url: str, dir_name: str) -> str:
yt = YouTube(url)
video = yt.streams.get_highest_resolution()
video.download(dir_name)
return rf'{dir_name}{yt.title}.mp4'


def download_music(url):
def download_music(url: str) -> str:
yt = YouTube(url)

video = yt.streams.filter(only_audio = True).first()
Expand All @@ -152,7 +152,7 @@ def download_music(url):
return mus


def generate_new_image(scene_image, video, style="vivid"):
def generate_new_image(scene_image: SceneImage, video: Videos, style: str = "vivid") -> SceneImage:
if video.mode == "DALL-E":
try:
img = generate_from_dalle(scene_image.prompt, video.dir_name, style, title = video.title)
Expand All @@ -170,7 +170,7 @@ def generate_new_image(scene_image, video, style="vivid"):
return scene_image


def create_twitch_clip_scene(clip, title, prompt):
def create_twitch_clip_scene(clip: str, title: str, prompt: str) -> None:
splited_clip = split_video_and_mp3(clip)
edited_video = add_text_to_video(splited_clip[1], title, x = 80, y = 900)

Expand Down
2 changes: 1 addition & 1 deletion src/videomanagement/utils/file_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os


def generate_directory(name, x=0):
def generate_directory(name: str, x: int = 0) -> str:

while True:
dir_name = (name + (' ' + str(x) if x is not 0 else '')).strip()
Expand Down
14 changes: 9 additions & 5 deletions src/videomanagement/utils/google_image_downloader/downloader.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import requests
import urllib.request
import uuid
from django.conf import settings
from ..gpt_utils import select_from_vision
from django.conf import settings
from requests import Response
from typing import Union


def build_payload(query, start=1, num=1, **params):
def build_payload(query: str, start: int = 1, num: int = 1, **params) -> dict:
payload = {'key': settings.API_KEY,
'q': query, 'cx': settings.SEARCH_ENGINE_ID,
'start': start,
Expand All @@ -20,18 +22,20 @@ def build_payload(query, start=1, num=1, **params):
return payload


def make_request(payload):
def make_request(payload: dict) -> Response:
response = requests.get('https://www.googleapis.com/customsearch/v1', params = payload)
if response.status_code != 200:
raise Exception('Request Failed')
return response


def download(q, amt=1, path=''):
def download(q: str, amt: int = 1, path: str = '') -> Union[str, None]:
payload = build_payload(q, num = amt)
try:
response = make_request(payload)
except:

except Exception as exc:
print(exc)
return None

if response.status_code != 200:
Expand Down
6 changes: 4 additions & 2 deletions src/videomanagement/utils/gpt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import requests


def check_json(json_file):
def check_json(json_file: json) -> bool:
if "scenes" not in json_file:
return False

Expand All @@ -30,12 +30,14 @@ def get_reply(prompt, time=0, reply_format="json", gpt_model='gpt-4'):
g4f.logging = True # enable logging
g4f.check_version = False

gpt_model = g4f.models.gpt_4 if gpt_model == "gpt-4" else 'gpt-3.5-turbo'
gpt_model = g4f.models.gpt_4_turbo if gpt_model == "gpt-4" else 'gpt-3.5-turbo'

response = g4f.ChatCompletion.create(model = gpt_model, messages = [{"content": prompt}], stream = True,
)

x = io.StringIO()
for message in response:

x.write(message)

if reply_format == "json":
Expand Down
10 changes: 6 additions & 4 deletions src/videomanagement/utils/prompt_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
def format_prompt(template_format, template_category, userprompt="", title='', target_audience=''):
def format_prompt(template_format: str, template_category: str, userprompt: str = "", title: str = '',
target_audience: str = '') -> str:

if title == '':
title = "The title will be selected by you, depending on the prompt"
Expand All @@ -18,7 +19,8 @@ def format_prompt(template_format, template_category, userprompt="", title='', t
return output


def format_prompt_for_official_gpt(template_format, template_category, userprompt="", title='', target_audience=''):
def format_prompt_for_official_gpt(template_format: str, template_category: str, userprompt: str = "", title: str = '',
target_audience: str = ''):

if title == '':
title = "The title will be selected by you, depending on the prompt"
Expand All @@ -36,10 +38,10 @@ def format_prompt_for_official_gpt(template_format, template_category, userpromp
return system_output, user_output


def format_update_form(text, prompt):
def format_update_form(text: str, prompt: str) -> str:
return f"The text i will give you is a scene in a video. {text}. Rewrite this text: {prompt} . " \
f"The text must be around the same size"


def format_dalle_prompt(title, image_description):
def format_dalle_prompt(title: str, image_description: str) -> str:
return f'Title : {title} \nImage Description:{image_description}'
7 changes: 4 additions & 3 deletions src/videomanagement/utils/tts_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from TTS.utils.synthesizer import Synthesizer
from TTS.utils.manage import ModelManager
import os
from typing import Union

from .gpt_utils import tts_from_open_api, tts_from_eleven_labs
from dataclasses import dataclass
Expand All @@ -12,8 +13,8 @@ class ApiSyn:
path: str


def create_model(model_path=rf"{os.path.abspath(os.getcwd())}\.models.json",
model="tts_models/en/ljspeech/vits--neon", vocoder="default_vocoder"):
def create_model(model_path: str = rf"{os.path.abspath(os.getcwd())}\.models.json",
model: str = "tts_models/en/ljspeech/vits--neon", vocoder: str = "default_vocoder") -> Synthesizer:

model_manager = ModelManager(model_path)
model_path, config_path, model_item = model_manager.download_model(model)
Expand All @@ -40,7 +41,7 @@ def create_model(model_path=rf"{os.path.abspath(os.getcwd())}\.models.json",
return syn


def save(syn, text="", save_path=""):
def save(syn: Union[Synthesizer, ApiSyn], text: str = "", save_path: str = "") -> str:
if type(syn) is Synthesizer:
outputs = syn.tts(text)
syn.save_wav(outputs, save_path)
Expand Down
10 changes: 5 additions & 5 deletions src/videomanagement/utils/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, path):
self.path = path
self.headers = None

def set_headers(self):
def set_headers(self) -> dict:
headers = {'Content-Type': 'application/x-www-form-urlencoded', }
data = f'client_id={settings.TWITCH_CLIENT}&client_secret={settings.TWITCH_CLIENT_SECRET}' \
f'&grant_type=client_credentials'
Expand All @@ -30,7 +30,7 @@ def set_headers(self):
self.headers = {"Authorization": f"Bearer {bearer}", "Client-Id": settings.TWITCH_CLIENT}
return {"Authorization": f"Bearer {bearer}", "Client-Id": settings.TWITCH_CLIENT}

def get_game_id(self, name):
def get_game_id(self, name: str) -> str:
if self.headers is None:
raise HeaderInitiationError

Expand All @@ -44,7 +44,7 @@ def get_game_id(self, name):

return req.json().get("data")[0].get("id")

def get_streamer_id(self, name):
def get_streamer_id(self, name: str) -> str:

url = f'https://api.twitch.tv/helix/users?login={name}'
req = requests.get(url, headers = self.headers)
Expand All @@ -56,7 +56,7 @@ def get_streamer_id(self, name):

return req.json().get("data").get("id")

def get_clips(self, value, mode="game", start_date=""):
def get_clips(self, value: str, mode="game", start_date: str = ""):
base_url = "https://api.twitch.tv/helix/clips"

url = f"{base_url}?game_id={value}" if mode == "game" else \
Expand All @@ -69,7 +69,7 @@ def get_clips(self, value, mode="game", start_date=""):

return clips.json().get("data")

def download_clip(self, clip):
def download_clip(self, clip) -> str:
index = clip.get("thumbnail_url").find('-preview')
filename = f'{str(uuid.uuid4())}.mp4'
urllib.request.urlretrieve(clip['thumbnail_url'][:index]+".mp4", f'{self.path}\\{filename}')
Expand Down
Loading

0 comments on commit 03fa008

Please sign in to comment.