Skip to content

Commit

Permalink
Simplify typing
Browse files Browse the repository at this point in the history
  • Loading branch information
henryruhs committed Aug 8, 2023
1 parent fea6a0c commit ee4de80
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 20 deletions.
7 changes: 4 additions & 3 deletions roop/uis/__components__/output.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Any, Dict, Tuple, Optional
from typing import Tuple, Optional
import gradio

import roop.globals
from roop.core import start
from roop.uis.typing import Update
from roop.utilities import has_image_extension, has_video_extension, normalize_output_path


Expand Down Expand Up @@ -31,7 +32,7 @@ def listen() -> None:
CLEAR_BUTTON.click(clear, outputs=[OUTPUT_IMAGE, OUTPUT_VIDEO])


def update() -> Tuple[Dict[str, Any], Dict[str, Any]]:
def update() -> Tuple[Update, Update]:
roop.globals.output_path = normalize_output_path(roop.globals.source_path, roop.globals.target_path, '..')
if roop.globals.output_path:
start()
Expand All @@ -42,5 +43,5 @@ def update() -> Tuple[Dict[str, Any], Dict[str, Any]]:
return gradio.update(value=None, visible=False), gradio.update(value=None, visible=False)


def clear() -> Tuple[Dict[str, Any], Dict[str, Any]]:
def clear() -> Tuple[Update, Update]:
return gradio.update(value=None, visible=False), gradio.update(value=None, visible=False)
4 changes: 2 additions & 2 deletions roop/uis/__components__/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from roop.processors.frame.core import load_frame_processor_module
from roop.typing import Frame
from roop.uis import core as ui
from roop.uis.typing import ComponentName
from roop.uis.typing import ComponentName, Update
from roop.utilities import is_video, is_image

PREVIEW_IMAGE: Optional[gradio.Image] = None
Expand Down Expand Up @@ -67,7 +67,7 @@ def listen() -> None:
component.change(update, inputs=PREVIEW_SLIDER, outputs=[PREVIEW_IMAGE, PREVIEW_SLIDER])


def update(frame_number: int = 0) -> Tuple[Dict[Any, Any], Dict[Any, Any]]:
def update(frame_number: int = 0) -> Tuple[Update, Update]:
sleep(0.5)
if is_image(roop.globals.target_path):
temp_frame = cv2.imread(roop.globals.target_path)
Expand Down
10 changes: 5 additions & 5 deletions roop/uis/__components__/reference.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, Any, List, Optional
from typing import List, Optional
from time import sleep

import cv2
Expand All @@ -9,7 +9,7 @@
from roop.face_analyser import get_faces_total
from roop.face_reference import clear_face_reference
from roop.uis import core as ui
from roop.uis.typing import ComponentName
from roop.uis.typing import ComponentName, Update
from roop.utilities import is_image, is_video

REFERENCE_FACE_POSITION_SLIDER: Optional[gradio.Slider] = None
Expand Down Expand Up @@ -57,12 +57,12 @@ def listen() -> None:
REFERENCE_FACE_POSITION_SLIDER.change(clear_and_update_face_reference_position, inputs=REFERENCE_FACE_POSITION_SLIDER)


def clear_and_update_face_reference_position(reference_face_position: int) -> Dict[Any, Any]:
def clear_and_update_face_reference_position(reference_face_position: int) -> Update:
clear_face_reference()
return update_face_reference_position(reference_face_position)


