Skip to content

Commit

Permalink
More linting - ui/music/utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
inFocus7 committed Jan 15, 2024
1 parent 2d4cd61 commit fd7b6c3
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10"]
python-version: ["3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand Down
25 changes: 13 additions & 12 deletions api/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def get_chat_response(client: openai.Client, api_model: str, role: str, prompt:

# Give the model previous chat context
if context is not None and len(context) > 0:
for c in context:
message.append(c)
for curr_context in context:
message.append(curr_context)

message.append({
"role": "user",
Expand All @@ -102,16 +102,17 @@ def get_chat_response(client: openai.Client, api_model: str, role: str, prompt:

response = response.choices[0]
if response.finish_reason != "stop":
if response.finish_reason == "length":
gr.Warning(
f"finish_reason: {response.finish_reason}. The maximum number of tokens specified in the request was "
f"reached.")
return None
elif response.finish_reason == "content_filter":
gr.Warning(
f"finish_reason: {response.finish_reason}. The content was omitted due to a flag from OpenAI's content "
f"filters.")
return None
match response.finish_reason:
case "length":
gr.Warning(
f"finish_reason: {response.finish_reason}. The maximum number of tokens specified in the request "
f"was reached.")
return None
case "content_filter":
gr.Warning(
f"finish_reason: {response.finish_reason}. The content was omitted due to a flag from OpenAI's "
f"content filters.")
return None

content = response.message.content
if content is None or content == "":
Expand Down
51 changes: 37 additions & 14 deletions processing/image.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from typing import Optional, Literal
from PIL import ImageFont, ImageDraw, Image, ImageFilter
import numpy as np
"""
This module contains functions for processing images.
"""
import textwrap
import gradio as gr
import uuid
from datetime import datetime
import os
import cv2
from pathlib import Path
import utils.path_handler as path_handler
from datetime import datetime
from typing import Optional, Literal, Union, Tuple
from PIL import ImageFont, ImageDraw, Image, ImageFilter
import numpy as np
import gradio as gr
import cv2
from utils import path_handler
import utils.gradio as gru

image_folder = "images"
default_path = os.path.join(path_handler.get_default_path(), image_folder)
IMAGE_FOLDER = "images"
default_path = os.path.join(path_handler.get_default_path(), IMAGE_FOLDER)


def render_image_output() -> (gr.Image, gr.Textbox, gr.Dropdown, gr.Button):
Expand Down Expand Up @@ -212,9 +215,29 @@ def save_image_to_disk(image_path: str, name: Optional[str] = None, save_dir: st


# Function to add text to an image with custom font, size, and wrapping
def add_text(image, text, position, font_path, font_size, font_color=(255, 255, 255, 255), shadow_color=(255, 255, 255),
shadow_radius=None, max_width=None, show_background=False, show_shadow=False,
background_color=(0, 0, 0, 255), x_center=False):
def add_text(image: Union[Image.Image, np.ndarray], text: str, position: Tuple[int, int], font_path: str,
font_size: int, font_color: Tuple[int, int, int, int] = (255, 255, 255, 255),
shadow_color: Tuple[int, int, int, int] = (255, 255, 255, 255),
shadow_radius: Optional[int] = None, max_width: Optional[int] = None, show_background: bool = False,
show_shadow: bool = False, background_color: Tuple[int, int, int, int] = (0, 0, 0, 255),
x_center: bool = False) -> (np.ndarray, Tuple[int, int]):
"""
Adds text to an image with custom font, size, and wrapping.
:param image: The image to add text to.
:param text: The text to add to the image.
:param position: The (x, y) position of the text on the image.
:param font_path: The path to the font to use.
:param font_size: The size of the font.
:param font_color: The color of the font.
:param shadow_color: The color of the shadow.
:param shadow_radius: The radius of the shadow.
:param max_width: The maximum width of the text before wrapping.
:param show_background: Whether to show a background behind the text.
:param show_shadow: Whether to show a shadow behind the text.
:param background_color: The color of the background.
:param x_center: Whether to center the text on the x-axis. This ignores the positional x parameter.
:return: A tuple containing the image with text added and the size of the text block.
"""
if not isinstance(position, tuple):
raise TypeError("Position must be a 2-tuple.", type(position))

Expand All @@ -231,7 +254,7 @@ def add_text(image, text, position, font_path, font_size, font_color=(255, 255,
font = ImageFont.truetype(font_path, font_size)
draw = ImageDraw.Draw(txt_layer)

img_width, img_height = image_pil.size
img_width, _ = image_pil.size

if max_width: # Prepare for text wrapping if max_width is provided
wrapped_text = textwrap.fill(text, width=max_width)
Expand All @@ -244,7 +267,7 @@ def add_text(image, text, position, font_path, font_size, font_color=(255, 255,
y_offset = 0
max_line_width = 0 # Keep track of the widest line
total_height = 0 # Accumulate total height of text block
for i, line in enumerate(lines):
for line in lines:
bbox = draw.textbbox((0, 0), line, font=font)
line_width = bbox[2] - bbox[0]
line_height = bbox[3] - bbox[1]
Expand Down
5 changes: 3 additions & 2 deletions ui/music/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ def render_process_cover() -> (gr.Button, gr.Image, gr.Image):
with gr.Column():
gr.Markdown("## Input")
with gr.Group():
input_image = gr.Image(sources=["upload"], label="Cover Image (png)", type="filepath", show_download_button=False,
scale=2, elem_classes=["single-image-input"], image_mode="RGBA")
input_image = gr.Image(sources=["upload"], label="Cover Image (png)", type="filepath",
show_download_button=False, scale=2, elem_classes=["single-image-input"],
image_mode="RGBA")

with gr.Row(equal_height=False):
with gr.Group():
Expand Down
Loading

0 comments on commit fd7b6c3

Please sign in to comment.