Skip to content

Commit

Permalink
refactor(image-gen): update ImageGenerator class
Browse files Browse the repository at this point in the history
- Add logging with Loguru for better debugging
- Implement model setting functionality
- Update dependencies in requirements.txt
- Ignore settings.json in .gitignore
  • Loading branch information
rtuszik committed Aug 23, 2024
1 parent 0b81045 commit 62b3fed
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
settings.json
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
PyQt6==6.7.1
replicate==0.31.0
replicate==0.32.0
python-dotenv==1.0.1
token_count==0.2.1
pillow==10.4.0
loguru==0.7.2
nicegui==1.4.36
49 changes: 46 additions & 3 deletions src/image_generator.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,62 @@
import json
import sys

import replicate
from dotenv import load_dotenv
from loguru import logger

load_dotenv()

# Configure Loguru
logger.remove() # Remove the default handler
logger.add(
sys.stderr, format="{time} {level} {message}", filter="my_module", level="INFO"
)
logger.add(
"image_generator.log",
rotation="10 MB",
format="{time} {level} {message}",
level="INFO",
)


class ImageGenerator:
def __init__(self):
self.model = "rtuszik/fluxlyptus:b23b9b488de7af95eba09786ef3156d345d979024712f54b3e5a32d61f14e568"
self.replicate_model = None
logger.info("ImageGenerator initialized")

def set_model(self, replicate_model):
self.replicate_model = replicate_model
logger.info(f"Model set to: {replicate_model}")

def generate_images(self, params):
if not self.replicate_model:
error_message = (
"No Replicate model set. Please set a model before generating images."
)
logger.error(error_message)
raise ImageGenerationError(error_message)

try:
output = replicate.run(self.model, input=params)
# Remove the Flux model from params and store it separately
flux_model = params.pop("flux_model", "dev")

# Add the Flux model choice to the input parameters
params["model"] = flux_model

logger.info(
f"Generating images with params: {json.dumps(params, indent=2)}"
)
logger.info(f"Using Replicate model: {self.replicate_model}")

output = replicate.run(self.replicate_model, input=params)

logger.success(f"Images generated successfully. Output: {output}")
return output
except Exception as e:
raise ImageGenerationError(f"Error generating images: {str(e)}")
error_message = f"Error generating images: {str(e)}"
logger.exception(error_message)
raise ImageGenerationError(error_message)


class ImageGenerationError(Exception):
Expand Down

0 comments on commit 62b3fed

Please sign in to comment.