def update_face_reference_position(reference_face_position: int) -> Dict[Any, Any]:
def update_face_reference_position(reference_face_position: int) -> Update:
sleep(0.5)
maximum = 0
roop.globals.reference_face_position = reference_face_position
Expand All @@ -75,6 +75,6 @@ def update_face_reference_position(reference_face_position: int) -> Dict[Any, An
return gradio.update(value=reference_face_position, maximum=maximum)


def update_similar_face_distance(similar_face_distance: float) -> Dict[Any, Any]:
def update_similar_face_distance(similar_face_distance: float) -> Update:
roop.globals.similar_face_distance = similar_face_distance
return gradio.update(value=similar_face_distance)
9 changes: 5 additions & 4 deletions roop/uis/__components__/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import roop.globals
from roop.processors.frame.core import list_frame_processors_names, load_frame_processor_module, clear_frame_processors_modules
from roop.uis import core as ui
from roop.uis.typing import Update

FRAME_PROCESSORS_CHECKBOX_GROUP: Optional[gradio.CheckboxGroup] = None
EXECUTION_PROVIDERS_CHECKBOX_GROUP: Optional[gradio.CheckboxGroup] = None
Expand Down Expand Up @@ -99,21 +100,21 @@ def sort_frame_processors(frame_processors: List[str]) -> list[str]:
return sorted(list_frame_processors_names(), key=frame_processor_key)


def update_execution_providers(execution_providers: List[str]) -> Dict[Any, Any]:
def update_execution_providers(execution_providers: List[str]) -> Update:
roop.globals.execution_providers = execution_providers
return gradio.update(value=execution_providers)


def update_execution_thread_count(execution_thread_count: int = 1) -> Dict[Any, Any]:
def update_execution_thread_count(execution_thread_count: int = 1) -> Update:
roop.globals.execution_thread_count = execution_thread_count
return gradio.update(value=execution_thread_count)


def update_execution_queue_count(execution_queue_count: int = 1) -> Dict[Any, Any]:
def update_execution_queue_count(execution_queue_count: int = 1) -> Update:
roop.globals.execution_queue_count = execution_queue_count
return gradio.update(value=execution_queue_count)


def update_checkbox(name: str, value: bool) -> Dict[Any, Any]:
def update_checkbox(name: str, value: bool) -> Update:
setattr(roop.globals, name, value)
return gradio.update(value=value)
5 changes: 3 additions & 2 deletions roop/uis/__components__/source.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Any, Dict, IO, Optional
from typing import Any, IO, Optional
import gradio

import roop.globals
from roop.uis import core as ui
from roop.uis.typing import Update
from roop.utilities import is_image

SOURCE_FILE: Optional[gradio.File] = None
Expand Down Expand Up @@ -33,7 +34,7 @@ def listen() -> None:
SOURCE_FILE.change(update, inputs=SOURCE_FILE, outputs=SOURCE_IMAGE)


def update(file: IO[Any]) -> Dict[str, Any]:
def update(file: IO[Any]) -> Update:
if file and is_image(file.name):
roop.globals.source_path = file.name
return gradio.update(value=file.name, visible=True)
Expand Down
5 changes: 3 additions & 2 deletions roop/uis/__components__/target.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Any, Dict, IO, Tuple, Optional
from typing import Any, IO, Tuple, Optional
import gradio

import roop.globals
from roop.face_reference import clear_face_reference
from roop.uis import core as ui
from roop.uis.typing import Update
from roop.utilities import is_image, is_video

TARGET_FILE: Optional[gradio.File] = None
Expand Down Expand Up @@ -42,7 +43,7 @@ def listen() -> None:
TARGET_FILE.change(update, inputs=TARGET_FILE, outputs=[TARGET_IMAGE, TARGET_VIDEO])


def update(file: IO[Any]) -> Tuple[Dict[str, Any], Dict[str, Any]]:
def update(file: IO[Any]) -> Tuple[Update, Update]:
clear_face_reference()
if file and is_image(file.name):
roop.globals.target_path = file.name
Expand Down
3 changes: 2 additions & 1 deletion roop/uis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import gradio

import roop.globals
import roop.metadata
from roop.typing import Frame
from roop.uis.typing import Component, ComponentName
from roop.utilities import list_module_names
Expand All @@ -18,7 +19,7 @@


def init() -> None:
with gradio.Blocks(theme=get_theme()) as ui:
with gradio.Blocks(theme=get_theme(), title=roop.metadata.name) as ui:
for ui_layout in roop.globals.ui_layouts:
ui_layout_module = load_ui_layout_module(ui_layout)
ui_layout_module.render()
Expand Down
3 changes: 2 additions & 1 deletion roop/uis/typing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal
from typing import Literal, Dict, Any
import gradio

Component = gradio.File or gradio.Image or gradio.Video or gradio.Slider
Expand All @@ -11,3 +11,4 @@
'frame_processors_checkbox_group',
'many_faces_checkbox'
]
Update = Dict[Any, Any]

0 comments on commit ee4de80

Please sign in to comment.